-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
std: Run at_exit cleanup on process::exit #28069
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
r? @aturon I remember this being super hard to land in the past, but I forget precisely why, and I can always try to patch up any bot failures! |
72bce95
to
4749d84
Compare
I'm concerned that this may tie us to C semantics in a deeper way than anything else presently in std. Are Rust exit handlers going to be defined always as compatible with the C runtime's atexit handlers? What if there are situations where std can't depend on C atexit? |
Hm, could you elaborate on your concerns a bit more? I don't think we necessarily need to provide any guarantees just yet other than that we'll try our best to run them, not necessarily the exact same semantics as libc. If there are situations where we can't depend on C atexit (like on Windows which I need to fix here) then we'll just call |
At the very least on Windows, don't rely on the libc |
ebf06b3
to
5e066ae
Compare
I've updated to call |
The commit message seems to indicate that a call to Wouldn't it be possible to avoid splitting the implementation for win/unix and just use the windows implementation for both? This would also reduce the dependence on the semantics of the C runtime, so it might also address some of the concerns expressed by @brson. diff --git a/src/libstd/process.rs b/src/libstd/process.rs
index be921d9..3d5090f 100644
--- a/src/libstd/process.rs
+++ b/src/libstd/process.rs
@@ -21,6 +21,7 @@ use fmt;
use io::{self, Error, ErrorKind};
use path;
use sync::mpsc::{channel, Receiver};
+use sync::Once;
use sys::pipe::{self, AnonPipe};
use sys::process as imp;
use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
@@ -582,6 +583,8 @@ impl Child {
/// to run.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn exit(code: i32) -> ! {
+ static CLEANUP: Once = Once::new();
+ CLEANUP.call_once(|| unsafe { ::rt::cleanup() });
::sys::os::exit(code)
} |
We could piggy-back on the TLS destructors to have unified implementation on both platforms. If that is unacceptable, it seems that the same scheme as used to register TLS destructors could be used to schedule cleanup on windows as well. It also sounds much more reliable. I’m not sure about UNIX, but at least on windows cleanup would logically run whatever way you’d exit the process. |
Note that relying on the TLS destructor mechanism means that #28111 would have to be fixed. |
@ranma42 I would prefer to take the more idiomatic road on Unix to ensure that if Rust is statically linked into another application that we play as nicely as possible there (e.g. our dtors will run via @nagisa unfortunately running TLS dtors when |
@alexcrichton The semantic change here is: 'This ensures that cleanup handlers are |
We could totally implement process::exit via direct syscall, pretty much like on Windows. |
@brson I'm not sure I quite understand these concerns. The alternative is that we intentionally avoid calling standard C functions, but that seems... bad? |
5e066ae
to
9b6b679
Compare
@alexcrichton The difference between |
9b6b679
to
9108e80
Compare
84c9e3f
to
10dcf5c
Compare
OK, after some discussion with @brson offline I've not moved this to avoid I did a bit of an audit to make sure it's ok to continue using all the pieces that are cleaned up even after they've been cleaned up (as this can all happen concurrently) and we should be good to go there. |
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
10dcf5c
to
04c09f9
Compare
@bors r+ thanks |
📌 Commit 04c09f9 has been approved by |
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
This adds a call to
rt::cleanup
onprocess::exit
to make sure we clean upafter ourselves on the way out from Rust.
Closes #28065