Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Add timeout-aware exec-server configuration reads (#52679)

Henry Levy6e093c608122
diff --git a/codex-rs/exec-server/src/client.rs b/codex-rs/exec-server/src/client.rs
index 0369bb7185..2b874bc11f 100644
--- a/codex-rs/exec-server/src/client.rs
+++ b/codex-rs/exec-server/src/client.rs
@@ -676,6 +676,10 @@ pub enum ExecServerError {
WebSocketConfiguration(String),
#[error("timed out waiting for exec-server initialize handshake after {timeout:?}")]
InitializeTimedOut { timeout: Duration },
+ #[error(
+ "exec-server protocol error: timed out waiting for exec-server `{method}` response after {timeout:?}"
+ )]
+ RpcTimedOut { method: String, timeout: Duration },
#[error(transparent)]
ApplicationNetworkPolicy(#[from] codex_http_client::NetworkPolicyDenied),
#[error("exec-server transport closed")]
@@ -845,28 +849,11 @@ impl ExecServerClient {
/// Fetches executor metadata over RPC without reading or updating the cache.
// TODO: Remove after app-server migrates off this call.
pub async fn force_environment_info(&self) -> Result<EnvironmentInfo, ExecServerError> {
- let rpc_client = self.rpc_client().await?;
- // Bound sending as well as receiving: a stuck transport can fill the outbound queue.
- let result = timeout(
- ENVIRONMENT_INFO_TIMEOUT,
- rpc_client.call(ENVIRONMENT_INFO_METHOD, &()),
- )
- .await;
- match result {
- Ok(result) => self.map_rpc_call_result(result),
- Err(_) => {
- let error = ExecServerError::from(RpcCallError::TimedOut {
- method: ENVIRONMENT_INFO_METHOD.to_string(),
- timeout: ENVIRONMENT_INFO_TIMEOUT,
- });
- // Retire only the connection we probed; recovery ignores a stale client.
- rpc_client.close_transport().await;
- self.inner.request_recovery(rpc_client, error.to_string());
- Err(error)
- }
- }
+ self.call_with_timeout_recovery(ENVIRONMENT_INFO_METHOD, &(), ENVIRONMENT_INFO_TIMEOUT)
+ .await
}
+ /// Reads configuration without imposing an additional RPC timeout.
pub async fn read_environment_config(
&self,
params: EnvironmentConfigReadParams,
@@ -874,6 +861,17 @@ impl ExecServerClient {
self.call(ENVIRONMENT_CONFIG_READ_METHOD, &params).await
}
+ /// Bounds the RPC after connecting, recovering reconnectable transports on timeout.
+ /// Transports without a reconnect strategy remain open after a timeout.
+ pub async fn read_environment_config_with_timeout(
+ &self,
+ params: EnvironmentConfigReadParams,
+ rpc_timeout: Duration,
+ ) -> Result<EnvironmentConfigReadResponse, ExecServerError> {
+ self.call_with_timeout_recovery(ENVIRONMENT_CONFIG_READ_METHOD, &params, rpc_timeout)
+ .await
+ }
+
pub async fn environment_status(&self) -> Result<EnvironmentStatus, ExecServerError> {
// Health checks only reuse an existing RPC connection and never initiate recovery.
let rpc_client = self.rpc_client_without_recovery()?;
@@ -1343,6 +1341,35 @@ impl ExecServerClient {
self.call_rpc(&rpc_client, method, params).await
}
+ async fn call_with_timeout_recovery<P, T>(
+ &self,
+ method: &str,
+ params: &P,
+ call_timeout: Duration,
+ ) -> Result<T, ExecServerError>
+ where
+ P: serde::Serialize,
+ T: serde::de::DeserializeOwned,
+ {
+ let rpc_client = self.rpc_client().await?;
+ // Bound sending as well as receiving: a stuck transport can fill the outbound queue.
+ match timeout(call_timeout, rpc_client.call(method, params)).await {
+ Ok(result) => self.map_rpc_call_result(result),
+ Err(_) => {
+ let error = ExecServerError::RpcTimedOut {
+ method: method.to_string(),
+ timeout: call_timeout,
+ };
+ if self.inner.reconnect_strategy.is_some() {
+ // Retire only the connection we probed; recovery ignores a stale client.
+ rpc_client.close_transport().await;
+ self.inner.request_recovery(rpc_client, error.to_string());
+ }
+ Err(error)
+ }
+ }
+ }
+
async fn call_rpc<P, T>(
&self,
rpc_client: &Arc<RpcClient>,
@@ -1413,9 +1440,7 @@ impl From<RpcCallError> for ExecServerError {
code: error.code,
message: error.message,
},
- RpcCallError::TimedOut { method, timeout } => Self::Protocol(format!(
- "timed out waiting for exec-server `{method}` response after {timeout:?}"
- )),
+ RpcCallError::TimedOut { method, timeout } => Self::RpcTimedOut { method, timeout },
RpcCallError::PendingRequestLimitExceeded { limit } => Self::Protocol(format!(
"exec-server has reached its limit of {limit} pending requests"
)),
@@ -3443,5 +3468,6 @@ mod tests {
server.await.expect("server task should finish");
}
+ mod config_timeout_tests;
mod network_policy_tests;
}
diff --git a/codex-rs/exec-server/src/client/connection_failure.rs b/codex-rs/exec-server/src/client/connection_failure.rs
index 5265d610c2..3dd54f7925 100644
--- a/codex-rs/exec-server/src/client/connection_failure.rs
+++ b/codex-rs/exec-server/src/client/connection_failure.rs
@@ -37,6 +37,7 @@ impl ExecServerError {
| Self::WebSocketConnectTimeout { .. }
| Self::WebSocketConfiguration(_)
| Self::InitializeTimedOut { .. }
+ | Self::RpcTimedOut { .. }
| Self::Closed
| Self::Disconnected(_)
| Self::ProvisioningFailed(_)
diff --git a/codex-rs/exec-server/src/client/tests/config_timeout_tests.rs b/codex-rs/exec-server/src/client/tests/config_timeout_tests.rs
new file mode 100644
index 0000000000..2c894403da
--- /dev/null
+++ b/codex-rs/exec-server/src/client/tests/config_timeout_tests.rs
@@ -0,0 +1,144 @@
+//! Config-read deadlines must preserve non-reconnectable stdio connections.
+
+use super::read_jsonrpc_line;
+use super::write_jsonrpc_line;
+use crate::ExecServerClient;
+use crate::ExecServerClientConnectOptions;
+use crate::ExecServerError;
+use crate::connection::JsonRpcConnection;
+use crate::protocol::ENVIRONMENT_CONFIG_READ_METHOD;
+use crate::protocol::EnvironmentConfigLayerStack;
+use crate::protocol::EnvironmentConfigReadParams;
+use crate::protocol::EnvironmentConfigReadResponse;
+use crate::protocol::INITIALIZE_METHOD;
+use crate::protocol::INITIALIZED_METHOD;
+use crate::protocol::InitializeResponse;
+use codex_exec_server_protocol::JSONRPCMessage;
+use codex_exec_server_protocol::JSONRPCResponse;
+use codex_utils_path_uri::PathUri;
+use pretty_assertions::assert_eq;
+use tokio::io::AsyncBufReadExt;
+use tokio::io::BufReader;
+use tokio::io::duplex;
+use tokio::time::Duration;
+
+#[tokio::test(start_paused = true)]
+async fn config_read_timeout_keeps_stdio_connection_usable() {
+ let (client_stdin, server_reader) = duplex(4096);
+ let (mut server_writer, client_stdout) = duplex(4096);
+ let mut lines = BufReader::new(server_reader).lines();
+ let connect = ExecServerClient::connect(
+ JsonRpcConnection::from_stdio(client_stdout, client_stdin, "config-timeout".to_string()),
+ ExecServerClientConnectOptions::default(),
+ );
+ tokio::pin!(connect);
+ assert!(futures::poll!(connect.as_mut()).is_pending());
+ let initialize = match read_jsonrpc_line(&mut lines).await {
+ JSONRPCMessage::Request(request) if request.method == INITIALIZE_METHOD => request,
+ other => panic!("expected initialize request, got {other:?}"),
+ };
+ write_jsonrpc_line(
+ &mut server_writer,
+ JSONRPCMessage::Response(JSONRPCResponse {
+ id: initialize.id,
+ result: serde_json::to_value(InitializeResponse {
+ session_id: "config-timeout".to_string(),
+ environment_info: None,
+ })
+ .expect("initialize response should serialize"),
+ }),
+ )
+ .await;
+ let client = connect.await.expect("stdio client should connect");
+ assert!(matches!(
+ read_jsonrpc_line(&mut lines).await,
+ JSONRPCMessage::Notification(notification) if notification.method == INITIALIZED_METHOD
+ ));
+
+ let cwd = PathUri::from_host_native_path(std::env::current_dir().expect("current directory"))
+ .expect("current directory URI");
+ let params = EnvironmentConfigReadParams {
+ cwd: cwd.clone(),
+ config_paths: vec![vec!["features".to_string()]],
+ requirements_paths: Vec::new(),
+ };
+ let mut expected = EnvironmentConfigReadResponse {
+ user_home_dir: None,
+ codex_home_dir: cwd,
+ hostname: Some("original".to_string()),
+ config: EnvironmentConfigLayerStack {
+ layers: Vec::new(),
+ cloud_insertion_index: 0,
+ },
+ requirements: EnvironmentConfigLayerStack {
+ layers: Vec::new(),
+ cloud_insertion_index: 0,
+ },
+ };
+
+ // Model inline dispatch by holding valid work while the timed read queues behind it.
+ let original = client.read_environment_config(params.clone());
+ tokio::pin!(original);
+ assert!(futures::poll!(original.as_mut()).is_pending());
+ let original_request = match read_jsonrpc_line(&mut lines).await {
+ JSONRPCMessage::Request(request) if request.method == ENVIRONMENT_CONFIG_READ_METHOD => {
+ request
+ }
+ other => panic!("expected original config read, got {other:?}"),
+ };
+ let rpc_timeout = Duration::from_secs(1);
+ let timed = client.read_environment_config_with_timeout(params.clone(), rpc_timeout);
+ tokio::pin!(timed);
+ assert!(futures::poll!(timed.as_mut()).is_pending());
+ tokio::time::advance(rpc_timeout).await;
+ assert!(matches!(
+ timed.await,
+ Err(ExecServerError::RpcTimedOut { method, timeout })
+ if method == ENVIRONMENT_CONFIG_READ_METHOD && timeout == rpc_timeout
+ ));
+ assert!(
+ futures::poll!(original.as_mut()).is_pending(),
+ "a queued config-read timeout must not disconnect valid in-flight work"
+ );
+
+ write_jsonrpc_line(
+ &mut server_writer,
+ JSONRPCMessage::Response(JSONRPCResponse {
+ id: original_request.id,
+ result: serde_json::to_value(&expected).expect("config response should serialize"),
+ }),
+ )
+ .await;
+ assert_eq!(
+ original.await.expect("original read should finish"),
+ expected
+ );
+
+ // Finish the expired request, then serve a fresh read over the same connection.
+ let retry = client.read_environment_config_with_timeout(params, rpc_timeout);
+ tokio::pin!(retry);
+ assert!(futures::poll!(retry.as_mut()).is_pending());
+ for hostname in ["late", "fresh"] {
+ let request = match read_jsonrpc_line(&mut lines).await {
+ JSONRPCMessage::Request(request)
+ if request.method == ENVIRONMENT_CONFIG_READ_METHOD =>
+ {
+ request
+ }
+ other => panic!("expected queued config read, got {other:?}"),
+ };
+ expected.hostname = Some(hostname.to_string());
+ write_jsonrpc_line(
+ &mut server_writer,
+ JSONRPCMessage::Response(JSONRPCResponse {
+ id: request.id,
+ result: serde_json::to_value(&expected).expect("config response should serialize"),
+ }),
+ )
+ .await;
+ }
+ assert_eq!(
+ retry.await.expect("stdio connection should be reusable"),
+ expected
+ );
+}
diff --git a/codex-rs/exec-server/src/client_error.rs b/codex-rs/exec-server/src/client_error.rs
index e77d56a0f7..97487c21b0 100644
--- a/codex-rs/exec-server/src/client_error.rs
+++ b/codex-rs/exec-server/src/client_error.rs
@@ -28,6 +28,7 @@ impl ExecServerError {
Self::Closed
| Self::Disconnected(_)
| Self::WebSocketConnectTimeout { .. }
+ | Self::RpcTimedOut { .. }
| Self::InitializeTimedOut { .. } => true,
Self::WebSocketConnect { source, .. } => match source {
WebSocketError::ConnectionClosed
diff --git a/codex-rs/exec-server/src/environment.rs b/codex-rs/exec-server/src/environment.rs
index baa0f22049..9ffbb0b838 100644
--- a/codex-rs/exec-server/src/environment.rs
+++ b/codex-rs/exec-server/src/environment.rs
@@ -5,6 +5,7 @@ use std::collections::HashSet;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;
+use std::time::Duration;
use arc_swap::ArcSwapOption;
use codex_config::ScopedSkillsConfig;
@@ -1016,6 +1017,26 @@ impl Environment {
}
}
+ /// Reads configuration with a timeout for the remote RPC once connected.
+ /// Remote transports without a reconnect strategy remain open after a timeout.
+ /// Local reads retain their existing behavior without a timeout.
+ pub async fn read_environment_config_with_timeout(
+ &self,
+ params: EnvironmentConfigReadParams,
+ rpc_timeout: Duration,
+ ) -> Result<EnvironmentConfigReadResponse, ExecServerError> {
+ match &self.remote_client {
+ Some(client) => {
+ client
+ .get()
+ .await?
+ .read_environment_config_with_timeout(params, rpc_timeout)
+ .await
+ }
+ None => self.read_environment_config(params).await,
+ }
+ }
+
/// Discovers plugin and skill manifests through the environment's high-level discovery API.
pub async fn discover_capability_roots(
&self,