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:
Andy Gayton
2024-08-06 06:35:40 -04:00
committed by GitHub
parent 73e8de9753
commit 1cd0544a3f
12 changed files with 86 additions and 79 deletions

View File

@ -11,9 +11,9 @@ use nu_plugin_protocol::{
PluginOutput, ProtocolInfo,
};
use nu_protocol::{
engine::{ctrlc, Closure, Sequence},
Config, DeclId, LabeledError, PipelineData, PluginMetadata, PluginSignature, ShellError,
Signals, Span, Spanned, Value,
engine::{Closure, Sequence},
Config, DeclId, Handler, HandlerGuard, Handlers, LabeledError, PipelineData, PluginMetadata,
PluginSignature, ShellError, SignalAction, Signals, Span, Spanned, Value,
};
use nu_utils::SharedCow;
use std::{
@ -64,10 +64,11 @@ struct EngineInterfaceState {
mpsc::Sender<(EngineCallId, mpsc::Sender<EngineCallResponse<PipelineData>>)>,
/// The synchronized output writer
writer: Box<dyn PluginWrite<PluginOutput>>,
// Mirror signals from `EngineState`
/// Mirror signals from `EngineState`. You can make use of this with
/// `engine_interface.signals()` when constructing a Stream that requires signals
signals: Signals,
/// Registered Ctrl-C handlers
ctrlc_handlers: ctrlc::Handlers,
/// Registered signal handlers
signal_handlers: Handlers,
}
impl std::fmt::Debug for EngineInterfaceState {
@ -122,7 +123,7 @@ impl EngineInterfaceManager {
engine_call_subscription_sender: subscription_tx,
writer: Box::new(writer),
signals: Signals::new(Arc::new(AtomicBool::new(false))),
ctrlc_handlers: ctrlc::Handlers::new(),
signal_handlers: Handlers::new(),
}),
protocol_info_mut,
plugin_call_sender: Some(plug_tx),
@ -337,9 +338,12 @@ impl InterfaceManager for EngineInterfaceManager {
});
self.send_engine_call_response(id, response)
}
PluginInput::Ctrlc => {
self.state.signals.trigger();
self.state.ctrlc_handlers.run();
PluginInput::Signal(action) => {
match action {
SignalAction::Interrupt => self.state.signals.trigger(),
SignalAction::Reset => self.state.signals.reset(),
}
self.state.signal_handlers.run(action);
Ok(())
}
}
@ -521,13 +525,10 @@ impl EngineInterface {
self.state.writer.is_stdout()
}
/// Register a closure which will be called when the engine receives a Ctrl-C signal. Returns a
/// RAII guard that will keep the closure alive until it is dropped.
pub fn register_ctrlc_handler(
&self,
handler: ctrlc::Handler,
) -> Result<ctrlc::Guard, ShellError> {
self.state.ctrlc_handlers.register(handler)
/// Register a closure which will be called when the engine receives an interrupt signal.
/// Returns a RAII guard that will keep the closure alive until it is dropped.
pub fn register_signal_handler(&self, handler: Handler) -> Result<HandlerGuard, ShellError> {
self.state.signal_handlers.register(handler)
}
/// Get the full shell configuration from the engine. As this is quite a large object, it is