You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exiting a process calls exit() which isn’t thread-safe #83994
C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libs-api[DEPRECATED; DO NOT USE]disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
On Unix-like platforms, std::process::exitcalls libc::exit(). However, this can lead to undefined behavior in a multithreaded process.
In my case, this showed up as use-after-free crashes when running the testsuite for RPM Oxide. librpm registers an atexit() handler to clean up global resources, but this causes some of the resources to be freed while other threads are still using them. Other projects have had similar issues, such as the Rust bindings for RocksDB.
The best answer I know of is to call quick_exit instead, which only calls functions registered with at_quick_exit. Such functions should be safe to be called while other threads are running.
Hmm, it seems really weird to me for exit() not to call libc::exit. It seems like the issue is libraries shouldn't be registering exit handler that aren't thread safe?
The best answer I know of is to call quick_exit instead, which only calls functions registered with at_quick_exit. Such functions should be safe to be called while other threads are running.
I don't see how would quick_exit be different from exit if you registered a non-thread-safe exit handler (by at_quick_exit). You should either use _Exit or not registering one at all.
The assumption (from an old C++ proposal I read) is that doing so is considered a bug on the part of the caller of at_quick_exit.
In particular, quick_exit does not call C++ static destructors for this reason.
And yes it makes a huge difference for us whether a library can "just" stop the entire process ... vs cause UB that might have arbitrary bad side-effects (which should be 100% ruled out, that is the entire point of Rust's safety guarantees).
You're talking about a library written in C, not Rust. A library written in C is inherently not memory-safe and can always blow up your process with arbitrarily-bad side effects in ways Rust cannot rule out.
If the library is written in Rust and uses std::process::exit, Rust's standard library can put a lock around the actual call to the underlying C exit and thereby ensure that it's well-defined.
It should be possible to safely combine code written in Rust with code written in another safe language, such as Java. But there's no way to coordinate a shared lock between Rust's and Java's exit functions. So this relies on an ecosystem that provides reasonable primitives, free from the kind of surprising footguns that libc exit seems to (be allowed to) have.
I'm still in favor of working to get this "fixed" on the C side, because the undefinedness seems completely gratuitous and potentially problematic.
I'm putting my support behind that option then. :D
This could be fixed for C2y. If a major libc like musl agrees with the new wording, that would help pushing the paper.
Am I understanding correctly that you are looking for something along the lines of "If a program calls the exit function more than once, or calls the quick_exit function in addition to the exit function, then every thread that calls either of the functions, except the first thread, will be blocked until process termination."? (plus all related changes needed)
Another way would be to make it IB, and hope all major libcs end up documenting a lock.
I'm not sure I'm reading that proposed text correctly, but as I read it, it's saying exit would suspend execution of all threads except the one that calls it? That would be contrary to all existing practice and would necessarily deadlock if any thread were in the middle of a stdio operation or held a lock used by any of the exit handlers. I think maybe the intent was that only those vying to exit would be suspended, i.e. exit would behave as if it acquires a mutex before performing any operations and never releases that mutex.
I'm not sure the degree to which we could get consensus on something like that, but we can see. This probably calls for some research into what glibc, the BSDs, Apple, etc. do when multiple threads call exit.
I still think the extent of the problem is exaggerated. Traditionally and even now, while Java is supposedly memory safe at the specification level, the implementation of the runtime is chock-full of UB around system-interface-level things like this. For example I recall there being multiple versions of the backend for executing child processes, and all of them having UB around misuse of vfork or AS-unsafe functions after fork in a MT parent. To me, it's completely expected that, if you mix a runtime of Java's quality (or lack thereof) with Rust, and it races to exit, things will break in subtle to spectacular ways.
While it's certainly not a language that tickles my fancy, taking such a swipe at Java's prior bugs here is not only beneath you, it is a distraction, as Java was just an example and its quality of implementation doesn't matter. The problem in question returns if one introduces runtimes that don't have any UB around system interfaces. Even most purely functional programming languages have functions that can diverge at runtime (and thus call exit(n)).
It would seem absurd if, by dint of a system only providing C interfaces to call process-terminating functions, one could not actually confidently run a program made by linking any two purely functional programming languages, even if both had their soundness actually formally proven, independently, and even if both used a "thin runtime" as C and Rust do, without having them both first agree to only allow one to call a diverging function amongst all their threads.
Indeed feel free to substitute Java for literally any language that provides exit, which is the vast majority. The way exit seems to work makes it fundamentally impossible to write multi-language programs with memory safety guarantees. Each language can hack around that issue for single-language programs, but the only way to actually fix this is to have the system libs provide an exit that is safe to call from multiple threads concurrently.
(That is also the parallel with the env situation: each individual language can actually provide a thread-safe mutable environment by adding a lock. But the only way to have that work in multi-language programs is to make the underlying system lib thread safe for env mutation. Those libs don't seem to consider that a desirable property, and the rest of the ecosystem is beholden to that decision.)
We don't necessarily need the C standard to change here, but for targets Rust supports we do need to ensure that their exit implementation if sufficiently thread-safe.
I'm not sure I'm reading that proposed text correctly, but as I read it, it's saying exit would suspend execution of all threads except the one that calls it?
Yeah, one could interpret it like that. I moved the "except" part and added a couple commas, that should make it a bit more clear, please take a look. Otherwise, the standard can always be more explicit, like repeating "that called ...". To be clear, the actual wording could be different, but I wanted to see if we were speaking about the same thing.
For what it's worth we have done some research and glibc does in fact lock things. It apparently sometimes causes issues if you have more than 32 atexit handlers (it organizes those handlers in buckets of 32) but if you don't have such an exciting program you are likely to have a fairly predictable result (and the issues may simply be due to bugs in the locking impl).
For what it's worth we have done some research and glibc does in fact lock things. It apparently sometimes causes issues if you have more than 32 atexit handlers (it organizes those handlers in buckets of 32) but if you don't have such an exciting program you are likely to have a fairly predictable result (and the issues may simply be due to bugs in the locking impl).
In my case, this showed up as use-after-free crashes when running the testsuite for RPM Oxide. librpm registers an atexit() handler to clean up global resources, but this causes some of the resources to be freed while other threads are still using them. Other projects have had similar issues, such as the Rust bindings for RocksDB.
The issue here is that the other threads need to be quiesced before releasing those resources or do not release those resources. It's a bug in RPM Oxide.
In my case, this showed up as use-after-free crashes when running the testsuite for RPM Oxide. librpm registers an atexit() handler to clean up global resources, but this causes some of the resources to be freed while other threads are still using them. Other projects have had similar issues, such as the Rust bindings for RocksDB.
The issue here is that the other threads need to be quiesced before releasing those resources or do not release those resources. It's a bug in RPM Oxide.
I avoided the problem by adding an atexit() handler that acquired a mutex (and never released it), and requiring all calls to the library that used the relevant state to acquire the same mutex. This worked, but the reason I filed this issue is because I ran into the problem during development.
C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libs-api[DEPRECATED; DO NOT USE]disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
Activity
jyn514 commentedon Apr 8, 2021
Hmm, it seems really weird to me for exit() not to call libc::exit. It seems like the issue is libraries shouldn't be registering exit handler that aren't thread safe?
jyn514 commentedon Apr 8, 2021
I'm going to move this from T-libs-impl to T-libs because not calling libc::exit is user-facing.
hameerabbasi commentedon Apr 8, 2021
Removing
I-prioritizeand addingI-nominateas discussed on Zulip.@rustbot labels -I-prioritize +I-nominate
DemiMarie commentedon Apr 8, 2021
The assumption (from an old C++ proposal I read) is that doing so is considered a bug on the part of the caller of
at_quick_exit.In particular,
quick_exitdoes not call C++ static destructors for this reason.cuviper commentedon Apr 12, 2021
In Linux man-pages,
exitis marked MT-unsafe:https://man7.org/linux/man-pages/man3/exit.3.html#ATTRIBUTES
57 remaining items
richfelker commentedon Feb 14, 2023
You're talking about a library written in C, not Rust. A library written in C is inherently not memory-safe and can always blow up your process with arbitrarily-bad side effects in ways Rust cannot rule out.
If the library is written in Rust and uses
std::process::exit, Rust's standard library can put a lock around the actual call to the underlying Cexitand thereby ensure that it's well-defined.RalfJung commentedon Feb 14, 2023
Now we're back to this message. ;)
It should be possible to safely combine code written in Rust with code written in another safe language, such as Java. But there's no way to coordinate a shared lock between Rust's and Java's
exitfunctions. So this relies on an ecosystem that provides reasonable primitives, free from the kind of surprising footguns that libcexitseems to (be allowed to) have.I'm putting my support behind that option then. :D
ojeda commentedon Feb 14, 2023
This could be fixed for C2y. If a major libc like musl agrees with the new wording, that would help pushing the paper.
Am I understanding correctly that you are looking for something along the lines of "If a program calls the
exitfunction more than once, or calls thequick_exitfunction in addition to theexitfunction, then every thread that calls either of the functions, except the first thread, will be blocked until process termination."? (plus all related changes needed)Another way would be to make it IB, and hope all major libcs end up documenting a lock.
richfelker commentedon Feb 15, 2023
I'm not sure I'm reading that proposed text correctly, but as I read it, it's saying
exitwould suspend execution of all threads except the one that calls it? That would be contrary to all existing practice and would necessarily deadlock if any thread were in the middle of a stdio operation or held a lock used by any of the exit handlers. I think maybe the intent was that only those vying to exit would be suspended, i.e.exitwould behave as if it acquires a mutex before performing any operations and never releases that mutex.I'm not sure the degree to which we could get consensus on something like that, but we can see. This probably calls for some research into what glibc, the BSDs, Apple, etc. do when multiple threads call
exit.I still think the extent of the problem is exaggerated. Traditionally and even now, while Java is supposedly memory safe at the specification level, the implementation of the runtime is chock-full of UB around system-interface-level things like this. For example I recall there being multiple versions of the backend for executing child processes, and all of them having UB around misuse of
vforkor AS-unsafe functions afterforkin a MT parent. To me, it's completely expected that, if you mix a runtime of Java's quality (or lack thereof) with Rust, and it races to exit, things will break in subtle to spectacular ways.workingjubilee commentedon Feb 15, 2023
While it's certainly not a language that tickles my fancy, taking such a swipe at Java's prior bugs here is not only beneath you, it is a distraction, as Java was just an example and its quality of implementation doesn't matter. The problem in question returns if one introduces runtimes that don't have any UB around system interfaces. Even most purely functional programming languages have functions that can diverge at runtime (and thus call
exit(n)).It would seem absurd if, by dint of a system only providing C interfaces to call process-terminating functions, one could not actually confidently run a program made by linking any two purely functional programming languages, even if both had their soundness actually formally proven, independently, and even if both used a "thin runtime" as C and Rust do, without having them both first agree to only allow one to call a diverging function amongst all their threads.
RalfJung commentedon Feb 15, 2023
Indeed feel free to substitute Java for literally any language that provides
exit, which is the vast majority. The wayexitseems to work makes it fundamentally impossible to write multi-language programs with memory safety guarantees. Each language can hack around that issue for single-language programs, but the only way to actually fix this is to have the system libs provide anexitthat is safe to call from multiple threads concurrently.(That is also the parallel with the env situation: each individual language can actually provide a thread-safe mutable environment by adding a lock. But the only way to have that work in multi-language programs is to make the underlying system lib thread safe for env mutation. Those libs don't seem to consider that a desirable property, and the rest of the ecosystem is beholden to that decision.)
We don't necessarily need the C standard to change here, but for targets Rust supports we do need to ensure that their
exitimplementation if sufficiently thread-safe.ojeda commentedon Feb 15, 2023
Yeah, one could interpret it like that. I moved the "except" part and added a couple commas, that should make it a bit more clear, please take a look. Otherwise, the standard can always be more explicit, like repeating "that called ...". To be clear, the actual wording could be different, but I wanted to see if we were speaking about the same thing.
workingjubilee commentedon Feb 18, 2023
For what it's worth we have done some research and glibc does in fact lock things. It apparently sometimes causes issues if you have more than 32
atexithandlers (it organizes those handlers in buckets of 32) but if you don't have such an exciting program you are likely to have a fairly predictable result (and the issues may simply be due to bugs in the locking impl).RalfJung commentedon Jul 1, 2024
@workingjubilee turns out that exactly 32 handlers are enough: #126600
tbu- commentedon Jul 1, 2024
No, glibc adds its own, so it is technically more than 32 if you add 32 handlers.
nicowilliams commentedon Aug 21, 2024
The issue here is that the other threads need to be quiesced before releasing those resources or do not release those resources. It's a bug in RPM Oxide.
DemiMarie commentedon Aug 22, 2024
I avoided the problem by adding an
atexit()handler that acquired a mutex (and never released it), and requiring all calls to the library that used the relevant state to acquire the same mutex. This worked, but the reason I filed this issue is because I ran into the problem during development.RalfJung commentedon Aug 22, 2024
This is a closed issue, please move the discussion to a place that is better suited. :)
(e.g. #126600)
fix shutdown teardown crash by joining runtimes and cancelling rocksd…
disable coredumps in fuel-core-bin to keep pod restarts fast (#3301)