mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:06:03 +02:00
feat: make ctrlc available to plugins (#13181)
# Description This PR adds a new method to `EngineInterface`: `register_ctrlc_handler` which takes a closure to run when the plugin's driving engine receives a ctrlc-signal. It also adds a mirror of the `signals` attribute from the main shell `EngineState`. This is an example of how a plugin which makes a long poll http request can end the request on ctrlc: https://github.com/cablehead/nu_plugin_http/blob/main/src/commands/request.rs#L68-L77 To facilitate the feature, a new attribute has been added to `EngineState`: `ctrlc_handlers`. This is a Vec of closures that will be run when the engine's process receives a ctrlc signal. When plugins are added to an `engine_state` during a `merge_delta`, the engine passes the ctrlc_handlers to the plugin's `.configure_ctrlc_handler` method, which gives the plugin a chance to register a handler that sends a ctrlc packet through the `PluginInterface`, if an instance of the plugin is currently running. On the plugin side: `EngineInterface` also has a ctrlc_handlers Vec of closures. Plugin calls can use `register_ctrlc_handler` to register a closure that will be called in the plugin process when the PluginInput::Ctrlc command is received. For future reference these are some alternate places that were investigated for tying the ctrlc trigger to transmitting a Ctrlc packet through the `PluginInterface`: - Directly from `src/signals.rs`: the handler there would need a reference to the Vec<Arc<RegisteredPlugins>>, which would require us to wrap the plugins in a Mutex, which we don't want to do. - have `PersistentPlugin.get_plugin` pass down the engine's CtrlcHandlers to .get and then to .spawn (if the plugin isn't already running). Once we have CtrlcHandlers in spawn, we can register a handler to write directly to PluginInterface. We don't want to double down on passing engine_state to spawn this way though, as it's unpredictable because it would depend on whether the plugin has already been spawned or not. - pass `ctrlc_handlers` to PersistentPlugin::new so it can store it on itself so it's available to spawn. - in `PersistentPlugin.spawn`, create a handler that sends to a clone of the GC event loop's tx. this has the same issues with regards to how to get CtrlcHandlers to the spawn method, and is more complicated than a handler that writes directly to PluginInterface # User-Facing Changes No breaking changes --------- Co-authored-by: Ian Manske <ian.manske@pm.me>
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
//! Interface used by the engine to communicate with the plugin.
|
||||
|
||||
use nu_plugin_core::{
|
||||
util::{with_custom_values_in, Sequence, Waitable, WaitableMut},
|
||||
util::{with_custom_values_in, Waitable, WaitableMut},
|
||||
Interface, InterfaceManager, PipelineDataWriter, PluginRead, PluginWrite, StreamManager,
|
||||
StreamManagerHandle,
|
||||
};
|
||||
@ -11,8 +11,8 @@ use nu_plugin_protocol::{
|
||||
PluginOutput, ProtocolInfo, StreamId, StreamMessage,
|
||||
};
|
||||
use nu_protocol::{
|
||||
ast::Operator, CustomValue, IntoSpanned, PipelineData, PluginMetadata, PluginSignature,
|
||||
ShellError, Signals, Span, Spanned, Value,
|
||||
ast::Operator, engine::Sequence, CustomValue, IntoSpanned, PipelineData, PluginMetadata,
|
||||
PluginSignature, ShellError, Signals, Span, Spanned, Value,
|
||||
};
|
||||
use nu_utils::SharedCow;
|
||||
use std::{
|
||||
@ -664,6 +664,12 @@ impl PluginInterface {
|
||||
self.flush()
|
||||
}
|
||||
|
||||
/// Send the plugin a ctrl-c signal.
|
||||
pub fn ctrlc(&self) -> Result<(), ShellError> {
|
||||
self.write(PluginInput::Ctrlc)?;
|
||||
self.flush()
|
||||
}
|
||||
|
||||
/// Write an [`EngineCallResponse`]. Writes the full stream contained in any [`PipelineData`]
|
||||
/// before returning.
|
||||
pub fn write_engine_call_response(
|
||||
|
@ -6,7 +6,7 @@ use crate::{
|
||||
use super::{PluginInterface, PluginSource};
|
||||
use nu_plugin_core::CommunicationMode;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack},
|
||||
engine::{ctrlc, EngineState, Stack},
|
||||
PluginGcConfig, PluginIdentity, PluginMetadata, RegisteredPlugin, ShellError,
|
||||
};
|
||||
use std::{
|
||||
@ -37,6 +37,8 @@ struct MutableState {
|
||||
preferred_mode: Option<PreferredCommunicationMode>,
|
||||
/// Garbage collector config
|
||||
gc_config: PluginGcConfig,
|
||||
/// RAII guard for this plugin's ctrl-c handler
|
||||
ctrlc_guard: Option<ctrlc::Guard>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@ -64,6 +66,7 @@ impl PersistentPlugin {
|
||||
metadata: None,
|
||||
preferred_mode: None,
|
||||
gc_config,
|
||||
ctrlc_guard: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
@ -299,6 +302,34 @@ impl RegisteredPlugin for PersistentPlugin {
|
||||
fn as_any(self: Arc<Self>) -> Arc<dyn std::any::Any + Send + Sync> {
|
||||
self
|
||||
}
|
||||
|
||||
fn configure_ctrlc_handler(
|
||||
self: Arc<Self>,
|
||||
handlers: &ctrlc::Handlers,
|
||||
) -> Result<(), ShellError> {
|
||||
let guard = {
|
||||
// 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(&self);
|
||||
handlers.register(Box::new(move || {
|
||||
// write a Ctrl-C packet through the PluginInterface if the plugin is alive and
|
||||
// running
|
||||
if let Some(plugin) = plugin.upgrade() {
|
||||
if let Ok(mutable) = plugin.mutable.lock() {
|
||||
if let Some(ref running) = mutable.running {
|
||||
let _ = running.interface.ctrlc();
|
||||
}
|
||||
}
|
||||
}
|
||||
}))?
|
||||
};
|
||||
|
||||
if let Ok(mut mutable) = self.mutable.lock() {
|
||||
mutable.ctrlc_guard = Some(guard);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Anything that can produce a plugin interface.
|
||||
|
Reference in New Issue
Block a user