Skip to content

Exiting a process calls exit() which isn’t thread-safe #83994

Closed as not planned

Description

@DemiMarie

On Unix-like platforms, std::process::exit calls 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.

Activity

  1. added
    C-bug
    on Apr 8, 2021
  2. added
    I-unsound
    T-libs
    on Apr 8, 2021
  3. added
    I-prioritize
    on Apr 8, 2021
  4. jyn514 commented on Apr 8, 2021

    @jyn514
    Member

    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?

  5. jyn514 commented on Apr 8, 2021

    @jyn514
    Member

    I'm going to move this from T-libs-impl to T-libs because not calling libc::exit is user-facing.

  6. added
    T-libs-api
    and removed
    T-libs
    on Apr 8, 2021
  7. hameerabbasi commented on Apr 8, 2021

    @hameerabbasi
    Contributor

    Removing I-prioritize and adding I-nominate as discussed on Zulip.

    @rustbot labels -I-prioritize +I-nominate

  8. added and removed
    I-prioritize
    on Apr 8, 2021
  9. DemiMarie commented on Apr 8, 2021

    @DemiMarie
    ContributorAuthor

    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.

  10. cuviper commented on Apr 12, 2021

    @cuviper
    Member

    In Linux man-pages, exit is marked MT-unsafe:
    https://man7.org/linux/man-pages/man3/exit.3.html#ATTRIBUTES

    The exit() function uses a global variable that is not protected, so it is not thread-safe.

  11. 57 remaining items

  12. richfelker commented on Feb 14, 2023

    @richfelker

    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.

  13. RalfJung commented on Feb 14, 2023

    @RalfJung
    Member

    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 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

  14. ojeda commented on Feb 14, 2023

    @ojeda
    Contributor

    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.

  15. richfelker commented on Feb 15, 2023

    @richfelker

    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.

  16. workingjubilee commented on Feb 15, 2023

    @workingjubilee
    Member

    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.

  17. RalfJung commented on Feb 15, 2023

    @RalfJung
    Member

    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.

  18. ojeda commented on Feb 15, 2023

    @ojeda
    Contributor

    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.

  19. workingjubilee commented on Feb 18, 2023

    @workingjubilee
    Member

    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).

  20. RalfJung commented on Jul 1, 2024

    @RalfJung
    Member

    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).

    @workingjubilee turns out that exactly 32 handlers are enough: #126600

  21. tbu- commented on Jul 1, 2024

    @tbu-
    Contributor

    @workingjubilee turns out that exactly 32 handlers are enough: #126600

    No, glibc adds its own, so it is technically more than 32 if you add 32 handlers.

  22. nicowilliams commented on Aug 21, 2024

    @nicowilliams

    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.

  23. DemiMarie commented on Aug 22, 2024

    @DemiMarie
    ContributorAuthor

    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.

  24. RalfJung commented on Aug 22, 2024

    @RalfJung
    Member

    This is a closed issue, please move the discussion to a place that is better suited. :)

    (e.g. #126600)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugI-unsoundT-libs-apidisposition-closefinished-final-comment-period

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions