Files
codex/codex-rs/stdio-to-uds/src/lib.rs
Ahmed Ibrahim 09aa71adb7 Fix stdio-to-uds peer-close flake (#13882)
## What changed
- `codex-stdio-to-uds` now tolerates `NotConnected` when
`shutdown(Write)` happens after the peer has already closed.
- The socket test was rewritten to send stdin from a fixture file and to
read an exact request payload length instead of waiting on EOF timing.

## Why this fixes the flake
- This one exposed a real cross-platform runtime edge case: on macOS,
the peer can close first after a successful exchange, and
`shutdown(Write)` can report `NotConnected` even though the interaction
already succeeded.
- Treating that specific ordering as a harmless shutdown condition
removes the production-level false failure.
- The old test compounded the problem by depending on EOF timing, which
varies by platform and scheduler. Exact-length IO makes the test
deterministic and focused on the actual data exchange.

## Scope
- Production logic change with matching test rewrite.
2026-03-12 09:52:50 -07:00

57 lines
1.7 KiB
Rust

#![deny(clippy::print_stdout)]
use std::io;
use std::io::Write;
use std::net::Shutdown;
use std::path::Path;
use std::thread;
use anyhow::Context;
use anyhow::anyhow;
#[cfg(unix)]
use std::os::unix::net::UnixStream;
#[cfg(windows)]
use uds_windows::UnixStream;
/// Connects to the Unix Domain Socket at `socket_path` and relays data between
/// standard input/output and the socket.
pub fn run(socket_path: &Path) -> anyhow::Result<()> {
let mut stream = UnixStream::connect(socket_path)
.with_context(|| format!("failed to connect to socket at {}", socket_path.display()))?;
let mut reader = stream
.try_clone()
.context("failed to clone socket for reading")?;
let stdout_thread = thread::spawn(move || -> io::Result<()> {
let stdout = io::stdout();
let mut handle = stdout.lock();
io::copy(&mut reader, &mut handle)?;
handle.flush()?;
Ok(())
});
let stdin = io::stdin();
{
let mut handle = stdin.lock();
io::copy(&mut handle, &mut stream).context("failed to copy data from stdin to socket")?;
}
// The peer can close immediately after sending its response; in that race,
// half-closing our write side can report NotConnected on some platforms.
if let Err(err) = stream.shutdown(Shutdown::Write)
&& err.kind() != io::ErrorKind::NotConnected
{
return Err(err).context("failed to shutdown socket writer");
}
let stdout_result = stdout_thread
.join()
.map_err(|_| anyhow!("thread panicked while copying socket data to stdout"))?;
stdout_result.context("failed to copy data from socket to stdout")?;
Ok(())
}