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
This commit is contained in:
richardopenai
2026-11-18 23:42:25 +00:00
committed by copyberry
parent 8e9746b19d
commit ab1ad9299b
3 changed files with 126 additions and 12 deletions
+16 -5
View File
@@ -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);
@@ -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));
}
@@ -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");