-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Rework init
and cleanup
#84115
Rework init
and cleanup
#84115
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
Would it be possible to avoid changing the behavior of when initialization is done (on Windows), making this a pure refactoring PR, and split that work out into a separate PR? The changes to initialization time for the Windows code are not obviously correct to me, as I think we don't make any guarantees that the init() function is run before std code is executed - e.g., if calling into a Rust library from C - is that right? Do we have some linker trickery to ensure it runs in that case? cc @m-ou-se as well; it'd be good to get some more eyes on this in the long run; I think we should get some eyes on PRs of this subtlety :) |
Of course. I excluded the last two commits containing the changes to initialization, so this is now purely a refactoring. |
☔ The latest upstream changes (presumably #84206) made this pull request unmergeable. Please resolve the merge conflicts. |
library/std/src/sys_common/rt.rs
Outdated
static INIT: Once = Once::new(); | ||
INIT.call_once(|| unsafe { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the use of the Once
static here? Don't we already know for sure this is called exactly once?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sys_common::rt::cleanup
needs to be safe to call multiple times: it could be called in process::exit
, then panic, which is caught by lang_start_internal
, and then be called again. For symmetry I then made sys_common::rt::init
also not unsafe to call, which is why I added the Once
, to not have to rely on that it can only be called once remaining true in the future. I could remove it however.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exit()
could also be called from multiple threads concurrently, so cleanup
definitely needs this. But for init
I'd remove it if it's not necessary, to not confuse the reader about what is going on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure 👍🏻
I confirmed this is correct, |
I'm not sure what is going on with the runtime on rust/library/std/src/sys/hermit/mod.rs Lines 109 to 128 in dc99219
It could be that I could change it to: // initialize environment
os::init_environment(env as *const *const i8);
sys_common::rt::init(argc as isize, argv);
let result = main(argc as isize, argv);
sys_common::rt::cleanup();
abi::exit(result); and move |
@CDirkx So, you can read that call to *Unrelated to the Rust |
Ah that's where my confusion came from, then I'll leave it as is. |
@bors r+ rollup=iffy Not rolling this one up, since it touches a lot of platform-specific code, so there's a higher chance of CI failures. |
📌 Commit 83608021cca5419d8dc1424c5c42bbd94b6cad8f has been approved by |
🌲 The tree is currently closed for pull requests below priority 1000. This pull request will be tested once the tree is reopened. |
☔ The latest upstream changes (presumably #84411) made this pull request unmergeable. Please resolve the merge conflicts. |
Rebased and resolved merge conflicts |
@bors r+ |
📌 Commit 9f153eeb34684924aa7880fac6f0e4926a471f20 has been approved by |
⌛ Testing commit 9f153eeb34684924aa7880fac6f0e4926a471f20 with merge 7289a3db6602624f3e3bc554f0668a599c5699b3... |
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
Added the extra required |
@bors r+ |
📌 Commit 7171fec has been approved by |
☀️ Test successful - checks-actions |
This PR reworks the code in
std
that runs before and aftermain
and centralizes this code respectively in the functionsinit
andcleanup
in bothsys_common
andsys
. This makes is easy to see what code is executed during initialization and cleanup on each platform just by looking at e.g.sys::windows::init
.Full list of changes:
rt
insys_common
to containinit
andcleanup
and the runtime macros.at_exit
and the mechanism to register exit handlers has been completely removed. In practice this was only used for closing sockets on windows and flushing stdout, which have been moved tocleanup
.On windowsalloc
andnet
initialization is now done ininit
, this saves a runtime check in every allocation and network use.