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
This commit is contained in:
zm-oai
2026-11-18 04:52:38 +00:00
committed by copyberry
parent 8e1fc8e059
commit b78fa4af86
4 changed files with 83 additions and 18 deletions
+4
View File
@@ -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 => {
@@ -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<WindowsSandboxProvisioningOutcome> {
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::<io::Error>().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<crate::SandboxProvisioningResponse> {
let Some(cause) = error.downcast_ref::<io::Error>() 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,
@@ -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());
}
@@ -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",