Skip to content

Commit 54e47cd

Browse files
authored
Fix os.closerange and immediate process exit (#9006)
* Ignore invalid descriptors in os.closerange Close raw descriptor numbers through the host CRT layer rather than constructing OwnedFd values for possibly closed descriptors. Preserve the half-open range, ignored close errors and Windows invalid-parameter handling. Assisted-by: Codex:model-version-unavailable * Use immediate exit for host process termination Python os._exit, failed child exec and fatal/watchdog exits must bypass C exit handlers. Call the host CRT _exit on Unix, Windows and WASI while retaining the existing non-host fallback. Normal interpreter termination is unchanged. Assisted-by: Codex:model-version-unavailable
1 parent b6c60a5 commit 54e47cd

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

‎crates/host_env/src/crt_fd.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ pub fn close(fd: Owned) -> io::Result<()> {
348348
_close(fd.into_raw())
349349
}
350350

351+
pub fn closerange(fd_low: Raw, fd_high: Raw) {
352+
for fd in fd_low..fd_high {
353+
// The range can contain closed descriptors, so it cannot use Owned.
354+
let _ = _close(fd);
355+
}
356+
}
357+
351358
pub fn ftruncate(fd: Borrowed<'_>, len: Offset) -> io::Result<()> {
352359
let ret = unsafe { suppress_iph!(c::ftruncate(fd.as_raw(), len)) };
353360
// On Windows, _chsize_s returns 0 on success, or a positive error code (errno value) on failure.

‎crates/host_env/src/crt_fd_unsupported.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ pub fn close(_fd: Owned) -> io::Result<()> {
153153
Err(ebadf())
154154
}
155155

156+
pub fn closerange(_fd_low: Raw, _fd_high: Raw) {}
157+
156158
pub fn ftruncate(_fd: Borrowed<'_>, _len: Offset) -> io::Result<()> {
157159
Err(ebadf())
158160
}

‎crates/host_env/src/os.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,11 @@ pub fn device_encoding(_fd: i32) -> Option<String> {
520520
}
521521

522522
pub fn exit(code: i32) -> ! {
523-
std::process::exit(code)
523+
// These callers require immediate termination without C exit handlers.
524+
cfg_select! {
525+
any(unix, windows, target_os = "wasi") => unsafe { libc::_exit(code) },
526+
_ => std::process::exit(code),
527+
}
524528
}
525529

526530
/// Wrapper around the C `abort()` call: terminates the process abnormally.

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,7 @@ pub(super) mod _os {
272272

273273
#[pyfunction]
274274
fn closerange(fd_low: i32, fd_high: i32) {
275-
for fileno in fd_low..fd_high {
276-
if let Ok(fd) = unsafe { crt_fd::Owned::try_from_raw(fileno) } {
277-
drop(fd);
278-
}
279-
}
275+
crt_fd::closerange(fd_low, fd_high);
280276
}
281277

282278
#[cfg(any(unix, windows, target_os = "wasi"))]

0 commit comments

Comments
 (0)