Skip to content
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

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

Closed
sfackler opened this issue Aug 28, 2015 · 5 comments
Closed

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

sfackler opened this issue Aug 28, 2015 · 5 comments

Comments

@sfackler
Copy link
Member

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

@alexcrichton
Copy link
Member

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

@retep998
Copy link
Member

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.

@alexcrichton
Copy link
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

@retep998
Copy link
Member

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

@alexcrichton
Copy link
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)

alexcrichton added a commit to alexcrichton/rust that referenced this issue Sep 3, 2015
This adds a call to `rt::cleanup` on `process::exit` to make sure we clean up
after ourselves on the way out from Rust.

Closes rust-lang#28065
bors added a commit that referenced this issue Sep 4, 2015
This adds a call to `rt::cleanup` on `process::exit` to make sure we clean up
after ourselves on the way out from Rust.

Closes #28065
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants