[Feature] Improve signal and shutdown handling#3915
Merged
Conversation
fe9131c to
6b4fda4
Compare
6b4fda4 to
aec7b2a
Compare
eee21b7 to
d2a5661
Compare
3f2cbb7 to
369c075
Compare
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 21, 2025
ljedrz
reviewed
Nov 25, 2025
ljedrz
reviewed
Nov 25, 2025
ljedrz
reviewed
Nov 25, 2025
ljedrz
reviewed
Nov 25, 2025
ljedrz
reviewed
Nov 25, 2025
Collaborator
ljedrz
left a comment
There was a problem hiding this comment.
Just one small change left, and it's good to go 👍.
cbf4d86 to
1ff28fd
Compare
1ff28fd to
aa6ddfc
Compare
ljedrz
reviewed
Dec 2, 2025
ljedrz
reviewed
Dec 2, 2025
ljedrz
reviewed
Dec 2, 2025
ljedrz
reviewed
Dec 2, 2025
ljedrz
previously approved these changes
Dec 2, 2025
Collaborator
ljedrz
left a comment
There was a problem hiding this comment.
Left a few final nits, but basically LGTM 👌.
ljedrz
approved these changes
Dec 2, 2025
vicsn
reviewed
Dec 2, 2025
Collaborator
vicsn
left a comment
There was a problem hiding this comment.
Can you note in the PR README which manual tests were run? Are they comprehensive?
Contributor
Author
Done! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
These changes are part of #3874, but were moved to their own PR to reduce the size of the latter.
Motivation
Before, #4021, snarkOS nodes would call
std::process::exitbefore shutting down. This was problematic "because this function never returns [and] terminates the process, no destructors on the current stack or any other thread’s stack will be run" (source).The code worked around this by sleeping for multiple seconds during shutdown. Such a sleep may be too long in a test setting and too short in production. Additionally, the lack of a clean shutdown makes it more complicated to perform certain tasks at shutdown, such as caching the block tree on disk or flushing the log file.
#4021, as far as I understand, was intended to be a quick fix until this slightly cleaner version is merged. This PR improves on the former by encapsulating all signal handling logic in a dedicated module and avoids spawning the main node logic in a detached task, which makes catching panics easier.
For
snarkos-displayshutting down usingEscapewill now work the same as with Ctrl+C.Proposed Changes
In the current design, the code passes around an
AtomicBoolas a shutdown flag. This design generally works, but does not allow waiting for the flag to be set. This PR introduces aSignalHandlerstruct and aStoppabletrait.The
Stoppabletrait serves the same function as theAtomicBooldid before, but it is a little clearer what its purpose is from its struct and function names alone.The
SignalHandlerstruct implements theStoppabletrait and also launches a background task that wait for Ctrl+C.Testing
To test, I verified that shut down works correctly in multiple scenarios. "correctly" means that the process exists cleanly, and that the on-disk state is not corrupted. The latter was verified by starting the node again and ensuring it can load the stored state fine.
I went through the following scenarios
Additionally, I ran a development network and verified that nodes all nodes shut down cleanly without getting stuck or returning nonzero exit codes.