Retry transient registry failures during initial exec connection (#39777)

## What changed

- Retry transient environment-registry failures while opening the initial Noise rendezvous connection, including timeouts, interrupted response bodies, retryable HTTP statuses, and temporarily offline environments.
- Bound registry attempts with per-request and overall timeouts, exponential backoff, and a retry limit while returning permanent registry errors immediately.
- Preserve the existing one-time bundle refresh after an unauthorized WebSocket handshake, with a fresh registry retry window.
- Treat malformed complete JSON responses as terminal rather than retryable transport failures.

## Testing

- Cover transient and permanent registry responses, stalled requests, retry limits and deadlines, interrupted bodies, malformed JSON, unauthorized bundle refresh, and session-resume behavior.

GitOrigin-RevId: 5929274103702ad477e7a638ab489319a43a93fa
This commit is contained in:
richardopenai authored and copyberry committed 2026-08-20 18:48:17 +00:00
1 parent 5cada24434
commit 5663754f62
6 files changed
+587 -50

No files matched your search

+66 -16
View File
@@ -5,7 +5,10 @@ use std::time::Duration;
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::process::Command;
use tokio::time::Instant;
use tokio::time::sleep;
use tokio::time::timeout;
use tokio::time::timeout_at;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tracing::debug;
use tracing::warn;
@@ -18,6 +21,8 @@ use codex_websocket_client::WebSocketTlsMode;
use crate::ExecServerClient;
use crate::ExecServerError;
use crate::client::is_retryable_registry_error;
use crate::client::registry_recovery_retry_delay;
use crate::client_api::DEFAULT_REMOTE_EXEC_SERVER_CONNECT_TIMEOUT;
use crate::client_api::DEFAULT_REMOTE_EXEC_SERVER_INITIALIZE_TIMEOUT;
use crate::client_api::ExecServerClientConnectOptions;
@@ -37,6 +42,9 @@ use crate::relay::harness_connection_from_websocket;
use crate::trace_context::current_rendezvous_headers;
const ENVIRONMENT_CLIENT_NAME: &str = "codex-environment";
const INITIAL_REGISTRY_MAX_RETRIES: u32 = 4;
const INITIAL_REGISTRY_REQUEST_TIMEOUT: Duration = Duration::from_secs(6);
const INITIAL_REGISTRY_OPERATION_TIMEOUT: Duration = Duration::from_secs(14);
/// Reopens the transport for one logical exec-server client session.
///
@@ -199,23 +207,65 @@ impl ExecServerClient {
http_client_factory: http_client_factory.clone(),
})
};
let bundle = provider.connect_bundle(identity.public_key()).await?;
match open_connection(bundle).await {
Err(error)
if matches!(
&error,
ExecServerError::WebSocketConnect { source, .. }
if matches!(
source,
tokio_tungstenite::tungstenite::Error::Http(response)
if response.status().as_u16() == 401
)
) =>
{
let bundle = provider.connect_bundle(identity.public_key()).await?;
open_connection(bundle).await
let mut deadline = Instant::now() + INITIAL_REGISTRY_OPERATION_TIMEOUT;
let retry_key = uuid::Uuid::new_v4().to_string();
let mut retries = 0;
let mut refreshed_unauthorized_bundle = false;
let connect_bundle = || async {
timeout(
INITIAL_REGISTRY_REQUEST_TIMEOUT,
provider.connect_bundle(identity.public_key()),
)
.await
.unwrap_or_else(|_| {
Err(ExecServerError::EnvironmentRegistryRequest(
codex_http_client::RouteAwareRequestError::Timeout,
))
})
};
let mut result = connect_bundle().await;
loop {
let bundle = match result {
Ok(bundle) => bundle,
Err(error)
if is_retryable_registry_error(&error)
&& retries < INITIAL_REGISTRY_MAX_RETRIES =>
{
// Session resumption owns its separate recovery deadline.
let delay = registry_recovery_retry_delay(&retry_key, retries);
retries += 1;
result = match timeout_at(deadline, async {
sleep(delay).await;
connect_bundle().await
})
.await
{
Ok(result) => result,
Err(_) => return Err(error),
};
continue;
}
Err(error) => return Err(error),
};
match open_connection(bundle).await {
Err(error)
if !refreshed_unauthorized_bundle
&& matches!(
&error,
ExecServerError::WebSocketConnect { source, .. }
if matches!(
source,
tokio_tungstenite::tungstenite::Error::Http(response)
if response.status().as_u16() == 401
)
) =>
{
refreshed_unauthorized_bundle = true;
deadline = Instant::now() + INITIAL_REGISTRY_OPERATION_TIMEOUT;
result = connect_bundle().await;
}
result => return result,
}
result => result,
}
}