This commit is contained in:
Andy Gayton 2024-06-22 22:45:40 -04:00
parent b882f18333
commit f3830de758
4 changed files with 7 additions and 29 deletions

View File

@ -1,5 +1,5 @@
use crate::PersistentPlugin;
use nu_protocol::{engine::ctrlc, PluginGcConfig, RegisteredPlugin};
use nu_protocol::{PluginGcConfig, RegisteredPlugin};
use std::{
sync::{mpsc, Arc, Weak},
thread,
@ -14,7 +14,6 @@ use std::{
#[derive(Debug, Clone)]
pub struct PluginGc {
sender: mpsc::Sender<PluginGcMsg>,
_ctrlc_guard: Arc<Option<ctrlc::Guard>>,
}
impl PluginGc {
@ -22,7 +21,6 @@ impl PluginGc {
pub fn new(
config: PluginGcConfig,
plugin: &Arc<PersistentPlugin>,
ctrlc_handlers: Option<ctrlc::Handlers>,
) -> std::io::Result<PluginGc> {
let (sender, receiver) = mpsc::channel();
@ -39,14 +37,7 @@ impl PluginGc {
.name(format!("plugin gc ({})", plugin.identity().name()))
.spawn(move || state.run(receiver))?;
let guard = ctrlc_handlers.map(|ctrlc_handlers| {
let sender = sender.clone();
ctrlc_handlers.add(Box::new(move || {
let _ = sender.send(PluginGcMsg::Ctrlc);
}))
});
Ok(PluginGc { sender, _ctrlc_guard: Arc::new(guard) })
Ok(PluginGc { sender })
}
/// Update the garbage collector config
@ -94,26 +85,16 @@ impl PluginGc {
pub fn exited(&self) {
let _ = self.sender.send(PluginGcMsg::Exited);
}
/// Tell the GC that our process received a Ctrl-C signal
pub fn ctrlc(&self) {
let _ = self.sender.send(PluginGcMsg::Ctrlc);
}
pub fn clone_sender(&self) -> mpsc::Sender<PluginGcMsg> {
self.sender.clone()
}
}
#[derive(Debug)]
pub enum PluginGcMsg {
enum PluginGcMsg {
SetConfig(PluginGcConfig),
Flush(mpsc::Sender<()>),
AddLocks(i64),
SetDisabled(bool),
StopTracking,
Exited,
Ctrlc,
}
#[derive(Debug)]
@ -179,9 +160,6 @@ impl PluginGcState {
// Exit and stop the plugin
return Some(true);
}
PluginGcMsg::Ctrlc => {
eprintln!("Ctrl-C received, plugin: `{}`", self.name);
}
}
None
}

View File

@ -183,7 +183,7 @@ impl PersistentPlugin {
})?;
// Start the plugin garbage collector
let gc = PluginGc::new(mutable.gc_config.clone(), &self, ctrlc_handlers.clone())?;
let gc = PluginGc::new(mutable.gc_config.clone(), &self)?;
let pid = child.id();
let interface = make_plugin_interface(
@ -216,7 +216,7 @@ impl PersistentPlugin {
let guard = ctrlc_handlers.map(|ctrlc_handlers| {
let interface = interface.clone();
ctrlc_handlers.add(Box::new(move || {
ctrlc_handlers.register(Box::new(move || {
let _ = interface.ctrlc();
}))
});

View File

@ -503,7 +503,7 @@ impl EngineInterface {
/// todo: docs
pub fn register_ctrlc_handler(&self, handler: ctrlc::Handler) -> ctrlc::Guard {
self.state.ctrlc_handlers.add(handler)
self.state.ctrlc_handlers.register(handler)
}
/// Get the full shell configuration from the engine. As this is quite a large object, it is

View File

@ -40,7 +40,7 @@ impl Handlers {
Handlers { handlers, next_id }
}
pub fn add(&self, handler: Handler) -> Guard {
pub fn register(&self, handler: Handler) -> Guard {
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
if let Ok(mut handlers) = self.handlers.lock() {
handlers.push((id, handler));