Skip to content

rt::atexit callbacks don't run after exit(0) call #28065

Closed

Description

@sfackler

Specifically, the stdout buffer doesn't flush: from #28058:

fn main() { print!("Hi!"); } results in "Hi!", fn main() { print!("Hi!"); std::process::exit(0); } results in no output.

We should probably just be using libc atexit here.

cc @alexcrichton

Activity

  1. alexcrichton commented on Aug 28, 2015

    @alexcrichton
    Member

    I... thought we had an issue for this and did this previously, not sure what happened here!

  2. retep998 commented on Aug 28, 2015

    @retep998
    Contributor

    To quote the source code

    // FIXME: switch this to use atexit. Currently this
    // segfaults (the queue's memory is mysteriously gone), so
    // instead the cleanup is tied to the `std::rt` entry point.

    The only way that at_exit_imp::cleanup is invoked is when the main thread either returns gracefully or panics gracefully so the Rust entry point can run the cleanup method, which clearly excludes when invoking process::exit.

  3. alexcrichton commented on Aug 28, 2015

    @alexcrichton
    Member

    Hm, I had forgotten about that comment. Regardless though there is no reason that this should fundamentally not be able to use atexit, it's just some implementation detail that needs to be worked out

  4. retep998 commented on Aug 28, 2015

    @retep998
    Contributor

    The way the CRT on Windows handles atexit is it just has a table of all the atexit handlers, and when you call the CRT's exit it goes through and executes all the handlers and then calls ExitProcess (or TerminateProcess for windows store apps). Since Rust currently calls ExitProcess directly, that means using the libc atexit is useless since its handlers will never fire. However, it also means we can easily get rt::atexit to work with process::exit by just having process::exit call the rt::cleanup method (with some sort of synchronization to ensure it is only called once).

  5. alexcrichton commented on Aug 28, 2015

    @alexcrichton
    Member

    Hm, you know I had a feeling the test I added in #28069 would fail on Windows for a reason like that, I think we'll have to do something like that on Windows (call cleanup manually from process::exit)

  6. added a commit that references this issue on Sep 4, 2015
    6f1014f
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions