Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upthread_local macro stability precludes safe async signal handling #30003
Comments
This comment has been minimized.
This comment has been minimized.
|
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.
|
This comment has been minimized.
This comment has been minimized.
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 |
eefriedman
referenced this issue
Nov 24, 2015
Closed
Change thread local variables to only accept async-signal-safe types. #1379
brson
added
P-low
T-libs
T-lang
I-needs-decision
and removed
T-libs
labels
May 4, 2017
Mark-Simulacrum
added
the
C-bug
label
Jul 24, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
mahkoh commentedNov 23, 2015
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:RefCellis not signal safe. A mutable borrow will not mark theRefCellas being borrowed in this case. This can be simulated as follows:-O -C ltosignal 2Expected 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 markedSyncto 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].