vorner / signal-hook

Rust library allowing to register multiple handlers for the same signal
Apache License 2.0
725 stars 71 forks source link

[Question] Exiting from a thread in a signal handler #141

Closed mrcnski closed 1 year ago

mrcnski commented 1 year ago

Hello. Thank you for the delightful blog posts about signals -- that was a fun rabbithole.

I have what is (hopefully) a simple question: is it safe to call pthread_exit in register?

For background, I'm trying to handle a "thread-directed" signal (SIGSYS from seccomp) in a thread that does blocking work, and therefore can't use signal-hook's abstractions (AFAIK). I'm hoping to return a value from the single thread rather than just terminate the process.

vorner commented 1 year ago

Hello

I'd say that most probably no, it is not. For several reasons:

But looking at the documentation of when a thread might get a SIGSYS, I think one possible solution would be to either ignore the signal and let the syscall fail for the process (and then hope error handling is correct and the thread gracefully reaches some sane error state / return value) or block the signal for the thread and pick it up later with something like sigwait.

And you're right, signal-hook isn't really the tool for this particular job.

mrcnski commented 1 year ago

Thanks for the informative answer! Quite interesting, though I appreciate your work in shielding Rust developers from these details.

In our case, we determined that instead of using the SECCOMP_RET_TRAP action, having seccomp kill the process is acceptable, if somewhat suboptimal. Cheers!