forked from matter-labs/zksync-era
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node_framework): Add a task to handle sigint (matter-labs#1471)
## What ❔ Adds a task that handles SIGINT (aka ctrl+c), propagating the signal to the `ZkStackService` instead of shutting the node down immediately. ## Why ❔ - Graceful shutdown. - Replicating the current behavior. ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zk fmt` and `zk lint`. - [ ] Spellcheck has been run via `zk spellcheck`. - [ ] Linkcheck has been run via `zk linkcheck`.
- Loading branch information
Showing
6 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -920,3 +920,4 @@ oneshot | |
p2p | ||
StorageProcessor | ||
StorageMarker | ||
SIGINT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
core/node/node_framework/src/implementations/layers/sigint.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use tokio::sync::oneshot; | ||
|
||
use crate::{ | ||
service::{ServiceContext, StopReceiver}, | ||
task::UnconstrainedTask, | ||
wiring_layer::{WiringError, WiringLayer}, | ||
}; | ||
|
||
/// Layer that changes the handling of SIGINT signal, preventing an immediate shutdown. | ||
/// Instead, it would propagate the signal to the rest of the node, allowing it to shut down gracefully. | ||
#[derive(Debug)] | ||
pub struct SigintHandlerLayer; | ||
|
||
#[async_trait::async_trait] | ||
impl WiringLayer for SigintHandlerLayer { | ||
fn layer_name(&self) -> &'static str { | ||
"sigint_handler_layer" | ||
} | ||
|
||
async fn wire(self: Box<Self>, mut node: ServiceContext<'_>) -> Result<(), WiringError> { | ||
// SIGINT may happen at any time, so we must handle it as soon as it happens. | ||
node.add_unconstrained_task(Box::new(SigintHandlerTask)); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct SigintHandlerTask; | ||
|
||
#[async_trait::async_trait] | ||
impl UnconstrainedTask for SigintHandlerTask { | ||
fn name(&self) -> &'static str { | ||
"sigint_handler" | ||
} | ||
|
||
async fn run_unconstrained( | ||
self: Box<Self>, | ||
mut stop_receiver: StopReceiver, | ||
) -> anyhow::Result<()> { | ||
let (sigint_sender, sigint_receiver) = oneshot::channel(); | ||
let mut sigint_sender = Some(sigint_sender); // Has to be done this way since `set_handler` requires `FnMut`. | ||
ctrlc::set_handler(move || { | ||
if let Some(sigint_sender) = sigint_sender.take() { | ||
sigint_sender.send(()).ok(); | ||
// ^ The send fails if `sigint_receiver` is dropped. We're OK with this, | ||
// since at this point the node should be stopping anyway, or is not interested | ||
// in listening to interrupt signals. | ||
} | ||
}) | ||
.expect("Error setting Ctrl+C handler"); | ||
|
||
// Wait for either SIGINT or stop signal. | ||
tokio::select! { | ||
_ = sigint_receiver => {}, | ||
_ = stop_receiver.0.changed() => {}, | ||
}; | ||
|
||
Ok(()) | ||
} | ||
} |