From 29091c7234b0e58d4e5474841dd69474d6116ce0 Mon Sep 17 00:00:00 2001 From: Benjamin Carlsson Date: Wed, 18 Nov 2026 01:43:27 +0000 Subject: [PATCH] Wait for the system `bwrap` probe with a pidfd (#62160) ## Why The system `bwrap` user namespace probe checks for child exit at fixed intervals. A pidfd signals exit as soon as the child terminates, even when a descendant still holds its stderr pipe open. ## What changed Poll the probe child's pidfd for the time remaining before the deadline. Keep the bounded `try_wait` loop to collect the exit status and handle systems where `pidfd_open` fails. GitOrigin-RevId: f69f10aa49404a7760c33bbcbfde456513ebdaa0 --- codex-rs/sandboxing/src/bwrap.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/codex-rs/sandboxing/src/bwrap.rs b/codex-rs/sandboxing/src/bwrap.rs index 5c1fe74f1b..8c86d18d4a 100644 --- a/codex-rs/sandboxing/src/bwrap.rs +++ b/codex-rs/sandboxing/src/bwrap.rs @@ -6,6 +6,8 @@ use std::ffi::CString; use std::io::ErrorKind; use std::io::Read; use std::os::fd::AsRawFd; +use std::os::fd::FromRawFd; +use std::os::fd::OwnedFd; use std::os::unix::ffi::OsStrExt; use std::os::unix::fs::MetadataExt; use std::path::Path; @@ -101,6 +103,24 @@ fn system_bwrap_has_user_namespace_access(system_bwrap_path: &Path, timeout: Dur }; let deadline = Instant::now() + timeout; + // A pidfd becomes readable when this child exits, even if a descendant retains its stderr. + // Older kernels and restricted hosts can still use the bounded try_wait loop below. + // SAFETY: pidfd_open takes the live child's PID and zero flags, and returns an owned fd. + let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, child.id(), 0_u32) }; + if fd >= 0 { + // SAFETY: the successful syscall returned a new file descriptor owned by this scope. + let pidfd = unsafe { OwnedFd::from_raw_fd(fd as libc::c_int) }; + let mut event = libc::pollfd { + fd: pidfd.as_raw_fd(), + events: libc::POLLIN, + revents: 0, + }; + let remaining = deadline.saturating_duration_since(Instant::now()); + let timeout_ms = libc::c_int::try_from(remaining.as_nanos().div_ceil(1_000_000)) + .unwrap_or(libc::c_int::MAX); + // SAFETY: event is one writable pollfd and pidfd remains open for the duration of poll. + unsafe { libc::poll(&mut event, 1, timeout_ms) }; + } loop { match child.try_wait() { Ok(Some(status)) => {