From 108730843ae4303ae79e9b6b936343f0b7c483bb Mon Sep 17 00:00:00 2001 From: jif Date: Mon, 23 Nov 2026 13:44:49 +0000 Subject: [PATCH] Abort the remote app-server worker when its client is dropped (#63351) ## Why Dropping the remote client or cancelling its shutdown could leave the WebSocket transport worker running, even if it was stalled on a socket write. ## What changed Give the client an abort-on-drop task handle so dropping the client or cancelling its shutdown future aborts the transport worker. ## Testing Extend the full command queue regression test to cover shutdown, shutdown cancellation, and direct client drop. Each case verifies that the worker releases its command receiver and terminates. GitOrigin-RevId: a5a83b3ab8520d4096be444bc24676ac9c444cb1 --- codex-rs/Cargo.lock | 1 + codex-rs/app-server-remote-client/Cargo.toml | 3 +- .../app-server-remote-client/src/remote.rs | 85 ++++++++++++------- 3 files changed, 59 insertions(+), 30 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index a7d4dc41f9..49823fbd70 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2340,6 +2340,7 @@ dependencies = [ "serde_json", "tokio", "tokio-tungstenite", + "tokio-util", "tracing", "url", "zstd 0.13.3", diff --git a/codex-rs/app-server-remote-client/Cargo.toml b/codex-rs/app-server-remote-client/Cargo.toml index b212388807..acbae7285b 100644 --- a/codex-rs/app-server-remote-client/Cargo.toml +++ b/codex-rs/app-server-remote-client/Cargo.toml @@ -22,10 +22,11 @@ serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true, features = ["sync", "time", "rt"] } tokio-tungstenite = { workspace = true } +tokio-util = { workspace = true, features = ["rt"] } tracing = { workspace = true } url = { workspace = true } zstd = { workspace = true } [dev-dependencies] pretty_assertions = { workspace = true } -tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } +tokio = { workspace = true, features = ["macros", "rt-multi-thread", "test-util"] } diff --git a/codex-rs/app-server-remote-client/src/remote.rs b/codex-rs/app-server-remote-client/src/remote.rs index f9a8ad6ad6..1578c98011 100644 --- a/codex-rs/app-server-remote-client/src/remote.rs +++ b/codex-rs/app-server-remote-client/src/remote.rs @@ -7,6 +7,7 @@ //! same `AppServerEvent` surface for both in-process and remote transports, so //! callers such as the TUI can switch between them without changing their //! higher-level session logic. +//! Dropping the client or cancelling shutdown aborts its owned transport worker. use std::collections::HashMap; use std::collections::VecDeque; @@ -59,6 +60,7 @@ use tokio_tungstenite::tungstenite::handshake::client::Request; use tokio_tungstenite::tungstenite::http::HeaderValue; use tokio_tungstenite::tungstenite::http::header::AUTHORIZATION; use tokio_tungstenite::tungstenite::protocol::WebSocketConfig; +use tokio_util::task::AbortOnDropHandle; use tracing::warn; use url::Url; @@ -168,7 +170,7 @@ pub struct RemoteAppServerClient { event_rx: mpsc::UnboundedReceiver, pending_events: VecDeque, metadata: RemoteServerMetadata, - worker_handle: tokio::task::JoinHandle<()>, + worker_handle: AbortOnDropHandle<()>, } #[derive(Clone)] @@ -531,7 +533,7 @@ impl RemoteAppServerClient { event_rx, pending_events: pending_events.into(), metadata, - worker_handle, + worker_handle: AbortOnDropHandle::new(worker_handle), }) } @@ -1088,33 +1090,58 @@ mod tests { #[tokio::test(start_paused = true)] async fn shutdown_aborts_worker_when_command_queue_is_full() { - let (command_tx, command_rx) = mpsc::channel(1); - let (_event_tx, event_rx) = mpsc::unbounded_channel::(); - let (response_tx, _response_rx) = oneshot::channel(); - assert!( - command_tx - .try_send(RemoteClientCommand::Shutdown { response_tx }) - .is_ok() - ); - let worker_handle = tokio::spawn(async move { - // Retain the full queue while simulating a stalled socket write. - std::future::pending::<()>().await; - drop(command_rx); - }); - let worker_abort_handle = worker_handle.abort_handle(); - let client = RemoteAppServerClient { - command_tx, - event_rx, - pending_events: VecDeque::new(), - metadata: RemoteServerMetadata::default(), - worker_handle, - }; + enum Cleanup { + Shutdown, + CancelShutdown, + DropClient, + } + for cleanup in [ + Cleanup::Shutdown, + Cleanup::CancelShutdown, + Cleanup::DropClient, + ] { + let (command_tx, command_rx) = mpsc::channel(1); + let commands = command_tx.clone(); + let (_event_tx, event_rx) = mpsc::unbounded_channel::(); + let (response_tx, _response_rx) = oneshot::channel(); + assert!( + command_tx + .try_send(RemoteClientCommand::Shutdown { response_tx }) + .is_ok() + ); + let worker_handle = tokio::spawn(async move { + // Retain the full queue while simulating a stalled socket write. + std::future::pending::<()>().await; + drop(command_rx); + }); + let worker_abort_handle = worker_handle.abort_handle(); + let client = RemoteAppServerClient { + command_tx, + event_rx, + pending_events: VecDeque::new(), + metadata: RemoteServerMetadata::default(), + worker_handle: AbortOnDropHandle::new(worker_handle), + }; - timeout(SHUTDOWN_TIMEOUT * 3, client.shutdown()) - .await - .expect("shutdown must not wait indefinitely for command queue space") - .expect("shutdown should abort a stalled worker"); - assert!(worker_abort_handle.is_finished()); + match cleanup { + Cleanup::Shutdown => { + timeout(SHUTDOWN_TIMEOUT * 3, client.shutdown()) + .await + .expect("shutdown must not wait indefinitely for command queue space") + .expect("shutdown should abort a stalled worker"); + } + Cleanup::CancelShutdown => { + let mut shutdown = Box::pin(client.shutdown()); + assert!(futures::poll!(shutdown.as_mut()).is_pending()); + drop(shutdown); + } + Cleanup::DropClient => drop(client), + } + timeout(SHUTDOWN_TIMEOUT * 3, commands.closed()) + .await + .expect("cleanup must release the stalled worker's command receiver"); + assert!(worker_abort_handle.is_finished()); + } } #[tokio::test] @@ -1142,7 +1169,7 @@ mod tests { event_rx, pending_events: VecDeque::new(), metadata: RemoteServerMetadata::default(), - worker_handle, + worker_handle: AbortOnDropHandle::new(worker_handle), }; client