-
Notifications
You must be signed in to change notification settings - Fork 34
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
Signal handling #27
Comments
Thank you! I had meant to open an issue for exactly this yesterday and got side tracked. For Windows I wonder if @retep998 would know? |
Windows does not have signals. It does however have some similar mechanics.
|
A unix process can receive SIGWINCH when the user resizes the terminal it's running in. The right behaviour might be to do nothing, or to redraw some parts of the UI. It is definitely not to crash, which is something i have seen happen, so as a minimum, can we make sure that any signal handling we do doesn't overreact to SIGWINCH? |
Could termination signals be handled as a future? The application would obtain the future through some magical means; the signal handler would complete the future with the value of the signal; it would be up to the application to detect that it has completed and take some action. |
To summarize the linked SIGWINCH bug, as best I understand it:
In the world of Rust, Unfortunately, I don't think there's anything a Rust CLI framework can do to prevent application code from overreacting to |
Thoughts on signal-hook? See also the blog post and reddit discussion |
Real world example of an application that's using custom signal handling. Figured it might be useful to gather examples. |
Also probably useful to enumerate some crates for signal handling. If anyone has seen any others, it'd be great to know!
I feel there are a good amount of solutions out there already. It might be useful to try and enumerate which parts of these libraries are found lacking, and possibly contribute! I'm personally really interested in figuring out ways to make documentation of signal handlers. A thing that might help a lot would probably be if there's some consistency in how signals were defined. I'm not certain what the right approach for this might be, but that might be worth exploring further! Wrapping UpI hope all this makes sense; I'm real keen to hear what other folks think of this! |
Two aspects here are what crates to recommend when people look into this, and how to document which signals an app supports. I'll assign this to me but will ping people who commented here to find consensus around what to do next on this issues. |
chan-signal was deprecated in favor of signal-hook today — https://users.rust-lang.org/t/ann-chan-is-deprecated-use-crossbeam-channel-instead/19251 |
Hey, I'm about to write the signal chapter for the book, based on the info in this issue. Thanks again for the great summary, @uazu! The ctrlc crate seems to be a good start for getting into handling SIGINT and the equivalent on Windows, so I want to start with recommanding that. I've not seen much Rust-on-Windows specific. Is there any other common pattern/crate I should mention, @retep007? On top of that I want to mention signal-hook for UNIX platforms. |
@killercup Related issue: rust-lang-nursery/rust-cookbook#501 If you need some inspiration, here's a simple stopwatch that gets stopped on Ctrl-C (and I love how it combines |
Oh, very interesting! Thanks! I'll probably need to make this more CLI-related but I might end up stealing that example as it's the next logical step to use this with crossbeam-channel :) |
The main ones are SIGINT (^C) and SIGTERM (
kill
). For CLI apps that require no special cleanup, the default handling is fine (i.e. exit immediately and let the OS cleanup).For CLI apps that need to do special cleanup (e.g. flush to files or database, nicely terminate network connections, deconfigure devices, etc), these signal handlers need to be caught. Some possible models:
Catch signal, set a global flag, and have a call to check that global flag regularly in inner loops, and convert to an error to pass up the stack to do cleanup before exiting.
Try and do cleanup in the signal handler. But this is pretty hard as the calls that are permitted in a signal handler are quite restricted (see
man 7 signal-safety
on Linux)In the case of catching ^C, if the global flag is already set, then on the second ^C consider exiting immediately, to give the user a way out if the CLI app is being unresponsive.
I don't know Windows. What's the model there for these kinds of signals?
If there's a crate already out there that handles this well, then someone link that here and perhaps we can consider this dealt with.
The text was updated successfully, but these errors were encountered: