-
Notifications
You must be signed in to change notification settings - Fork 74
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
Restoring system handler when slot empties #102
Comments
Hello I'm pretty sure I've written the reasons somewhere, but in short, the library assumes:
Your solution has several problems:
These put together suggest the unregistering would behave quirkily at best, therefore the behaviour of just list of hooks that can even do nothing because of empty after the first takeover seemed much easier to reason about and make behave consistently when interacting with other parts, so it's not the thing it does by default. But I believe the use case you describe should be covered by this function: https://docs.rs/signal-hook/0.3.8/signal_hook/flag/fn.register_conditional_default.html. You register this and then turn that on or off by setting that atomic bool. When it's off, the hook does nothing, when on, it invokes the equivalent of the default handler. If you need something more complex, you can invoke the default behaviour directly by https://docs.rs/signal-hook/0.3.8/signal_hook/low_level/fn.emulate_default_handler.html. Configuring the slot to behave in the way you describe (more likely by checking if it's empty inside the handler and invoking the emulation, to avoid the burried-problem and racy-problem) is planned for the future, but I haven't gotten around to that yet. That's covered in #82. As I believe the feature is already tracked and your use case should already be somehow covered, I'll close this issue. |
I won't contest that -- at best, it's something I would have suggested in an environment where I already know what's going on. In my case, I needed to capture TERM signals within unit tests because I needed to do some teardown of an external mock environment before allowing the program to close, but I also wanted to be able to restore the environment for the next unit test (which would obviously have to operate serially). In hindsight, this is already incredibly complex due to the fact that Rust unit tests generally run in parallel. Trying to do this via a signal handler probably doesn't even make sense. A watchdog process is probably the way to go. Thanks for the commentary and explanation. |
Hmm, I think I can post few tips around the use case of tests:
|
signal_hook::low_level::unregister()
documents that the default handler isn't restored when the last action is removed. I couldn't find a justification for this. I'm trying to implement some temporary conditions where I want to trap signals, then restore the original behavior, but that appears to be unimplemented.Did this cause issues? A quick 'n dirty (and likely naive) fix appears to simply be this:
Some quick sanity tests seem to work fine.
The text was updated successfully, but these errors were encountered: