reversing course

This commit is contained in:
Andy Gayton 2024-07-04 19:09:19 -04:00
parent b9d1dfae8e
commit fc1fb5538d
5 changed files with 22 additions and 36 deletions

View File

@ -109,15 +109,13 @@ apparent the next time `nu` is next launched with that plugin registry file.
let custom_path = call.get_flag(engine_state, stack, "plugin-config")?; let custom_path = call.get_flag(engine_state, stack, "plugin-config")?;
// Start the plugin manually, to get the freshest signatures and to not affect engine // Start the plugin manually, to get the freshest signatures and to not affect engine
// state. Provide a GC config that will stop it ASAP. We don't pass ctrlc_handlers since // state. Provide a GC config that will stop it ASAP.
// this instance is temporary and won't be used to run commands.
let plugin = Arc::new(PersistentPlugin::new( let plugin = Arc::new(PersistentPlugin::new(
identity, identity,
PluginGcConfig { PluginGcConfig {
enabled: true, enabled: true,
stop_after: 0, stop_after: 0,
}, },
None,
)); ));
let interface = plugin.clone().get_plugin(Some((engine_state, stack)))?; let interface = plugin.clone().get_plugin(Some((engine_state, stack)))?;
let metadata = interface.get_metadata()?; let metadata = interface.get_metadata()?;

View File

@ -295,11 +295,7 @@ pub fn add_plugin_to_working_set(
// Add it to / get it from the working set // Add it to / get it from the working set
let plugin = working_set.find_or_create_plugin(identity, || { let plugin = working_set.find_or_create_plugin(identity, || {
Arc::new(PersistentPlugin::new( Arc::new(PersistentPlugin::new(identity.clone(), gc_config.clone()))
identity.clone(),
gc_config.clone(),
working_set.permanent_state.ctrlc_handlers.clone(),
))
}); });
plugin.set_gc_config(&gc_config); plugin.set_gc_config(&gc_config);

View File

@ -37,8 +37,8 @@ struct MutableState {
preferred_mode: Option<PreferredCommunicationMode>, preferred_mode: Option<PreferredCommunicationMode>,
/// Garbage collector config /// Garbage collector config
gc_config: PluginGcConfig, gc_config: PluginGcConfig,
/// Pool of ctrl-c handlers to subscribe to /// RAII guard for this plugin's ctrl-c handler
ctrlc_handlers: Option<ctrlc::Handlers>, ctrlc_guard: Option<ctrlc::Guard>,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -54,17 +54,11 @@ struct RunningPlugin {
interface: PluginInterface, interface: PluginInterface,
/// Garbage collector for the plugin /// Garbage collector for the plugin
gc: PluginGc, gc: PluginGc,
/// RAII guard for this plugin's ctrl-c handler
_ctrlc_guard: Option<ctrlc::Guard>,
} }
impl PersistentPlugin { impl PersistentPlugin {
/// Create a new persistent plugin. The plugin will not be spawned immediately. /// Create a new persistent plugin. The plugin will not be spawned immediately.
pub fn new( pub fn new(identity: PluginIdentity, gc_config: PluginGcConfig) -> PersistentPlugin {
identity: PluginIdentity,
gc_config: PluginGcConfig,
ctrlc_handlers: Option<ctrlc::Handlers>,
) -> PersistentPlugin {
PersistentPlugin { PersistentPlugin {
identity, identity,
mutable: Mutex::new(MutableState { mutable: Mutex::new(MutableState {
@ -72,7 +66,7 @@ impl PersistentPlugin {
metadata: None, metadata: None,
preferred_mode: None, preferred_mode: None,
gc_config, gc_config,
ctrlc_handlers, ctrlc_guard: None,
}), }),
} }
} }
@ -220,22 +214,7 @@ impl PersistentPlugin {
return self.spawn(envs, mutable); return self.spawn(envs, mutable);
} }
let guard = mutable mutable.running = Some(RunningPlugin { interface, gc });
.ctrlc_handlers
.as_mut()
.map(|ctrlc_handlers| {
let interface = interface.clone();
ctrlc_handlers.register(Box::new(move || {
let _ = interface.ctrlc();
}))
})
.transpose()?;
mutable.running = Some(RunningPlugin {
interface,
gc,
_ctrlc_guard: guard.clone(),
});
Ok(()) Ok(())
} }
@ -320,6 +299,12 @@ impl RegisteredPlugin for PersistentPlugin {
} }
} }
fn set_ctrlc_handler_guard(&self, guard: ctrlc::Guard) {
if let Ok(mut mutable) = self.mutable.lock() {
mutable.ctrlc_guard = Some(guard);
}
}
fn as_any(self: Arc<Self>) -> Arc<dyn std::any::Any + Send + Sync> { fn as_any(self: Arc<Self>) -> Arc<dyn std::any::Any + Send + Sync> {
self self
} }

View File

@ -5,7 +5,7 @@ use std::{
use nu_plugin_engine::{GetPlugin, PluginInterface}; use nu_plugin_engine::{GetPlugin, PluginInterface};
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, engine::{ctrlc, EngineState, Stack},
PluginGcConfig, PluginIdentity, PluginMetadata, RegisteredPlugin, ShellError, PluginGcConfig, PluginIdentity, PluginMetadata, RegisteredPlugin, ShellError,
}; };
@ -52,6 +52,10 @@ impl RegisteredPlugin for FakePersistentPlugin {
// We don't have a GC // We don't have a GC
} }
fn set_ctrlc_handler_guard(&self, _guard: ctrlc::Guard) {
// We don't have a Ctrl-C handler
}
fn stop(&self) -> Result<(), ShellError> { fn stop(&self) -> Result<(), ShellError> {
// We can't stop // We can't stop
Ok(()) Ok(())

View File

@ -1,6 +1,6 @@
use std::{any::Any, sync::Arc}; use std::{any::Any, sync::Arc};
use crate::{PluginGcConfig, PluginIdentity, PluginMetadata, ShellError}; use crate::{engine::ctrlc, PluginGcConfig, PluginIdentity, PluginMetadata, ShellError};
/// Trait for plugins registered in the [`EngineState`](crate::engine::EngineState). /// Trait for plugins registered in the [`EngineState`](crate::engine::EngineState).
pub trait RegisteredPlugin: Send + Sync { pub trait RegisteredPlugin: Send + Sync {
@ -22,6 +22,9 @@ pub trait RegisteredPlugin: Send + Sync {
/// Set garbage collection config for the plugin. /// Set garbage collection config for the plugin.
fn set_gc_config(&self, gc_config: &PluginGcConfig); fn set_gc_config(&self, gc_config: &PluginGcConfig);
/// Set a reference to the raii guard for the plugin's Ctrl-C handler.
fn set_ctrlc_handler_guard(&self, guard: ctrlc::Guard);
/// Stop the plugin. /// Stop the plugin.
fn stop(&self) -> Result<(), ShellError>; fn stop(&self) -> Result<(), ShellError>;