wip
This commit is contained in:
parent
4157ca711d
commit
354fc28e5d
|
@ -12,7 +12,7 @@ use nu_plugin_protocol::{
|
||||||
};
|
};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Operator, engine::Sequence, CustomValue, IntoSpanned, PipelineData, PluginMetadata,
|
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 nu_utils::SharedCow;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -665,8 +665,8 @@ impl PluginInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send the plugin a ctrl-c signal.
|
/// Send the plugin a ctrl-c signal.
|
||||||
pub fn ctrlc(&self) -> Result<(), ShellError> {
|
pub fn signal(&self, action: SignalAction) -> Result<(), ShellError> {
|
||||||
self.write(PluginInput::Ctrlc)?;
|
self.write(PluginInput::Signal(action))?;
|
||||||
self.flush()
|
self.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,9 @@ use crate::{
|
||||||
use super::{PluginInterface, PluginSource};
|
use super::{PluginInterface, PluginSource};
|
||||||
use nu_plugin_core::CommunicationMode;
|
use nu_plugin_core::CommunicationMode;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{ctrlc, EngineState, Stack},
|
engine::{EngineState, Stack},
|
||||||
PluginGcConfig, PluginIdentity, PluginMetadata, RegisteredPlugin, ShellError,
|
HandlerGuard, Handlers, PluginGcConfig, PluginIdentity, PluginMetadata, RegisteredPlugin,
|
||||||
|
ShellError,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
|
@ -37,8 +38,8 @@ struct MutableState {
|
||||||
preferred_mode: Option<PreferredCommunicationMode>,
|
preferred_mode: Option<PreferredCommunicationMode>,
|
||||||
/// Garbage collector config
|
/// Garbage collector config
|
||||||
gc_config: PluginGcConfig,
|
gc_config: PluginGcConfig,
|
||||||
/// RAII guard for this plugin's ctrl-c handler
|
/// RAII guard for this plugin's signal handler
|
||||||
ctrlc_guard: Option<ctrlc::Guard>,
|
signal_guard: Option<HandlerGuard>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
@ -66,7 +67,7 @@ impl PersistentPlugin {
|
||||||
metadata: None,
|
metadata: None,
|
||||||
preferred_mode: None,
|
preferred_mode: None,
|
||||||
gc_config,
|
gc_config,
|
||||||
ctrlc_guard: None,
|
signal_guard: None,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -303,21 +304,18 @@ impl RegisteredPlugin for PersistentPlugin {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn configure_ctrlc_handler(
|
fn configure_signal_handler(self: Arc<Self>, handlers: &Handlers) -> Result<(), ShellError> {
|
||||||
self: Arc<Self>,
|
|
||||||
handlers: &ctrlc::Handlers,
|
|
||||||
) -> Result<(), ShellError> {
|
|
||||||
let guard = {
|
let guard = {
|
||||||
// We take a weakref to the plugin so that we don't create a cycle to the
|
// 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.
|
// RAII guard that will be stored on the plugin.
|
||||||
let plugin = Arc::downgrade(&self);
|
let plugin = Arc::downgrade(&self);
|
||||||
handlers.register(Box::new(move || {
|
handlers.register(Box::new(move |action| {
|
||||||
// write a Ctrl-C packet through the PluginInterface if the plugin is alive and
|
// write a Ctrl-C packet through the PluginInterface if the plugin is alive and
|
||||||
// running
|
// running
|
||||||
if let Some(plugin) = plugin.upgrade() {
|
if let Some(plugin) = plugin.upgrade() {
|
||||||
if let Ok(mutable) = plugin.mutable.lock() {
|
if let Ok(mutable) = plugin.mutable.lock() {
|
||||||
if let Some(ref running) = mutable.running {
|
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() {
|
if let Ok(mut mutable) = self.mutable.lock() {
|
||||||
mutable.ctrlc_guard = Some(guard);
|
mutable.signal_guard = Some(guard);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -23,7 +23,7 @@ pub mod test_util;
|
||||||
|
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Operator, engine::Closure, ByteStreamType, Config, DeclId, LabeledError, PipelineData,
|
ast::Operator, engine::Closure, ByteStreamType, Config, DeclId, LabeledError, PipelineData,
|
||||||
PluginMetadata, PluginSignature, ShellError, Span, Spanned, Value,
|
PluginMetadata, PluginSignature, ShellError, SignalAction, Span, Spanned, Value,
|
||||||
};
|
};
|
||||||
use nu_utils::SharedCow;
|
use nu_utils::SharedCow;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -208,8 +208,8 @@ pub enum PluginInput {
|
||||||
Drop(StreamId),
|
Drop(StreamId),
|
||||||
/// See [`StreamMessage::Ack`].
|
/// See [`StreamMessage::Ack`].
|
||||||
Ack(StreamId),
|
Ack(StreamId),
|
||||||
/// Signal a ctrlc event
|
/// Relay signals to the plugin
|
||||||
Ctrlc,
|
Signal(SignalAction),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<PluginInput> for StreamMessage {
|
impl TryFrom<PluginInput> for StreamMessage {
|
||||||
|
|
|
@ -2,14 +2,14 @@ use crate::{
|
||||||
ast::Block,
|
ast::Block,
|
||||||
debugger::{Debugger, NoopDebugger},
|
debugger::{Debugger, NoopDebugger},
|
||||||
engine::{
|
engine::{
|
||||||
ctrlc,
|
|
||||||
usage::{build_usage, Usage},
|
usage::{build_usage, Usage},
|
||||||
CachedFile, Command, CommandType, EnvVars, OverlayFrame, ScopeFrame, Stack, StateDelta,
|
CachedFile, Command, CommandType, EnvVars, OverlayFrame, ScopeFrame, Stack, StateDelta,
|
||||||
Variable, Visibility, DEFAULT_OVERLAY_NAME,
|
Variable, Visibility, DEFAULT_OVERLAY_NAME,
|
||||||
},
|
},
|
||||||
eval_const::create_nu_constant,
|
eval_const::create_nu_constant,
|
||||||
BlockId, Category, Config, DeclId, FileId, GetSpan, HistoryConfig, Module, ModuleId, OverlayId,
|
BlockId, Category, Config, DeclId, FileId, GetSpan, Handlers, HistoryConfig, Module, ModuleId,
|
||||||
ShellError, Signals, Signature, Span, SpanId, Type, Value, VarId, VirtualPathId,
|
OverlayId, ShellError, SignalAction, Signals, Signature, Span, SpanId, Type, Value, VarId,
|
||||||
|
VirtualPathId,
|
||||||
};
|
};
|
||||||
use fancy_regex::Regex;
|
use fancy_regex::Regex;
|
||||||
use lru::LruCache;
|
use lru::LruCache;
|
||||||
|
@ -86,8 +86,8 @@ pub struct EngineState {
|
||||||
pub spans: Vec<Span>,
|
pub spans: Vec<Span>,
|
||||||
usage: Usage,
|
usage: Usage,
|
||||||
pub scope: ScopeFrame,
|
pub scope: ScopeFrame,
|
||||||
pub ctrlc_handlers: Option<ctrlc::Handlers>,
|
|
||||||
signals: Signals,
|
signals: Signals,
|
||||||
|
pub signal_handlers: Option<Handlers>,
|
||||||
pub env_vars: Arc<EnvVars>,
|
pub env_vars: Arc<EnvVars>,
|
||||||
pub previous_env_vars: Arc<HashMap<String, Value>>,
|
pub previous_env_vars: Arc<HashMap<String, Value>>,
|
||||||
pub config: Arc<Config>,
|
pub config: Arc<Config>,
|
||||||
|
@ -147,7 +147,7 @@ impl EngineState {
|
||||||
0,
|
0,
|
||||||
false,
|
false,
|
||||||
),
|
),
|
||||||
ctrlc_handlers: None,
|
signal_handlers: None,
|
||||||
signals: Signals::empty(),
|
signals: Signals::empty(),
|
||||||
env_vars: Arc::new(
|
env_vars: Arc::new(
|
||||||
[(DEFAULT_OVERLAY_NAME.to_string(), HashMap::new())]
|
[(DEFAULT_OVERLAY_NAME.to_string(), HashMap::new())]
|
||||||
|
@ -186,7 +186,10 @@ impl EngineState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset_signals(&mut self) {
|
pub fn reset_signals(&mut self) {
|
||||||
self.signals.reset()
|
self.signals.reset();
|
||||||
|
if let Some(ref handlers) = self.signal_handlers {
|
||||||
|
handlers.run(SignalAction::Reset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_signals(&mut self, signals: Signals) {
|
pub fn set_signals(&mut self, signals: Signals) {
|
||||||
|
@ -272,9 +275,9 @@ impl EngineState {
|
||||||
#[cfg(feature = "plugin")]
|
#[cfg(feature = "plugin")]
|
||||||
if !delta.plugins.is_empty() {
|
if !delta.plugins.is_empty() {
|
||||||
for plugin in std::mem::take(&mut delta.plugins) {
|
for plugin in std::mem::take(&mut delta.plugins) {
|
||||||
// Connect plugins to the ctrlc handlers
|
// Connect plugins to the signal handlers
|
||||||
if let Some(handlers) = &self.ctrlc_handlers {
|
if let Some(handlers) = &self.signal_handlers {
|
||||||
plugin.clone().configure_ctrlc_handler(handlers)?;
|
plugin.clone().configure_signal_handler(handlers)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace plugins that overlap in identity.
|
// Replace plugins that overlap in identity.
|
||||||
|
|
|
@ -34,5 +34,3 @@ pub use stack_out_dest::*;
|
||||||
pub use state_delta::*;
|
pub use state_delta::*;
|
||||||
pub use state_working_set::*;
|
pub use state_working_set::*;
|
||||||
pub use variable::*;
|
pub use variable::*;
|
||||||
|
|
||||||
pub mod ctrlc;
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use crate::{engine::Sequence, ShellError};
|
use crate::{engine::Sequence, ShellError, SignalAction};
|
||||||
|
|
||||||
/// Handler is a closure that can be sent across threads and shared.
|
/// Handler is a closure that can be sent across threads and shared.
|
||||||
pub type Handler = Box<dyn Fn() + Send + Sync>;
|
pub type Handler = Box<dyn Fn(SignalAction) + Send + Sync>;
|
||||||
|
|
||||||
/// Manages a collection of handlers.
|
/// Manages a collection of handlers.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -23,16 +23,16 @@ impl Debug for Handlers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Guard that unregisters a handler when dropped.
|
/// HandlerGuard that unregisters a handler when dropped.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Guard {
|
pub struct HandlerGuard {
|
||||||
/// Unique ID of the handler.
|
/// Unique ID of the handler.
|
||||||
id: usize,
|
id: usize,
|
||||||
/// Reference to the handlers list.
|
/// Reference to the handlers list.
|
||||||
handlers: Arc<Mutex<Vec<(usize, Handler)>>>,
|
handlers: Arc<Mutex<Vec<(usize, Handler)>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Guard {
|
impl Drop for HandlerGuard {
|
||||||
/// Drops the `Guard`, removing the associated handler from the list.
|
/// Drops the `Guard`, removing the associated handler from the list.
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Ok(mut handlers) = self.handlers.lock() {
|
if let Ok(mut handlers) = self.handlers.lock() {
|
||||||
|
@ -41,7 +41,7 @@ impl Drop for Guard {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for Guard {
|
impl Debug for HandlerGuard {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_struct("Guard").field("id", &self.id).finish()
|
f.debug_struct("Guard").field("id", &self.id).finish()
|
||||||
}
|
}
|
||||||
|
@ -56,23 +56,23 @@ impl Handlers {
|
||||||
|
|
||||||
/// Registers a new handler and returns an RAII guard which will unregister the handler when
|
/// Registers a new handler and returns an RAII guard which will unregister the handler when
|
||||||
/// dropped.
|
/// dropped.
|
||||||
pub fn register(&self, handler: Handler) -> Result<Guard, ShellError> {
|
pub fn register(&self, handler: Handler) -> Result<HandlerGuard, ShellError> {
|
||||||
let id = self.next_id.next()?;
|
let id = self.next_id.next()?;
|
||||||
if let Ok(mut handlers) = self.handlers.lock() {
|
if let Ok(mut handlers) = self.handlers.lock() {
|
||||||
handlers.push((id, handler));
|
handlers.push((id, handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Guard {
|
Ok(HandlerGuard {
|
||||||
id,
|
id,
|
||||||
handlers: Arc::clone(&self.handlers),
|
handlers: Arc::clone(&self.handlers),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs all registered handlers.
|
/// Runs all registered handlers.
|
||||||
pub fn run(&self) {
|
pub fn run(&self, action: SignalAction) {
|
||||||
if let Ok(handlers) = self.handlers.lock() {
|
if let Ok(handlers) = self.handlers.lock() {
|
||||||
for (_, handler) in handlers.iter() {
|
for (_, handler) in handlers.iter() {
|
||||||
handler();
|
handler(action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod byte_stream;
|
pub mod byte_stream;
|
||||||
|
mod handlers;
|
||||||
pub mod list_stream;
|
pub mod list_stream;
|
||||||
mod metadata;
|
mod metadata;
|
||||||
mod out_dest;
|
mod out_dest;
|
||||||
|
@ -6,6 +7,7 @@ mod pipeline_data;
|
||||||
mod signals;
|
mod signals;
|
||||||
|
|
||||||
pub use byte_stream::*;
|
pub use byte_stream::*;
|
||||||
|
pub use handlers::*;
|
||||||
pub use list_stream::*;
|
pub use list_stream::*;
|
||||||
pub use metadata::*;
|
pub use metadata::*;
|
||||||
pub use out_dest::*;
|
pub use out_dest::*;
|
||||||
|
|
|
@ -4,6 +4,8 @@ use std::sync::{
|
||||||
Arc,
|
Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// Used to check for signals to suspend or terminate the execution of Nushell code.
|
/// Used to check for signals to suspend or terminate the execution of Nushell code.
|
||||||
///
|
///
|
||||||
/// For now, this struct only supports interruption (ctrl+c or SIGINT).
|
/// For now, this struct only supports interruption (ctrl+c or SIGINT).
|
||||||
|
@ -81,3 +83,11 @@ impl Signals {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The types of things that can be signaled. Its anticipated this will change as we learn how we'd
|
||||||
|
/// like signals to be handled.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub enum SignalAction {
|
||||||
|
Interrupt,
|
||||||
|
Reset,
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{any::Any, sync::Arc};
|
use std::{any::Any, sync::Arc};
|
||||||
|
|
||||||
use crate::{engine::ctrlc, PluginGcConfig, PluginIdentity, PluginMetadata, ShellError};
|
use crate::{Handlers, 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 {
|
||||||
|
@ -36,10 +36,7 @@ pub trait RegisteredPlugin: Send + Sync {
|
||||||
fn as_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
|
fn as_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
|
||||||
|
|
||||||
/// Give this plugin a chance to register for Ctrl-C signals.
|
/// Give this plugin a chance to register for Ctrl-C signals.
|
||||||
fn configure_ctrlc_handler(
|
fn configure_signal_handler(self: Arc<Self>, _handler: &Handlers) -> Result<(), ShellError> {
|
||||||
self: Arc<Self>,
|
|
||||||
_handler: &ctrlc::Handlers,
|
|
||||||
) -> Result<(), ShellError> {
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
use nu_protocol::{
|
use nu_protocol::{engine::EngineState, Handlers, SignalAction, Signals};
|
||||||
engine::{ctrlc::Handlers, EngineState},
|
|
||||||
Signals,
|
|
||||||
};
|
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc,
|
Arc,
|
||||||
|
@ -11,12 +8,12 @@ pub(crate) fn ctrlc_protection(engine_state: &mut EngineState) {
|
||||||
let interrupt = Arc::new(AtomicBool::new(false));
|
let interrupt = Arc::new(AtomicBool::new(false));
|
||||||
engine_state.set_signals(Signals::new(interrupt.clone()));
|
engine_state.set_signals(Signals::new(interrupt.clone()));
|
||||||
|
|
||||||
let ctrlc_handlers = Handlers::new();
|
let signal_handlers = Handlers::new();
|
||||||
engine_state.ctrlc_handlers = Some(ctrlc_handlers.clone());
|
engine_state.signal_handlers = Some(signal_handlers.clone());
|
||||||
|
|
||||||
ctrlc::set_handler(move || {
|
ctrlc::set_handler(move || {
|
||||||
interrupt.store(true, Ordering::Relaxed);
|
interrupt.store(true, Ordering::Relaxed);
|
||||||
ctrlc_handlers.run();
|
signal_handlers.run(SignalAction::Interrupt);
|
||||||
})
|
})
|
||||||
.expect("Error setting Ctrl-C handler");
|
.expect("Error setting Ctrl-C handler");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user