-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[Feature] Improve signal and shutdown handling #3915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
fe9131c to
6b4fda4
Compare
6b4fda4 to
aec7b2a
Compare
eee21b7 to
d2a5661
Compare
3f2cbb7 to
369c075
Compare
ljedrz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one small change left, and it's good to go 👍.
cbf4d86 to
1ff28fd
Compare
1ff28fd to
aa6ddfc
Compare
ljedrz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few final nits, but basically LGTM 👌.
vicsn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you note in the PR README which manual tests were run? Are they comprehensive?
Done! |
vicsn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
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.