From ab1ad9299b53642517c77e1f617f63d5f9273e43 Mon Sep 17 00:00:00 2001 From: richardopenai Date: Wed, 18 Nov 2026 23:29:10 +0000 Subject: [PATCH] Skip the sandbox denial wait when unified exec output closes (#62382) ## Why The sandbox denial check can spend 20 ms waiting for more output even after the stream has closed and all denial context is available. ## What changed Classify the buffered output immediately if the stream is already closed. While the stream is open, wait up to 20 ms for new output or closure. Register for the closure notification before rechecking the stream state so closure during setup wakes the check. ## Testing Paused-clock tests cover output closed before or during the check, preservation of the timeout for open streams, and classification of sandboxed and unsandboxed failures. The remote process-event suite adds a direct sandbox denial case with a `process/closed` event. GitOrigin-RevId: 1f3029055d70ff4cd63939564872fe5d37240a33 --- codex-rs/core/src/unified_exec/process.rs | 21 ++++- .../core/src/unified_exec/process_tests.rs | 92 +++++++++++++++++++ .../suite/unified_exec_process_events.rs | 25 +++-- 3 files changed, 126 insertions(+), 12 deletions(-) diff --git a/codex-rs/core/src/unified_exec/process.rs b/codex-rs/core/src/unified_exec/process.rs index f9afaaa41d..6443dfc86c 100644 --- a/codex-rs/core/src/unified_exec/process.rs +++ b/codex-rs/core/src/unified_exec/process.rs @@ -303,11 +303,22 @@ impl UnifiedExecProcess { } pub(super) async fn check_for_sandbox_denial(&self) -> Result<(), UnifiedExecError> { - let _ = tokio::time::timeout( - Duration::from_millis(20), - self.output.output_notify.notified(), - ) - .await; + // A closed output stream cannot provide any more denial context. + if !self.output.output_closed.load(Ordering::Acquire) { + let closed = self.output.output_closed_notify.notified(); + tokio::pin!(closed); + closed.as_mut().enable(); + // Recheck after registering so closure cannot be lost before we wait. + if !self.output.output_closed.load(Ordering::Acquire) { + let _ = tokio::time::timeout(Duration::from_millis(20), async { + tokio::select! { + _ = self.output.output_notify.notified() => {} + _ = &mut closed => {} + } + }) + .await; + } + } let aggregated = self.snapshot_output().await; let aggregated_text = String::from_utf8_lossy(&aggregated); diff --git a/codex-rs/core/src/unified_exec/process_tests.rs b/codex-rs/core/src/unified_exec/process_tests.rs index 29dabeee2d..d4b341f79b 100644 --- a/codex-rs/core/src/unified_exec/process_tests.rs +++ b/codex-rs/core/src/unified_exec/process_tests.rs @@ -1,6 +1,9 @@ +#![cfg(test)] + use super::process::OutputDrainPolicy; use super::process::UnifiedExecProcess; use crate::unified_exec::UnifiedExecError; +use codex_exec_server::ExecOutputStream; use codex_exec_server::ExecProcess; use codex_exec_server::ExecProcessEventReceiver; use codex_exec_server::ExecProcessFuture; @@ -214,3 +217,92 @@ async fn remote_process_preserves_executor_sandbox_type() { assert_eq!(process.sandbox_type(), sandbox_type); } } + +#[tokio::test(start_paused = true)] +async fn closed_output_is_classified_without_waiting_for_more_output() { + use codex_sandboxing::SandboxType; + for sandbox_type in [SandboxType::None, SandboxType::LinuxSeccomp] { + let process = remote_process( + WriteStatus::Accepted, + /*terminate_error*/ None, + sandbox_type, + ) + .await; + let output = process.output_handles(); + output + .output_buffer + .lock() + .await + .push_chunk(ExecOutputStream::Stderr, b"permission denied"); + let closed = output.output_closed_notify.notified(); + tokio::pin!(closed); + closed.as_mut().enable(); + process.terminate_confirmed().await.expect("terminate"); + closed.await; + + let start = tokio::time::Instant::now(); + let result = process.check_for_sandbox_denial().await; + assert_eq!(start.elapsed(), std::time::Duration::ZERO); + if sandbox_type == SandboxType::None { + result.expect("unsandboxed failure is not a sandbox denial"); + } else { + assert!(matches!( + result, + Err(UnifiedExecError::SandboxDenied { .. }) + )); + } + } +} + +#[tokio::test(start_paused = true)] +async fn output_closing_during_denial_check_wakes_without_timeout() { + use codex_sandboxing::SandboxType; + for sandbox_type in [SandboxType::None, SandboxType::LinuxSeccomp] { + let process = remote_process( + WriteStatus::Accepted, + /*terminate_error*/ None, + sandbox_type, + ) + .await; + process + .output_handles() + .output_buffer + .lock() + .await + .push_chunk(ExecOutputStream::Stderr, b"permission denied"); + + let check = process.check_for_sandbox_denial(); + tokio::pin!(check); + let start = tokio::time::Instant::now(); + assert!(futures::poll!(check.as_mut()).is_pending()); + process.terminate_confirmed().await.expect("terminate"); + + let result = check.await; + assert_eq!(start.elapsed(), std::time::Duration::ZERO); + if sandbox_type == SandboxType::None { + result.expect("unsandboxed failure is not a sandbox denial"); + } else { + assert!(matches!( + result, + Err(UnifiedExecError::SandboxDenied { .. }) + )); + } + } +} + +#[tokio::test(start_paused = true)] +async fn open_output_preserves_denial_context_timeout() { + let process = remote_process( + WriteStatus::Accepted, + /*terminate_error*/ None, + codex_sandboxing::SandboxType::None, + ) + .await; + + let start = tokio::time::Instant::now(); + process + .check_for_sandbox_denial() + .await + .expect("running process is not a sandbox denial"); + assert_eq!(start.elapsed(), std::time::Duration::from_millis(20)); +} diff --git a/codex-rs/core/tests/suite/unified_exec_process_events.rs b/codex-rs/core/tests/suite/unified_exec_process_events.rs index d59e777d0b..760dcda85f 100644 --- a/codex-rs/core/tests/suite/unified_exec_process_events.rs +++ b/codex-rs/core/tests/suite/unified_exec_process_events.rs @@ -92,7 +92,7 @@ enum PushedExecScenario { CodexPathDisabledFeature, CodexPathWithExplicitOverride, UnusableCodexPath, - DirectDenied, + DirectDenied { output_closed: bool }, DenyWin, DenyWinUnchanged, DenyOldLinux, @@ -617,7 +617,7 @@ async fn serve_exec_with_pushed_events( PushedExecScenario::RejectedLongWindowsDangerousCommand => { panic!("dangerous command must not reach the executor") } - PushedExecScenario::DirectDenied => { + PushedExecScenario::DirectDenied { output_closed } => { send_exec_server_json( &mut websocket, json!({ @@ -631,6 +631,16 @@ async fn serve_exec_with_pushed_events( }), ) .await; + if output_closed { + send_exec_server_json( + &mut websocket, + json!({ + "method": "process/closed", + "params": { "processId": &process_id, "seq": 2 } + }), + ) + .await; + } } PushedExecScenario::SandboxedInterceptedPatch | PushedExecScenario::SandboxedDirectPatch @@ -749,12 +759,12 @@ async fn serve_exec_with_pushed_events( "failure": null, "sandboxDenied": false, }), - PushedExecScenario::DirectDenied => json!({ + PushedExecScenario::DirectDenied { output_closed } => json!({ "chunks": [], - "nextSeq": 2, + "nextSeq": if output_closed { 3 } else { 2 }, "exited": true, "exitCode": 1, - "closed": false, + "closed": output_closed, "failure": null, "sandboxDenied": true, }), @@ -862,7 +872,8 @@ async fn serve_exec_with_pushed_events( #[test_case(PushedExecScenario::CodexPathDisabledFeature, ManagedNetworkScenario::None, false ; "disabled_codex_path_preserves_original_login_command")] #[test_case(PushedExecScenario::CodexPathWithExplicitOverride, ManagedNetworkScenario::None, false ; "explicit_path_override_preserves_original_login_command")] #[test_case(PushedExecScenario::UnusableCodexPath, ManagedNetworkScenario::None, false ; "unusable_codex_path_preserves_requested_login_shell")] -#[test_case(PushedExecScenario::DirectDenied, ManagedNetworkScenario::None, false ; "direct_sandbox_denial")] +#[test_case(PushedExecScenario::DirectDenied { output_closed: false }, ManagedNetworkScenario::None, false ; "direct_sandbox_denial")] +#[test_case(PushedExecScenario::DirectDenied { output_closed: true }, ManagedNetworkScenario::None, false ; "direct_sandbox_denial_with_closed_output")] #[test_case(PushedExecScenario::LegacyExit, ManagedNetworkScenario::None, false ; "legacy_exit_metadata")] #[test_case(PushedExecScenario::ReplayGap, ManagedNetworkScenario::None, false ; "truncated_event_replay")] #[test_case(PushedExecScenario::Complete, ManagedNetworkScenario::Enabled { policy_callbacks: true }, false ; "managed_network_uses_executor_proxy_launch")] @@ -1941,7 +1952,7 @@ timeout = 900 PushedExecScenario::RejectedLongWindowsDangerousCommand => { unreachable!("dangerous command returned early") } - PushedExecScenario::DirectDenied => { + PushedExecScenario::DirectDenied { .. } => { assert!(!saw_exec_command_begin); assert!(output.contains("Process exited with code 1")); assert_eq!(process_read_requests, 0, "unexpected compatibility read");