mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
Add opt-in concurrent exec-server request dispatch (#36987)
## Why Sequential dispatch lets a long-running request block unrelated health checks and cleanup on the same connection. ## What changed - Add `--concurrent-requests <COUNT>` for local and remote exec-server connections, while retaining sequential dispatch when the option is omitted or set to `1`. - Preserve handshake ordering before enabling concurrent dispatch. - Reserve separate capacity for status, signal, terminate, and close requests so they remain responsive when ordinary request capacity is saturated. - Drain queued client responses during disconnect and cancel outstanding request tasks during connection shutdown. ## Testing - Cover CLI parsing and concurrency-limit validation. - Verify default sequential behavior, pipelined handshake ordering, concurrent request progress, control-request responsiveness, and disconnect handling. GitOrigin-RevId: 48e4b092e318204ed635543f01f9ee0e7df095fc
This commit is contained in:
committed by
copyberry
parent
1d952f027e
commit
eeae88d8a6
@@ -189,8 +189,10 @@ pub use remote::run_remote_environment_until_shutdown;
|
||||
pub use resolved_capability::ResolvedSelectedCapabilityRoot;
|
||||
pub use resolved_capability::SelectedCapabilityRootsStatus;
|
||||
pub use runtime_paths::ExecServerRuntimePaths;
|
||||
pub use server::ConcurrentRequestLimit;
|
||||
pub use server::DEFAULT_LISTEN_URL;
|
||||
pub use server::ExecServerListenUrlParseError;
|
||||
pub use server::RequestDispatchMode;
|
||||
pub use server::run_main;
|
||||
pub use server::run_main_with_telemetry;
|
||||
pub use telemetry::ExecServerTelemetry;
|
||||
|
||||
@@ -43,6 +43,7 @@ use crate::noise_relay::noise_relay_websocket_config;
|
||||
use crate::relay::HarnessKeyValidator;
|
||||
use crate::relay::run_multiplexed_environment;
|
||||
use crate::server::ConnectionProcessor;
|
||||
use crate::server::RequestDispatchMode;
|
||||
use crate::trace_context::current_trace_context_headers;
|
||||
|
||||
const ERROR_BODY_PREVIEW_BYTES: usize = 4096;
|
||||
@@ -464,6 +465,7 @@ pub struct RemoteEnvironmentConfig {
|
||||
pub base_url: String,
|
||||
pub environment_id: String,
|
||||
pub name: String,
|
||||
pub request_dispatch_mode: RequestDispatchMode,
|
||||
auth_provider: SharedAuthProvider,
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
@@ -475,6 +477,7 @@ impl std::fmt::Debug for RemoteEnvironmentConfig {
|
||||
.field("base_url", &self.base_url)
|
||||
.field("environment_id", &self.environment_id)
|
||||
.field("name", &self.name)
|
||||
.field("request_dispatch_mode", &self.request_dispatch_mode)
|
||||
.field("auth_provider", &"<redacted>")
|
||||
.finish()
|
||||
}
|
||||
@@ -492,6 +495,7 @@ impl RemoteEnvironmentConfig {
|
||||
base_url,
|
||||
environment_id,
|
||||
name: "codex-exec-server".to_string(),
|
||||
request_dispatch_mode: RequestDispatchMode::Inline,
|
||||
auth_provider,
|
||||
telemetry: ExecServerTelemetry::default(),
|
||||
http_client_factory,
|
||||
@@ -539,6 +543,7 @@ where
|
||||
runtime_paths,
|
||||
config.telemetry.clone(),
|
||||
config.http_client_factory.clone(),
|
||||
config.request_dispatch_mode,
|
||||
);
|
||||
|
||||
let result = {
|
||||
|
||||
@@ -9,6 +9,8 @@ mod transport;
|
||||
|
||||
pub(crate) use handler::ExecServerHandler;
|
||||
pub(crate) use processor::ConnectionProcessor;
|
||||
pub use request_dispatcher::ConcurrentRequestLimit;
|
||||
pub use request_dispatcher::RequestDispatchMode;
|
||||
pub use transport::DEFAULT_LISTEN_URL;
|
||||
pub use transport::ExecServerListenUrlParseError;
|
||||
|
||||
@@ -26,6 +28,7 @@ pub async fn run_main(
|
||||
runtime_paths,
|
||||
ExecServerTelemetry::default(),
|
||||
http_client_factory,
|
||||
RequestDispatchMode::Inline,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -40,8 +43,16 @@ pub async fn run_main_with_telemetry(
|
||||
runtime_paths: ExecServerRuntimePaths,
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
request_dispatch_mode: RequestDispatchMode,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
transport::run_transport(listen_url, runtime_paths, telemetry, http_client_factory).await
|
||||
transport::run_transport(
|
||||
listen_url,
|
||||
runtime_paths,
|
||||
telemetry,
|
||||
http_client_factory,
|
||||
request_dispatch_mode,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -78,6 +89,7 @@ mod tests {
|
||||
.expect("runtime paths"),
|
||||
ExecServerTelemetry::default(),
|
||||
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
super::RequestDispatchMode::Inline,
|
||||
)
|
||||
.await
|
||||
.expect_err("invalid listen URL should fail");
|
||||
|
||||
@@ -9,10 +9,13 @@ use crate::ExecServerRuntimePaths;
|
||||
use crate::connection::CHANNEL_CAPACITY;
|
||||
use crate::connection::JsonRpcConnection;
|
||||
use crate::connection::JsonRpcConnectionEvent;
|
||||
use crate::rpc::RpcCallError;
|
||||
use crate::rpc::RpcNotificationSender;
|
||||
use crate::rpc::RpcServerOutboundMessage;
|
||||
use crate::rpc::encode_server_message;
|
||||
use crate::rpc_server_requests::RpcServerRequestSender;
|
||||
use crate::server::ExecServerHandler;
|
||||
use crate::server::RequestDispatchMode;
|
||||
use crate::server::registry::build_router;
|
||||
use crate::server::request_dispatcher::RequestDispatcher;
|
||||
use crate::server::request_dispatcher::RequestTaskResult;
|
||||
@@ -27,6 +30,7 @@ pub(crate) struct ConnectionProcessor {
|
||||
runtime_paths: ExecServerRuntimePaths,
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
request_dispatch_mode: RequestDispatchMode,
|
||||
}
|
||||
|
||||
impl ConnectionProcessor {
|
||||
@@ -38,6 +42,7 @@ impl ConnectionProcessor {
|
||||
codex_http_client::HttpClientFactory::new(
|
||||
codex_http_client::OutboundProxyPolicy::ReqwestDefault,
|
||||
),
|
||||
RequestDispatchMode::Inline,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -45,12 +50,14 @@ impl ConnectionProcessor {
|
||||
runtime_paths: ExecServerRuntimePaths,
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
request_dispatch_mode: RequestDispatchMode,
|
||||
) -> Self {
|
||||
Self {
|
||||
session_registry: SessionRegistry::new(telemetry.clone()),
|
||||
runtime_paths,
|
||||
telemetry,
|
||||
http_client_factory,
|
||||
request_dispatch_mode,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +73,7 @@ impl ConnectionProcessor {
|
||||
self.telemetry.clone(),
|
||||
self.http_client_factory.clone(),
|
||||
transport,
|
||||
self.request_dispatch_mode,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -82,13 +90,13 @@ async fn run_connection(
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
transport: ConnectionTransport,
|
||||
request_dispatch_mode: RequestDispatchMode,
|
||||
) {
|
||||
let _connection_metrics = telemetry.connection_started(transport);
|
||||
let router = Arc::new(build_router());
|
||||
let JsonRpcConnection {
|
||||
outgoing_tx: json_outgoing_tx,
|
||||
mut incoming_rx,
|
||||
disconnected_rx,
|
||||
mut disconnected_rx,
|
||||
task_handles: connection_tasks,
|
||||
transport: _transport,
|
||||
} = connection;
|
||||
@@ -119,20 +127,41 @@ async fn run_connection(
|
||||
});
|
||||
|
||||
let mut dispatcher = RequestDispatcher::new(
|
||||
router,
|
||||
Arc::new(build_router()),
|
||||
Arc::clone(&handler),
|
||||
outgoing_tx.clone(),
|
||||
disconnected_rx,
|
||||
disconnected_rx.clone(),
|
||||
requests.clone(),
|
||||
telemetry,
|
||||
request_dispatch_mode,
|
||||
);
|
||||
|
||||
// Process inbound events sequentially to preserve initialize/initialized ordering.
|
||||
while let Some(event) = incoming_rx.recv().await {
|
||||
loop {
|
||||
let has_request_tasks = dispatcher.has_tasks();
|
||||
let event = tokio::select! {
|
||||
result = dispatcher.join_next(), if has_request_tasks => {
|
||||
if result == RequestTaskResult::ConnectionClosed {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
_ = disconnected_rx.changed() => {
|
||||
debug!("exec-server transport disconnected");
|
||||
break;
|
||||
}
|
||||
event = incoming_rx.recv() => {
|
||||
let Some(event) = event else {
|
||||
break;
|
||||
};
|
||||
event
|
||||
}
|
||||
};
|
||||
|
||||
if !handler.is_session_attached() {
|
||||
debug!("exec-server connection evicted after session resume");
|
||||
break;
|
||||
}
|
||||
|
||||
let result = match event {
|
||||
JsonRpcConnectionEvent::MalformedMessage { reason } => {
|
||||
dispatcher.handle_malformed_message(reason).await
|
||||
@@ -157,8 +186,11 @@ async fn run_connection(
|
||||
}
|
||||
}
|
||||
|
||||
if *disconnected_rx.borrow() {
|
||||
complete_queued_client_responses(&requests, &mut incoming_rx);
|
||||
}
|
||||
requests.close();
|
||||
drop(dispatcher);
|
||||
dispatcher.shutdown().await;
|
||||
handler.shutdown().await;
|
||||
drop(handler);
|
||||
drop(requests);
|
||||
@@ -170,6 +202,30 @@ async fn run_connection(
|
||||
let _ = outbound_task.await;
|
||||
}
|
||||
|
||||
fn complete_queued_client_responses(
|
||||
requests: &RpcServerRequestSender,
|
||||
incoming_rx: &mut mpsc::Receiver<JsonRpcConnectionEvent>,
|
||||
) {
|
||||
while let Ok(event) = incoming_rx.try_recv() {
|
||||
let (request_id, response) = match event {
|
||||
JsonRpcConnectionEvent::Message(JSONRPCMessage::Response(response)) => {
|
||||
(response.id, Ok(response.result))
|
||||
}
|
||||
JsonRpcConnectionEvent::Message(JSONRPCMessage::Error(error)) => {
|
||||
(error.id, Err(RpcCallError::Server(error.error)))
|
||||
}
|
||||
JsonRpcConnectionEvent::Message(
|
||||
JSONRPCMessage::Request(_) | JSONRPCMessage::Notification(_),
|
||||
)
|
||||
| JsonRpcConnectionEvent::MalformedMessage { .. }
|
||||
| JsonRpcConnectionEvent::Disconnected { .. } => continue,
|
||||
};
|
||||
if !requests.complete(request_id.clone(), response) {
|
||||
warn!("ignoring unexpected client response while disconnecting: {request_id:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
@@ -191,13 +247,16 @@ mod tests {
|
||||
use tokio::io::DuplexStream;
|
||||
use tokio::io::Lines;
|
||||
use tokio::io::duplex;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::timeout;
|
||||
|
||||
use super::complete_queued_client_responses;
|
||||
use super::run_connection;
|
||||
use crate::ExecServerRuntimePaths;
|
||||
use crate::ProcessId;
|
||||
use crate::connection::JsonRpcConnection;
|
||||
use crate::connection::JsonRpcConnectionEvent;
|
||||
use crate::protocol::ENVIRONMENT_INFO_METHOD;
|
||||
use crate::protocol::ENVIRONMENT_STATUS_METHOD;
|
||||
use crate::protocol::EXEC_METHOD;
|
||||
@@ -208,13 +267,19 @@ mod tests {
|
||||
use crate::protocol::EnvironmentStatusKind;
|
||||
use crate::protocol::ExecParams;
|
||||
use crate::protocol::ExecResponse;
|
||||
use crate::protocol::ExecServerNetworkPolicyDecision;
|
||||
use crate::protocol::INITIALIZE_METHOD;
|
||||
use crate::protocol::INITIALIZED_METHOD;
|
||||
use crate::protocol::InitializeParams;
|
||||
use crate::protocol::InitializeResponse;
|
||||
use crate::protocol::NETWORK_POLICY_REQUEST_METHOD;
|
||||
use crate::protocol::NetworkPolicyRequestResponse;
|
||||
use crate::protocol::ReadParams;
|
||||
use crate::protocol::TerminateParams;
|
||||
use crate::protocol::TerminateResponse;
|
||||
use crate::rpc::RpcServerOutboundMessage;
|
||||
use crate::rpc_server_requests::RpcServerRequestSender;
|
||||
use crate::server::RequestDispatchMode;
|
||||
use crate::server::session_registry::SessionRegistry;
|
||||
|
||||
#[tokio::test]
|
||||
@@ -256,6 +321,61 @@ mod tests {
|
||||
.expect("processor should join");
|
||||
}
|
||||
|
||||
/// A callback response received before EOF must survive transport shutdown.
|
||||
#[tokio::test]
|
||||
async fn disconnect_completes_queued_network_policy_response() {
|
||||
let (outgoing_tx, mut outgoing_rx) = mpsc::channel(/*buffer*/ 1);
|
||||
let requests = RpcServerRequestSender::new(outgoing_tx);
|
||||
let pending_request = {
|
||||
let requests = requests.clone();
|
||||
tokio::spawn(async move {
|
||||
requests
|
||||
.call_with_timeout::<_, NetworkPolicyRequestResponse>(
|
||||
NETWORK_POLICY_REQUEST_METHOD,
|
||||
&(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
})
|
||||
};
|
||||
let RpcServerOutboundMessage::Request(request) = outgoing_rx
|
||||
.recv()
|
||||
.await
|
||||
.expect("network policy request should be queued")
|
||||
else {
|
||||
panic!("expected outbound network policy request");
|
||||
};
|
||||
|
||||
let (incoming_tx, mut incoming_rx) = mpsc::channel(/*buffer*/ 2);
|
||||
incoming_tx
|
||||
.try_send(JsonRpcConnectionEvent::Message(JSONRPCMessage::Response(
|
||||
JSONRPCResponse {
|
||||
id: request.id,
|
||||
result: serde_json::to_value(NetworkPolicyRequestResponse {
|
||||
decision: ExecServerNetworkPolicyDecision::Allow,
|
||||
})
|
||||
.expect("serialize network policy response"),
|
||||
},
|
||||
)))
|
||||
.expect("queue network policy response");
|
||||
incoming_tx
|
||||
.try_send(JsonRpcConnectionEvent::Disconnected { reason: None })
|
||||
.expect("queue transport disconnect");
|
||||
|
||||
complete_queued_client_responses(&requests, &mut incoming_rx);
|
||||
requests.close();
|
||||
|
||||
assert_eq!(
|
||||
pending_request
|
||||
.await
|
||||
.expect("network policy request should join")
|
||||
.expect("network policy request should complete"),
|
||||
NetworkPolicyRequestResponse {
|
||||
decision: ExecServerNetworkPolicyDecision::Allow,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn transport_disconnect_detaches_session_during_in_flight_read() {
|
||||
let registry = SessionRegistry::new(crate::ExecServerTelemetry::default());
|
||||
@@ -363,6 +483,7 @@ mod tests {
|
||||
codex_http_client::OutboundProxyPolicy::ReqwestDefault,
|
||||
),
|
||||
crate::telemetry::ConnectionTransport::Stdio,
|
||||
RequestDispatchMode::Inline,
|
||||
));
|
||||
(client_writer, BufReader::new(client_reader).lines(), task)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
use std::num::NonZeroUsize;
|
||||
use std::num::ParseIntError;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
@@ -6,12 +9,21 @@ use codex_exec_server_protocol::JSONRPCNotification;
|
||||
use codex_exec_server_protocol::JSONRPCRequest;
|
||||
use codex_exec_server_protocol::JSONRPCResponse;
|
||||
use codex_exec_server_protocol::RequestId;
|
||||
use tokio::sync::Semaphore;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::watch;
|
||||
use tokio::task::JoinSet;
|
||||
use tracing::Instrument;
|
||||
use tracing::debug;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::protocol::ENVIRONMENT_INFO_METHOD;
|
||||
use crate::protocol::ENVIRONMENT_STATUS_METHOD;
|
||||
use crate::protocol::EXEC_SIGNAL_METHOD;
|
||||
use crate::protocol::EXEC_TERMINATE_METHOD;
|
||||
use crate::protocol::FS_CLOSE_METHOD;
|
||||
use crate::protocol::INITIALIZE_METHOD;
|
||||
use crate::protocol::INITIALIZED_METHOD;
|
||||
use crate::rpc::RpcCallError;
|
||||
use crate::rpc::RpcRouter;
|
||||
use crate::rpc::RpcServerOutboundMessage;
|
||||
@@ -28,6 +40,9 @@ pub(super) struct RequestDispatcher {
|
||||
disconnected_rx: watch::Receiver<bool>,
|
||||
requests: RpcServerRequestSender,
|
||||
telemetry: ExecServerTelemetry,
|
||||
lanes: Option<RequestLanes>,
|
||||
tasks: JoinSet<RequestTaskResult>,
|
||||
initialized: bool,
|
||||
}
|
||||
|
||||
impl RequestDispatcher {
|
||||
@@ -38,7 +53,18 @@ impl RequestDispatcher {
|
||||
disconnected_rx: watch::Receiver<bool>,
|
||||
requests: RpcServerRequestSender,
|
||||
telemetry: ExecServerTelemetry,
|
||||
mode: RequestDispatchMode,
|
||||
) -> Self {
|
||||
let lanes = match mode {
|
||||
RequestDispatchMode::Inline => None,
|
||||
RequestDispatchMode::Concurrent {
|
||||
max_concurrent_requests,
|
||||
} => Some(RequestLanes {
|
||||
ordinary: Arc::new(Semaphore::new(max_concurrent_requests.get())),
|
||||
control: Arc::new(Semaphore::new(max_concurrent_requests.get())),
|
||||
}),
|
||||
};
|
||||
|
||||
Self {
|
||||
router,
|
||||
handler,
|
||||
@@ -46,6 +72,24 @@ impl RequestDispatcher {
|
||||
disconnected_rx,
|
||||
requests,
|
||||
telemetry,
|
||||
lanes,
|
||||
tasks: JoinSet::new(),
|
||||
initialized: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn has_tasks(&self) -> bool {
|
||||
!self.tasks.is_empty()
|
||||
}
|
||||
|
||||
pub(super) async fn join_next(&mut self) -> RequestTaskResult {
|
||||
match self.tasks.join_next().await {
|
||||
Some(Ok(result)) => result,
|
||||
Some(Err(error)) => {
|
||||
warn!("exec-server request task failed: {error}");
|
||||
RequestTaskResult::ConnectionClosed
|
||||
}
|
||||
None => RequestTaskResult::Completed,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +114,7 @@ impl RequestDispatcher {
|
||||
&mut self,
|
||||
notification: JSONRPCNotification,
|
||||
) -> RequestTaskResult {
|
||||
let is_initialized = notification.method == INITIALIZED_METHOD;
|
||||
let Some(route) = self.router.notification_route(notification.method.as_str()) else {
|
||||
warn!(
|
||||
"closing exec-server connection after unexpected notification: {}",
|
||||
@@ -88,6 +133,9 @@ impl RequestDispatcher {
|
||||
warn!("closing exec-server connection after protocol error: {error}");
|
||||
return RequestTaskResult::ConnectionClosed;
|
||||
}
|
||||
if is_initialized {
|
||||
self.initialized = true;
|
||||
}
|
||||
|
||||
RequestTaskResult::Completed
|
||||
}
|
||||
@@ -150,33 +198,126 @@ impl RequestDispatcher {
|
||||
return RequestTaskResult::Completed;
|
||||
};
|
||||
|
||||
let span = request_span(method, &request);
|
||||
let message = tokio::select! {
|
||||
message = route(Arc::clone(&self.handler), request).instrument(span.clone()) => message,
|
||||
_ = self.disconnected_rx.changed() => {
|
||||
span.record("result", "disconnected");
|
||||
self.telemetry
|
||||
.request_completed(method, "disconnected", started_at.elapsed());
|
||||
debug!("exec-server transport disconnected while handling request");
|
||||
let task_span = request_span(method, &request);
|
||||
let route = route(Arc::clone(&self.handler), request);
|
||||
let outgoing_tx = self.outgoing_tx.clone();
|
||||
let mut disconnected_rx = self.disconnected_rx.clone();
|
||||
let telemetry = self.telemetry.clone();
|
||||
let task = async move {
|
||||
let message = tokio::select! {
|
||||
message = route.instrument(task_span.clone()) => message,
|
||||
_ = disconnected_rx.changed() => {
|
||||
task_span.record("result", "disconnected");
|
||||
telemetry.request_completed(method, "disconnected", started_at.elapsed());
|
||||
return RequestTaskResult::ConnectionClosed;
|
||||
}
|
||||
};
|
||||
let result = request_result(&message);
|
||||
let response_sent = match message {
|
||||
Some(message) => tokio::select! {
|
||||
result = outgoing_tx.send(message) => result.is_ok(),
|
||||
_ = disconnected_rx.changed() => false,
|
||||
},
|
||||
None => true,
|
||||
};
|
||||
if !response_sent {
|
||||
task_span.record("result", "disconnected");
|
||||
telemetry.request_completed(method, "disconnected", started_at.elapsed());
|
||||
return RequestTaskResult::ConnectionClosed;
|
||||
}
|
||||
task_span.record("result", result);
|
||||
telemetry.request_completed(method, result, started_at.elapsed());
|
||||
RequestTaskResult::Completed
|
||||
};
|
||||
let result = request_result(&message);
|
||||
if let Some(message) = message
|
||||
&& self.outgoing_tx.send(message).await.is_err()
|
||||
{
|
||||
span.record("result", "disconnected");
|
||||
self.telemetry
|
||||
.request_completed(method, "disconnected", started_at.elapsed());
|
||||
return RequestTaskResult::ConnectionClosed;
|
||||
}
|
||||
span.record("result", result);
|
||||
self.telemetry
|
||||
.request_completed(method, result, started_at.elapsed());
|
||||
drop(span);
|
||||
|
||||
let Some(RequestLanes { ordinary, control }) = &self.lanes else {
|
||||
// Keep requests ordered when concurrent dispatch is not enabled.
|
||||
return task.await;
|
||||
};
|
||||
// Finish the handshake before concurrent requests can observe session state.
|
||||
if method == INITIALIZE_METHOD || !self.initialized {
|
||||
return task.await;
|
||||
}
|
||||
|
||||
// Reserve capacity for health checks and cleanup while ordinary requests are blocked.
|
||||
let admission = if matches!(
|
||||
method,
|
||||
ENVIRONMENT_INFO_METHOD
|
||||
| ENVIRONMENT_STATUS_METHOD
|
||||
| EXEC_SIGNAL_METHOD
|
||||
| EXEC_TERMINATE_METHOD
|
||||
| FS_CLOSE_METHOD
|
||||
) {
|
||||
Arc::clone(control)
|
||||
} else {
|
||||
Arc::clone(ordinary)
|
||||
};
|
||||
|
||||
// TODO(anp) bound queued request bytes without blocking later responses or cleanup.
|
||||
self.tasks.spawn(async move {
|
||||
let Ok(_permit) = admission.acquire_owned().await else {
|
||||
return RequestTaskResult::ConnectionClosed;
|
||||
};
|
||||
task.await
|
||||
});
|
||||
RequestTaskResult::Completed
|
||||
}
|
||||
|
||||
pub(super) async fn shutdown(mut self) {
|
||||
self.tasks.abort_all();
|
||||
while self.tasks.join_next().await.is_some() {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-connection request dispatch policy for local and remote exec-servers.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum RequestDispatchMode {
|
||||
Inline,
|
||||
Concurrent {
|
||||
max_concurrent_requests: ConcurrentRequestLimit,
|
||||
},
|
||||
}
|
||||
|
||||
/// A valid request concurrency limit accepted by Tokio's semaphore.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct ConcurrentRequestLimit(usize);
|
||||
|
||||
impl ConcurrentRequestLimit {
|
||||
/// Returns a limit when it enables concurrency and fits Tokio's semaphore.
|
||||
pub fn new(max_concurrent_requests: usize) -> Option<Self> {
|
||||
if !(2..=Semaphore::MAX_PERMITS).contains(&max_concurrent_requests) {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Self(max_concurrent_requests))
|
||||
}
|
||||
|
||||
/// Returns the validated number of concurrent requests.
|
||||
pub fn get(self) -> usize {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for RequestDispatchMode {
|
||||
type Err = ParseIntError;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
let max_concurrent_requests = value.parse::<NonZeroUsize>()?.get();
|
||||
if max_concurrent_requests == 1 {
|
||||
Ok(Self::Inline)
|
||||
} else {
|
||||
Ok(Self::Concurrent {
|
||||
max_concurrent_requests: ConcurrentRequestLimit(
|
||||
max_concurrent_requests.min(Semaphore::MAX_PERMITS),
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RequestLanes {
|
||||
ordinary: Arc<Semaphore>,
|
||||
control: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
|
||||
@@ -6,11 +6,54 @@ use opentelemetry::trace::TracerProvider as _;
|
||||
use opentelemetry_sdk::trace::InMemorySpanExporter;
|
||||
use opentelemetry_sdk::trace::SdkTracerProvider;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tokio::sync::Semaphore;
|
||||
use tracing_subscriber::filter::filter_fn;
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
use super::ConcurrentRequestLimit;
|
||||
use super::RequestDispatchMode;
|
||||
use super::request_span;
|
||||
|
||||
/// Public limits reject values that cannot safely enable semaphore-backed concurrency.
|
||||
#[test]
|
||||
fn concurrent_request_limit_rejects_invalid_values() {
|
||||
assert_eq!(
|
||||
ConcurrentRequestLimit::new(/*max_concurrent_requests*/ 0),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
ConcurrentRequestLimit::new(/*max_concurrent_requests*/ 1),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
ConcurrentRequestLimit::new(Semaphore::MAX_PERMITS.saturating_add(1)),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
ConcurrentRequestLimit::new(/*max_concurrent_requests*/ 2).map(ConcurrentRequestLimit::get),
|
||||
Some(2)
|
||||
);
|
||||
}
|
||||
|
||||
/// CLI parsing keeps one request inline and bounds larger positive concurrency limits.
|
||||
#[test]
|
||||
fn request_dispatch_mode_parses_bounded_concurrency() {
|
||||
assert!(matches!("1".parse(), Ok(RequestDispatchMode::Inline)));
|
||||
assert!("0".parse::<RequestDispatchMode>().is_err());
|
||||
|
||||
let oversized_limit = Semaphore::MAX_PERMITS.saturating_add(1).to_string();
|
||||
let mode = oversized_limit
|
||||
.parse::<RequestDispatchMode>()
|
||||
.expect("parse oversized concurrent request limit");
|
||||
let RequestDispatchMode::Concurrent {
|
||||
max_concurrent_requests,
|
||||
} = mode
|
||||
else {
|
||||
panic!("expected concurrent request dispatch");
|
||||
};
|
||||
assert_eq!(max_concurrent_requests.get(), Semaphore::MAX_PERMITS);
|
||||
}
|
||||
|
||||
/// Request spans retain the wire method and inbound trace while bounding their exported names.
|
||||
#[test]
|
||||
fn request_span_uses_bounded_name_wire_method_and_inbound_trace_parent() {
|
||||
|
||||
@@ -25,6 +25,7 @@ use tracing::warn;
|
||||
use crate::ExecServerRuntimePaths;
|
||||
use crate::ExecServerTelemetry;
|
||||
use crate::connection::JsonRpcConnection;
|
||||
use crate::server::RequestDispatchMode;
|
||||
use crate::server::processor::ConnectionProcessor;
|
||||
use crate::telemetry::ConnectionTransport;
|
||||
|
||||
@@ -85,14 +86,27 @@ pub(crate) async fn run_transport(
|
||||
runtime_paths: ExecServerRuntimePaths,
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
request_dispatch_mode: RequestDispatchMode,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
match parse_listen_url(listen_url)? {
|
||||
ExecServerListenTransport::WebSocket(bind_address) => {
|
||||
run_websocket_listener(bind_address, runtime_paths, telemetry, http_client_factory)
|
||||
.await
|
||||
run_websocket_listener(
|
||||
bind_address,
|
||||
runtime_paths,
|
||||
telemetry,
|
||||
http_client_factory,
|
||||
request_dispatch_mode,
|
||||
)
|
||||
.await
|
||||
}
|
||||
ExecServerListenTransport::Stdio => {
|
||||
run_stdio_connection(runtime_paths, telemetry, http_client_factory).await
|
||||
run_stdio_connection(
|
||||
runtime_paths,
|
||||
telemetry,
|
||||
http_client_factory,
|
||||
request_dispatch_mode,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,6 +115,7 @@ async fn run_stdio_connection(
|
||||
runtime_paths: ExecServerRuntimePaths,
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
request_dispatch_mode: RequestDispatchMode,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
run_stdio_connection_with_io(
|
||||
io::stdin(),
|
||||
@@ -108,6 +123,7 @@ async fn run_stdio_connection(
|
||||
runtime_paths,
|
||||
telemetry,
|
||||
http_client_factory,
|
||||
request_dispatch_mode,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -118,13 +134,18 @@ async fn run_stdio_connection_with_io<R, W>(
|
||||
runtime_paths: ExecServerRuntimePaths,
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
request_dispatch_mode: RequestDispatchMode,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
|
||||
where
|
||||
R: AsyncRead + Unpin + Send + 'static,
|
||||
W: AsyncWrite + Unpin + Send + 'static,
|
||||
{
|
||||
let processor =
|
||||
ConnectionProcessor::new_with_telemetry(runtime_paths, telemetry, http_client_factory);
|
||||
let processor = ConnectionProcessor::new_with_telemetry(
|
||||
runtime_paths,
|
||||
telemetry,
|
||||
http_client_factory,
|
||||
request_dispatch_mode,
|
||||
);
|
||||
tracing::info!("codex-exec-server listening on stdio");
|
||||
processor
|
||||
.run_connection(
|
||||
@@ -142,11 +163,16 @@ async fn run_websocket_listener(
|
||||
runtime_paths: ExecServerRuntimePaths,
|
||||
telemetry: ExecServerTelemetry,
|
||||
http_client_factory: HttpClientFactory,
|
||||
request_dispatch_mode: RequestDispatchMode,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let listener = TcpListener::bind(bind_address).await?;
|
||||
let local_addr = listener.local_addr()?;
|
||||
let processor =
|
||||
ConnectionProcessor::new_with_telemetry(runtime_paths, telemetry, http_client_factory);
|
||||
let processor = ConnectionProcessor::new_with_telemetry(
|
||||
runtime_paths,
|
||||
telemetry,
|
||||
http_client_factory,
|
||||
request_dispatch_mode,
|
||||
);
|
||||
info!("codex-exec-server listening on ws://{local_addr}");
|
||||
println!("ws://{local_addr}");
|
||||
std::io::stdout().flush()?;
|
||||
|
||||
@@ -22,6 +22,7 @@ use crate::protocol::INITIALIZE_METHOD;
|
||||
use crate::protocol::INITIALIZED_METHOD;
|
||||
use crate::protocol::InitializeParams;
|
||||
use crate::protocol::InitializeResponse;
|
||||
use crate::server::RequestDispatchMode;
|
||||
|
||||
#[test]
|
||||
fn parse_listen_url_accepts_default_websocket_url() {
|
||||
@@ -65,6 +66,7 @@ async fn stdio_listen_transport_serves_initialize() {
|
||||
codex_http_client::HttpClientFactory::new(
|
||||
codex_http_client::OutboundProxyPolicy::ReqwestDefault,
|
||||
),
|
||||
RequestDispatchMode::Inline,
|
||||
));
|
||||
let mut client_lines = BufReader::new(client_reader).lines();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user