Files
codex/codex-rs/exec-server/src/local_process/tests/output_close_tests.rs
T
jif 30c23ded92 Avoid delaying sandboxed process exits after output closes (#65017)
## Why

The exec server can wait up to 20 ms for sandbox denial output even
when all output streams have already closed. If the last stream closes
during that wait, the close alone does not wake the exit watcher.

## What changed

Skip the wait when all streams are closed and wake the exit watcher
when the last stream closes. Register for notifications under the
process lock so the exit watcher can observe stream closure and
`process/read` can observe the exit as soon as it is published.

## Testing

Add paused-clock regression tests for streams closing before exit,
the final stream closing during the wait, and denial output arriving
after the first stream closes.

GitOrigin-RevId: 22715a0fbe4e582d82abcba8eab4802c42353f69
2026-12-01 15:43:47 +00:00

79 lines
2.8 KiB
Rust

#![cfg(test)]
use super::*;
use pretty_assertions::assert_eq;
use tokio::time::Instant;
#[tokio::test(start_paused = true)]
async fn exit_does_not_wait_when_output_already_closed() {
let backend = LocalProcess::default();
let process =
spawn_test_process(&backend, "closed-before-exit", SandboxType::MacosSeatbelt).await;
drop(process.stdout_tx);
drop(process.stderr_tx);
// With paused time both stream readers run to completion before the read times out.
let read = read_process_until_change(&backend, &process.process_id, /*after_seq*/ None);
assert!(timeout(Duration::from_millis(10), read).await.is_err());
let start = Instant::now();
process
.exit_tx
.expect("exit sender")
.send(0)
.expect("send exit");
let response = read_process_until_closed(&backend, &process.process_id).await;
assert_eq!(
(response.exit_code, start.elapsed()),
(Some(0), Duration::ZERO)
);
}
#[tokio::test(start_paused = true)]
async fn last_stream_closing_wakes_exit_and_preserves_denial() {
let backend = LocalProcess::default();
let mut process =
spawn_test_process(&backend, "closed-after-exit", SandboxType::MacosSeatbelt).await;
process
.stderr_tx
.send(b"Permission denied".to_vec())
.await
.expect("send stderr");
let output = read_process_until_change(&backend, &process.process_id, /*after_seq*/ None).await;
process.exit(/*exit_code*/ 1);
drop(process.stdout_tx);
let read = read_process_until_change(&backend, &process.process_id, Some(output.next_seq - 1));
assert!(timeout(Duration::from_millis(10), read).await.is_err());
let start = Instant::now();
drop(process.stderr_tx);
let response = read_process_until_closed(&backend, &process.process_id).await;
assert_eq!(
(response.sandbox_denied, start.elapsed()),
(true, Duration::ZERO)
);
}
#[tokio::test(start_paused = true)]
async fn first_stream_closing_still_allows_late_denial_on_the_other() {
let backend = LocalProcess::default();
let mut process = spawn_test_process(&backend, "late-denial", SandboxType::MacosSeatbelt).await;
process.exit(/*exit_code*/ 1);
let read = read_process_until_change(&backend, &process.process_id, /*after_seq*/ None);
assert!(timeout(Duration::from_millis(5), read).await.is_err());
drop(process.stdout_tx);
let read = read_process_until_change(&backend, &process.process_id, /*after_seq*/ None);
assert!(timeout(Duration::from_millis(5), read).await.is_err());
process
.stderr_tx
.send(b"Permission denied".to_vec())
.await
.expect("send stderr");
drop(process.stderr_tx);
let response = read_process_until_closed(&backend, &process.process_id).await;
assert!(response.sandbox_denied);
}