mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 04:38:15 +02:00
fix: relay Signals reset to plugins (#13510)
This PR will close #13501 # Description This PR expands on [the relay of signals to running plugin processes](https://github.com/nushell/nushell/pull/13181). The Ctrlc relay has been generalized to SignalAction::Interrupt and when reset_signal is called on the main EngineState, a SignalAction::Reset is now relayed to running plugins. # User-Facing Changes The signal handler closure now takes a `signals::SignalAction`, while previously it took no arguments. The handler will now be called on both interrupt and reset. The method to register a handler on the plugin side is now called `register_signal_handler` instead of `register_ctrlc_handler` [example](https://github.com/nushell/nushell/pull/13510/files#diff-3e04dff88fd0780a49778a3d1eede092ec729a1264b4ef07ca0d2baa859dad05L38). This will only affect plugin authors who have started making use of https://github.com/nushell/nushell/pull/13181, which isn't currently part of an official release. The change will also require all of user's plugins to be recompiled in order that they don't error when a signal is received on the PluginInterface. # Testing ``` : example ctrlc interrupt status: false waiting for interrupt signal... ^Cinterrupt status: true peace. Error: × Operation interrupted ╭─[display_output hook:1:1] 1 │ if (term size).columns >= 100 { table -e } else { table } · ─┬ · ╰── This operation was interrupted ╰──── : example ctrlc interrupt status: false <-- NOTE status is false waiting for interrupt signal... ^Cinterrupt status: true peace. Error: × Operation interrupted ╭─[display_output hook:1:1] 1 │ if (term size).columns >= 100 { table -e } else { table } · ─┬ · ╰── This operation was interrupted ╰──── ```
This commit is contained in:
@ -12,7 +12,7 @@ use nu_plugin_protocol::{
|
||||
};
|
||||
use nu_protocol::{
|
||||
ast::Operator, engine::Sequence, CustomValue, IntoSpanned, PipelineData, PluginMetadata,
|
||||
PluginSignature, ShellError, Signals, Span, Spanned, Value,
|
||||
PluginSignature, ShellError, SignalAction, Signals, Span, Spanned, Value,
|
||||
};
|
||||
use nu_utils::SharedCow;
|
||||
use std::{
|
||||
@ -664,9 +664,9 @@ impl PluginInterface {
|
||||
self.flush()
|
||||
}
|
||||
|
||||
/// Send the plugin a ctrl-c signal.
|
||||
pub fn ctrlc(&self) -> Result<(), ShellError> {
|
||||
self.write(PluginInput::Ctrlc)?;
|
||||
/// Send the plugin a signal.
|
||||
pub fn signal(&self, action: SignalAction) -> Result<(), ShellError> {
|
||||
self.write(PluginInput::Signal(action))?;
|
||||
self.flush()
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,9 @@ use crate::{
|
||||
use super::{PluginInterface, PluginSource};
|
||||
use nu_plugin_core::CommunicationMode;
|
||||
use nu_protocol::{
|
||||
engine::{ctrlc, EngineState, Stack},
|
||||
PluginGcConfig, PluginIdentity, PluginMetadata, RegisteredPlugin, ShellError,
|
||||
engine::{EngineState, Stack},
|
||||
HandlerGuard, Handlers, PluginGcConfig, PluginIdentity, PluginMetadata, RegisteredPlugin,
|
||||
ShellError,
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
@ -37,8 +38,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>,
|
||||
/// RAII guard for this plugin's signal handler
|
||||
signal_guard: Option<HandlerGuard>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@ -66,7 +67,7 @@ impl PersistentPlugin {
|
||||
metadata: None,
|
||||
preferred_mode: None,
|
||||
gc_config,
|
||||
ctrlc_guard: None,
|
||||
signal_guard: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
@ -303,21 +304,18 @@ impl RegisteredPlugin for PersistentPlugin {
|
||||
self
|
||||
}
|
||||
|
||||
fn configure_ctrlc_handler(
|
||||
self: Arc<Self>,
|
||||
handlers: &ctrlc::Handlers,
|
||||
) -> Result<(), ShellError> {
|
||||
fn configure_signal_handler(self: Arc<Self>, handlers: &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
|
||||
handlers.register(Box::new(move |action| {
|
||||
// write a signal 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();
|
||||
let _ = running.interface.signal(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -325,7 +323,7 @@ impl RegisteredPlugin for PersistentPlugin {
|
||||
};
|
||||
|
||||
if let Ok(mut mutable) = self.mutable.lock() {
|
||||
mutable.ctrlc_guard = Some(guard);
|
||||
mutable.signal_guard = Some(guard);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
Reference in New Issue
Block a user