Skip to content

Commit d2a8de9

Browse files
committed
Rename the host exits after C exit() and fix wasi and Windows builds
- host_env::os::exit now calls the C exit() on every platform; on Windows std::process::exit ends in ExitProcess, so it flushes the Rust stdout and calls libc::exit there. The former exit, which calls _exit(), is now exit_immediately, used by os._exit, faulthandler and posixsubprocess. - errno::strerror_string is available on wasi. - PyGlobalState::reinit_running_main is built only where its fork caller is. Assisted-by: Claude Code:claude-opus-5-5
1 parent 91483ac commit d2a8de9

7 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎crates/host_env/src/errno.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/// Return the platform `strerror(errno)` message as an owned `String`.
44
/// Returns `None` when the runtime gives no description for `errno`.
5-
#[cfg(any(unix, windows))]
5+
#[cfg(any(unix, windows, target_os = "wasi"))]
66
#[must_use]
77
pub fn strerror_string(errno: i32) -> Option<String> {
88
let ptr = unsafe { libc::strerror(errno) };

‎crates/host_env/src/os.rs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,22 @@ pub fn device_encoding(_fd: i32) -> Option<String> {
521521

522522
/// Terminate the process through the C `exit()`, which runs the C atexit
523523
/// handlers and flushes C stdio.
524-
pub fn exit_with_cleanup(code: i32) -> ! {
525-
std::process::exit(code)
524+
pub fn exit(code: i32) -> ! {
525+
cfg_select! {
526+
// std::process::exit ends in ExitProcess here, which skips the CRT
527+
// atexit handlers and the stdio flush.
528+
windows => {
529+
use std::io::Write;
530+
let _ = std::io::stdout().flush();
531+
unsafe { libc::exit(code) }
532+
}
533+
_ => std::process::exit(code),
534+
}
526535
}
527536

528-
pub fn exit(code: i32) -> ! {
529-
// These callers require immediate termination without C exit handlers.
537+
/// Terminate the process through `_exit()`, without running the C atexit
538+
/// handlers or flushing C stdio.
539+
pub fn exit_immediately(code: i32) -> ! {
530540
cfg_select! {
531541
any(unix, windows, target_os = "wasi") => unsafe { libc::_exit(code) },
532542
_ => std::process::exit(code),

‎crates/stdlib/src/faulthandler.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ mod decl {
408408
host_faulthandler::signal_default_and_raise(signum);
409409

410410
// Fallback
411-
rustpython_host_env::os::exit(1);
411+
rustpython_host_env::os::exit_immediately(1);
412412
}
413413

414414
// Windows vectored exception handler (faulthandler.c:417-480)
@@ -687,7 +687,7 @@ mod decl {
687687
}
688688

689689
if exit {
690-
rustpython_host_env::os::exit(1);
690+
rustpython_host_env::os::exit_immediately(1);
691691
}
692692
}
693693
}

‎crates/stdlib/src/posixsubprocess.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn exec(args: &ForkExecArgs<'_>, procargs: ProcArgs<'_>, vm: &VirtualMachine) ->
283283
let errno = e.raw_os_error().unwrap_or(0);
284284
let _ = write!(pipe, "OSError:{errno:x}:{}", ctx.as_msg());
285285
}
286-
rustpython_host_env::os::exit(255)
286+
rustpython_host_env::os::exit_immediately(255)
287287
}
288288
}
289289
}

‎crates/vm/src/stdlib/os.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ pub(super) mod _os {
17461746

17471747
#[pyfunction]
17481748
fn _exit(status: ExitStatus) {
1749-
crate::host_env::os::exit(status.status)
1749+
crate::host_env::os::exit_immediately(status.status)
17501750
}
17511751

17521752
#[pyfunction]

‎crates/vm/src/vm/interpreter.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ impl VirtualMachine {
728728
} else {
729729
status
730730
};
731-
crate::host_env::os::exit_with_cleanup(status as i32)
731+
crate::host_env::os::exit(status as i32)
732732
}
733733

734734
/// The steps of Py_FinalizeEx after the first flush of stdout and stderr.

‎crates/vm/src/vm/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ impl PyGlobalState {
995995
///
996996
/// After a fork only the forking thread survives, so `__main__` is no
997997
/// longer running unless that thread was running it.
998+
#[cfg(all(feature = "host_env", unix))]
998999
pub(crate) fn reinit_running_main(&self) {
9991000
if !self.thread_is_running_main() {
10001001
self.running_main.store(None);

0 commit comments

Comments
 (0)