vorner / signal-hook

Rust library allowing to register multiple handlers for the same signal

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

signal-hook-tokio: Functionality available inside signal handlers?

glts opened this issue · comments

commented

The documentation of signal-hook contains a few warnings about usage. For example, it says:

[…] the set of functions one may call inside a signal handler is limited to very few of them. […] mutexes (or other locking mechanisms) and memory allocation and deallocation is not allowed

Does this apply to signal-hook-tokio, too? The doc does not have such a warning. The example even shows reloading configuration and reopening log files inside the signal handler.

Are there some guidelines on Rust functionality safely available in a signal-hook-tokio signal handler? Thank you!

Hello

The warnings talks (at least in this specific cited case) about the actual inside of the „raw“ OS signal handler, the thing one passes to eg. sigaction. That place is very dangerous indeed.

Most of the patterns in signal-hook and signal-hook-tokio are actually tricks how to grab the info from the signal handler and postpone running any kind of „custom“ code to later, once already in a safe place outside of the signal handler. The warning is about things you would have to care if you were not using signal hook or if you use one of the unsafe APIs it exposes, eg register.

I'm open to improving the documentation in a way that makes it clearer this section is „advertisement“ or „motivation“, listing the things that signal-hook shields away from.

If you don't use anything unsafe from signal-hook-*, you'll not get UB from signals. You should, of course, use some caution, because they still can come in a bad moment (eg. when you're starting up or shutting down the application and the tokio loop no longer runs).

commented

Ah right, so signal-hook and signal-hook-tokio make safe what might not be safe otherwise. Perhaps this is obvious already, and it's just me who was confused. Thanks a lot for the help.

Let me close this one, then.