nushell/src/signals.rs

20 lines
623 B
Rust
Raw Normal View History

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 ╰──── ```
2024-08-06 12:35:40 +02:00
use nu_protocol::{engine::EngineState, Handlers, SignalAction, Signals};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
pub(crate) fn ctrlc_protection(engine_state: &mut EngineState) {
let interrupt = Arc::new(AtomicBool::new(false));
engine_state.set_signals(Signals::new(interrupt.clone()));
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>
2024-07-30 15:29:18 +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 ╰──── ```
2024-08-06 12:35:40 +02:00
let signal_handlers = Handlers::new();
engine_state.signal_handlers = Some(signal_handlers.clone());
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>
2024-07-30 15:29:18 +02:00
ctrlc::set_handler(move || {
interrupt.store(true, Ordering::Relaxed);
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 ╰──── ```
2024-08-06 12:35:40 +02:00
signal_handlers.run(SignalAction::Interrupt);
})
.expect("Error setting Ctrl-C handler");
}