-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
thread_local macro stability precludes safe async signal handling #30003
Comments
There are other substantial problems with asynchronous signal handlers... for example, Given all of that, it's extremely unlikely the standard library will ever provide safe async signal handlers.
|
It is certainly possible to write a signal-safe malloc and it is also possible to write programs that don't call any C functions. Furthermore, there are probably also malloc implementations out there that are not thread-safe. Yet this does not stop the language from requiring statics to be |
Triage: this is still marked as needs-decision for @rust-lang/lang, but i'm not sure there was ever a discussion on it. Personal opinion: the root issue here is the unsafe, and so it's not likely to be fixed. |
I just published |
Which mechanism achieves that? |
Ah, sorry. I mask (disable) all signals when borrowing |
Ah okay, so that way the Enabling signals while having the |
Yeah, enabling them should be "fine" but unwanted, since trying to borrow mutably in a signal handler while the main context holds another reference would not work. So losing mutual exclusion with signal handlers on |
Yeah, compiler fences are the base for this. |
Note that you absolutely need to have interrupts disabled whenever the RefCell's borrow flags are accessed. This is crucial for soundness. So doing just "best-effort" interrupt disabling is unsound. See mkroening/interrupt-ref-cell#5.
No, it's unsound. You get a data race between the thread releasing its hold of the RefCell and the handler trying to acquire it. |
The
thread_local!
macro accepts arbitrary (non-Sync) objects to be put into thread local storage. It is not hard to construct a case where this causes signal handlers to observe inconsistent state:RefCell
is not signal safe. A mutable borrow will not mark theRefCell
as being borrowed in this case. This can be simulated as follows:-O -C lto
signal 2
Expected result: panic/abort/segfault or similar. Actual result:
(1, 0)
is printed.Fixing RefCell by adding a memory barrier does not fix the problem since there might be many other non-Sync types that were not written with signal handling in mind and that use unsafe constructs. For correctness, TLS would have to be restricted to types that are async safe via a new marker trait. With such a trait, signal handling would be safe by default in all rust code and all signals handlers could call arbitrary rust functions (as long as said functions don't call non-rust code which might not be async safe.)
This concerns me because adding a signal handler is a safe operation in lrs and all
#[thread_local]
objects that require mutation are wrapped in a single threaded mutex implementation with interior mutability. And if it is decided that async signal handling is never safe in rust, then#[thread_local]
might be stabilized and might also start to accept arbitrary objects which would practically force me to create a full compiler fork for the sake of safety. The current implementation in lrs is already unsafe because the single threaded mutex implementation has to be markedSync
to be placed in a#[thread_local]
. For correctness, there would have to be the above mentioned marker trait that restricts what can be put in a#[thread_local]
.The text was updated successfully, but these errors were encountered: