Skip to content

Commit 30cdd54

Browse files
authored
docs: add details on signal handling to the plugin protocol reference (#1619)
1 parent 7a94f22 commit 30cdd54

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

contributor-book/plugin_protocol_reference.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ The plugin **may** send [engine calls](#enginecall) during the execution of a ca
3333

3434
The engine **may** send a [`Goodbye`](#goodbye) message to the plugin indicating that it will no longer send any more plugin calls. Upon receiving this message, the plugin **may** choose not to accept any more plugin calls, and **should** exit after any in-progress plugin calls have finished.
3535

36+
**Note**: During the sequence, the engine may also send a [`Signal`](#signal) message asynchronously, such as when an interrupt (Ctrl+C) or reset signal is triggered. Plugins should handle these messages as they are received, for example, by pausing or stopping operations when an `Interrupt` signal is sent.
37+
3638
## `Hello`
3739

3840
After the encoding type has been decided, both the engine and plugin **must** send a `Hello` message containing relevant version and protocol support information.
@@ -557,6 +559,21 @@ Example:
557559
}
558560
```
559561

562+
### `Signal`
563+
564+
The `Signal` message type is used to relay a signal from the engine to the plugin, allowing the plugin to respond to various system-level or user-initiated signals. The message body consists of a `SignalAction` enum, which currently supports the following variants:
565+
566+
- **`Interrupt`**: Sent when the engine receives an interrupt signal (such as Ctrl+C), to gracefully interrupt a plugin's operation.
567+
- **`Reset`**: Sent when the engine’s `reset_signals` method is called, indicating that the plugin should reset its signal state.
568+
569+
Example:
570+
571+
```json
572+
{
573+
"Signal": "Interrupt"
574+
}
575+
```
576+
560577
### `Goodbye`
561578

562579
Indicate that no further plugin calls are expected, and that the plugin **should** exit as soon as it is finished processing any in-progress plugin calls.
@@ -1228,6 +1245,42 @@ Example:
12281245
}
12291246
```
12301247

1248+
## Signal Handling in Plugins
1249+
1250+
Plugins can respond to signals sent from the engine, such as interrupts (Ctrl+C) or resets, by registering handlers. The plugin’s signal handling methods allow for customizable responses to user or system actions, enhancing the plugin's integration with the Nu engine.
1251+
1252+
### `register_signal_handler`
1253+
1254+
The `register_signal_handler` method allows a plugin to register a handler that will be called when a signal is received. This method accepts a closure with the following signature:
1255+
```rust
1256+
|action: SignalAction| { ... }
1257+
```
1258+
The closure will be invoked with the `SignalAction` variant received from the engine. This method returns an RAII guard that ensures the handler remains active until it is dropped.
1259+
1260+
#### Example Usage
1261+
1262+
Below is an example of registering a handler that responds to both `Interrupt` and `Reset` signals:
1263+
1264+
```rust
1265+
let _guard = engine.register_signal_handler(Box::new(move |action| {
1266+
match action {
1267+
SignalAction::Interrupt => println!("Interrupt signal received"),
1268+
SignalAction::Reset => println!("Reset signal received"),
1269+
}
1270+
}));
1271+
```
1272+
1273+
#### `signals()`
1274+
1275+
The `signals()` method allows the plugin to check the status of the signal, specifically for `Interrupt`. This method returns a `Signals` struct, which includes the method `interrupted()` that indicates if an interrupt has occurred.
1276+
1277+
```rust
1278+
if engine.signals().interrupted() {
1279+
println!("Operation was interrupted.");
1280+
}
1281+
```
1282+
Use `signals().interrupted()` to check for interrupt status, particularly when managing long-running operations.
1283+
12311284
## Encoding
12321285

12331286
### JSON
@@ -2271,3 +2324,14 @@ Serialized with serde's default enum representation. Examples:
22712324
{ "Bits": "BitOr" } // | Bits(BitOr)
22722325
{ "Comparison": "RegexMatch" } // =~ Comparison(RegexMatch)
22732326
```
2327+
2328+
### `SignalAction`
2329+
2330+
The `SignalAction` enum is used to specify actions that the plugin should take in response to signals from the engine.
2331+
2332+
| Variant | Description |
2333+
| ------------- | -------------------------------------------------------------------------------------- |
2334+
| `Interrupt` | Indicates an interrupt signal (e.g., Ctrl+C) was received. Plugins should pause, stop, or end their operation. |
2335+
| `Reset` | Indicates a reset signal from the engine’s `reset_signals` function. Plugins should reset any internal signal states. |
2336+
2337+
This enum can be used in conjunction with `register_signal_handler` to perform specific tasks when each signal type is received.

0 commit comments

Comments
 (0)