This commit is contained in:
Andy Gayton 2024-07-04 20:47:10 -04:00
parent a0b0b7a7f8
commit 1060b65e14
3 changed files with 20 additions and 4 deletions

View File

@ -299,14 +299,23 @@ impl RegisteredPlugin for PersistentPlugin {
} }
} }
fn as_any(self: Arc<Self>) -> Arc<dyn std::any::Any + Send + Sync> {
self
}
fn set_ctrlc_handler_guard(&self, guard: ctrlc::Guard) { fn set_ctrlc_handler_guard(&self, guard: ctrlc::Guard) {
if let Ok(mut mutable) = self.mutable.lock() { if let Ok(mut mutable) = self.mutable.lock() {
mutable.ctrlc_guard = Some(guard); mutable.ctrlc_guard = Some(guard);
} }
} }
fn as_any(self: Arc<Self>) -> Arc<dyn std::any::Any + Send + Sync> { fn ctrlc(&self) -> Result<(), ShellError> {
self if let Ok(mutable) = self.mutable.lock() {
if let Some(ref running) = mutable.running {
return running.interface.ctrlc();
}
}
Ok(())
} }
} }

View File

@ -61,6 +61,7 @@ impl Handlers {
if let Ok(mut handlers) = self.handlers.lock() { if let Ok(mut handlers) = self.handlers.lock() {
handlers.push((id, handler)); handlers.push((id, handler));
} }
Ok(Guard { Ok(Guard {
id, id,
handlers: Arc::clone(&self.handlers), handlers: Arc::clone(&self.handlers),

View File

@ -262,9 +262,15 @@ impl EngineState {
for plugin in std::mem::take(&mut delta.plugins) { for plugin in std::mem::take(&mut delta.plugins) {
if let Some(handlers) = self.ctrlc_handlers.as_ref() { if let Some(handlers) = self.ctrlc_handlers.as_ref() {
let guard = { let guard = {
let plugin = plugin.clone(); // We take a weakref to the plugin so that we don't create a cycle to the
// RAII guard that will be stored on the plugin.
let plugin = Arc::downgrade(&plugin);
handlers.register(Box::new(move || { handlers.register(Box::new(move || {
// If the plugin is still alive, call its ctrlc handler. It should
// never be None because the guard is dropped when the plugin is.
if let Some(plugin) = plugin.upgrade() {
let _ = plugin.ctrlc(); let _ = plugin.ctrlc();
}
}))? }))?
}; };
plugin.set_ctrlc_handler_guard(guard); plugin.set_ctrlc_handler_guard(guard);