From b78fa4af8636859f4acc4d57420e41e923c84cdd Mon Sep 17 00:00:00 2001 From: zm-oai Date: Wed, 18 Nov 2026 04:36:17 +0000 Subject: [PATCH] Distinguish Windows sandbox provisioning timeouts and pipe closures (#62192) ## Why When a registered Core provisioning request timed out or its pipe closed before a reply, setup reported only that the service was unavailable. That made the two failures indistinguishable in setup metrics. ## What changed Preserve the underlying I/O error and attach distinct setup error codes for timeouts and pipe closures. Registered Core still reports the service as unavailable and refuses helper fallback. `codex doctor` advises checking that the sandbox service is running and letting pending setup finish before retrying. ## Testing Add a unit test that checks error classification for timeouts, unexpected EOF, and broken pipes. It also checks that a non-registered request still returns an unavailable response on timeout and that permission errors receive neither new code. GitOrigin-RevId: fa6653fe5f9cafd10fc1562eb0763857a1bd628f --- codex-rs/cli/src/doctor/sandbox.rs | 4 ++ .../src/provisioning_client.rs | 54 +++++++++++++------ .../src/provisioning_client_tests.rs | 37 ++++++++++++- .../windows-sandbox-rs/src/setup_error.rs | 6 +++ 4 files changed, 83 insertions(+), 18 deletions(-) diff --git a/codex-rs/cli/src/doctor/sandbox.rs b/codex-rs/cli/src/doctor/sandbox.rs index 4e1d54bb19..94e2bb2fc9 100644 --- a/codex-rs/cli/src/doctor/sandbox.rs +++ b/codex-rs/cli/src/doctor/sandbox.rs @@ -305,6 +305,10 @@ pub(super) fn sandbox_check(config: &Config, arg0_paths: &Arg0DispatchPaths) -> | SetupErrorCode::HelperFirewallRuleVerifyFailed => { "ask IT to allow Codex sandbox rules in managed Windows Firewall policy" } + SetupErrorCode::RegisteredServiceTimedOut + | SetupErrorCode::RegisteredServicePipeClosed => { + "check that the Codex sandbox service is running and let pending setup finish before retrying" + } SetupErrorCode::OrchestratorPayloadSerializeFailed | SetupErrorCode::HelperRequestArgsFailed | SetupErrorCode::HelperUnknownError => { diff --git a/codex-rs/windows-sandbox-rs/src/provisioning_client.rs b/codex-rs/windows-sandbox-rs/src/provisioning_client.rs index 1590575fe7..96fb3da812 100644 --- a/codex-rs/windows-sandbox-rs/src/provisioning_client.rs +++ b/codex-rs/windows-sandbox-rs/src/provisioning_client.rs @@ -37,6 +37,8 @@ use windows_sys::Win32::System::Pipes::WaitNamedPipeW; use windows_sys::Win32::System::Services; use windows_sys::Win32::System::Services::SC_HANDLE; +const REGISTERED_SERVICE_UNAVAILABLE: &str = + "app runtime provisioning service is unavailable; refusing helper fallback"; const PROVISIONING_TIMEOUT: Duration = Duration::from_secs(120); const SERVICE_STARTUP_TIMEOUT: Duration = Duration::from_secs(5); @@ -185,7 +187,7 @@ fn provision( fn service_unavailable() -> anyhow::Result { if crate::registered_core_requested() { - bail!("app runtime provisioning service is unavailable; refusing helper fallback"); + bail!(REGISTERED_SERVICE_UNAVAILABLE); } Ok(WindowsSandboxProvisioningOutcome::Unavailable) } @@ -257,23 +259,13 @@ fn send_service_request( } let response = match exchange_request(&mut pipe, request, deadline) { Ok(response) => response, - Err(error) - if error.downcast_ref::().is_some_and(|error| { - matches!( - error.kind(), - io::ErrorKind::UnexpectedEof | io::ErrorKind::TimedOut - ) || matches!( - error.raw_os_error(), - Some(code) - if code == ERROR_BROKEN_PIPE as i32 - || code == ERROR_NO_DATA as i32 - || code == ERROR_PIPE_NOT_CONNECTED as i32 - ) - }) => - { - return Ok(crate::SandboxProvisioningResponse::Unavailable); + Err(error) => { + let registered_core = matches!( + &request.message, + crate::ProvisioningMessage::ProvisionSandboxRequest { payload } if payload.registered_core + ); + return service_exchange_error(error, registered_core); } - Err(error) => return Err(error), }; if matches!(&response, crate::SandboxProvisioningResponse::Error { message } if message == crate::SANDBOX_GROUP_CHANGED) @@ -284,6 +276,34 @@ fn send_service_request( Ok(response) } +fn service_exchange_error( + error: anyhow::Error, + registered_core: bool, +) -> anyhow::Result { + let Some(cause) = error.downcast_ref::() else { + return Err(error); + }; + let code = match cause.kind() { + io::ErrorKind::TimedOut => crate::SetupErrorCode::RegisteredServiceTimedOut, + io::ErrorKind::UnexpectedEof => crate::SetupErrorCode::RegisteredServicePipeClosed, + _ if matches!(cause.raw_os_error(), Some(code) if code == ERROR_BROKEN_PIPE as i32 + || code == ERROR_NO_DATA as i32 || code == ERROR_PIPE_NOT_CONNECTED as i32) => + { + crate::SetupErrorCode::RegisteredServicePipeClosed + } + _ => return Err(error), + }; + if !registered_core { + return Ok(crate::SandboxProvisioningResponse::Unavailable); + } + Err(error + .context(crate::SetupFailure::new( + code, + "sandbox provisioning transport failed", + )) + .context(REGISTERED_SERVICE_UNAVAILABLE)) +} + fn exchange_request( pipe: &mut File, request: &crate::FramedProvisioningMessage, diff --git a/codex-rs/windows-sandbox-rs/src/provisioning_client_tests.rs b/codex-rs/windows-sandbox-rs/src/provisioning_client_tests.rs index f08c29f444..da5b7aed1c 100644 --- a/codex-rs/windows-sandbox-rs/src/provisioning_client_tests.rs +++ b/codex-rs/windows-sandbox-rs/src/provisioning_client_tests.rs @@ -1,4 +1,4 @@ -//! Exercises process-local workload-identity routing without contacting the service. +//! Exercises provisioning client routing and transport failures without contacting the service. use super::WindowsSandboxProvisioningOutcome; use super::provision_windows_sandbox_via_service; @@ -68,3 +68,38 @@ fn workload_identity_child() -> Result<()> { } Ok(()) } + +#[test] +fn registered_setup_retains_the_transport_cause_for_metrics() { + use crate::SetupErrorCode; + use std::io; + use windows_sys::Win32::Foundation::ERROR_BROKEN_PIPE; + + for (cause, code) in [ + ( + io::ErrorKind::TimedOut.into(), + SetupErrorCode::RegisteredServiceTimedOut, + ), + ( + io::ErrorKind::UnexpectedEof.into(), + SetupErrorCode::RegisteredServicePipeClosed, + ), + ( + io::Error::from_raw_os_error(ERROR_BROKEN_PIPE as i32), + SetupErrorCode::RegisteredServicePipeClosed, + ), + ] { + let error = anyhow::Error::new(cause).context("wait for sandbox provisioning response"); + let error = super::service_exchange_error(error, /*registered_core*/ true).unwrap_err(); + assert_eq!(error.to_string(), super::REGISTERED_SERVICE_UNAVAILABLE); + assert_eq!(crate::extract_setup_failure(&error).unwrap().code, code); + } + let timeout = io::Error::from(io::ErrorKind::TimedOut); + assert!(matches!( + super::service_exchange_error(timeout.into(), /*registered_core*/ false).unwrap(), + crate::SandboxProvisioningResponse::Unavailable + )); + let denied = io::Error::from(io::ErrorKind::PermissionDenied); + let error = super::service_exchange_error(denied.into(), /*registered_core*/ true).unwrap_err(); + assert!(crate::extract_setup_failure(&error).is_none()); +} diff --git a/codex-rs/windows-sandbox-rs/src/setup_error.rs b/codex-rs/windows-sandbox-rs/src/setup_error.rs index d9104602bc..b4ae80a929 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_error.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_error.rs @@ -33,6 +33,10 @@ pub enum SetupErrorCode { OrchestratorHelperReportReadFailed, /// Helper exited successfully before setup completed. OrchestratorHelperIncomplete, + /// Registered Core's provisioning request reached its transport deadline. + RegisteredServiceTimedOut, + /// The provisioning pipe closed before registered Core received a reply. + RegisteredServicePipeClosed, // Helper (elevated process) failures. /// Helper failed while validating or decoding the request payload. HelperRequestArgsFailed, @@ -86,6 +90,8 @@ impl SetupErrorCode { Self::OrchestratorHelperExitNonzero => "orchestrator_helper_exit_nonzero", Self::OrchestratorHelperReportReadFailed => "orchestrator_helper_report_read_failed", Self::OrchestratorHelperIncomplete => "orchestrator_helper_incomplete", + Self::RegisteredServiceTimedOut => "registered_service_timed_out", + Self::RegisteredServicePipeClosed => "registered_service_pipe_closed", Self::HelperRequestArgsFailed => "helper_request_args_failed", Self::HelperSandboxDirCreateFailed => "helper_sandbox_dir_create_failed", Self::HelperLogFailed => "helper_log_failed",