From 4f556fdd6db3f614207bacc037b1bc4452e301bf Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 17 Nov 2026 00:10:41 +0000 Subject: [PATCH] Encapsulate app-server thread-list locking in `ThreadListState` (#61928) ## What changed Introduce `ThreadListState` to wrap the single-permit semaphore shared by thread and project request processors. Centralize exclusive permit acquisition and error handling while preserving the existing locking behavior. GitOrigin-RevId: e596241d5f2620ea791c3bf95f5d97aabc1e8623 --- codex-rs/app-server/src/message_processor.rs | 4 +-- codex-rs/app-server/src/request_processors.rs | 4 ++- .../src/request_processors/projects.rs | 14 ++++------ .../request_processors/thread_list_state.rs | 26 +++++++++++++++++++ .../thread_listener_shutdown_tests.rs | 3 +-- .../request_processors/thread_processor.rs | 11 +++----- 6 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 codex-rs/app-server/src/request_processors/thread_list_state.rs diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index f04c821c05..4c6311e7ba 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -51,6 +51,7 @@ use crate::request_processors::TurnRequestProcessor; use crate::request_processors::WindowsSandboxRequestProcessor; use crate::request_processors::account_processor::handle_gcp_setup; use crate::request_processors::read_server_diagnostics; +use crate::request_processors::thread_list_state::ThreadListState; use crate::request_serialization::QueuedInitializedRequest; use crate::request_serialization::RequestSerializationQueueKey; use crate::request_serialization::RequestSerializationQueues; @@ -103,7 +104,6 @@ use codex_thread_store::LocalQueueStore; use codex_thread_store::QueueStore; use codex_thread_store::ThreadStoreError; use tokio::sync::Mutex; -use tokio::sync::Semaphore; use tokio::sync::broadcast; use tokio::sync::mpsc; use tokio::sync::watch; @@ -452,7 +452,7 @@ impl MessageProcessor { let pending_thread_unloads = Arc::new(Mutex::new(HashMap::new())); let thread_watch_manager = crate::thread_status::ThreadWatchManager::new_with_outgoing(outgoing.clone()); - let thread_list_state_permit = Arc::new(Semaphore::new(/*permits*/ 1)); + let thread_list_state_permit = Arc::new(ThreadListState::default()); let app_list_shutdown_token = CancellationToken::new(); let request_serialization_queues = RequestSerializationQueues::default(); let config_processor = ConfigRequestProcessor::new( diff --git a/codex-rs/app-server/src/request_processors.rs b/codex-rs/app-server/src/request_processors.rs index 8d3cf0e4d5..d8175a7785 100644 --- a/codex-rs/app-server/src/request_processors.rs +++ b/codex-rs/app-server/src/request_processors.rs @@ -519,7 +519,6 @@ use std::sync::Arc; use std::time::Duration; use std::time::Instant; use tokio::sync::Mutex; -use tokio::sync::Semaphore; use tokio::sync::SemaphorePermit; use tokio::sync::broadcast; use tokio::sync::oneshot; @@ -528,6 +527,8 @@ use tokio_util::sync::CancellationToken; use tokio_util::sync::DropGuard; use tokio_util::task::TaskTracker; use toml::Value as TomlValue; + +use self::thread_list_state::ThreadListState; use tracing::Instrument; use tracing::error; use tracing::info; @@ -570,6 +571,7 @@ mod thread_attachments; mod thread_enrichment; mod thread_fork_goal; mod thread_input; +pub(crate) mod thread_list_state; mod thread_memory; mod thread_processor; mod thread_queue_processor; diff --git a/codex-rs/app-server/src/request_processors/projects.rs b/codex-rs/app-server/src/request_processors/projects.rs index fdc9d54588..92e3fee413 100644 --- a/codex-rs/app-server/src/request_processors/projects.rs +++ b/codex-rs/app-server/src/request_processors/projects.rs @@ -36,9 +36,10 @@ use codex_thread_store::StoredProjectRoot; use codex_thread_store::ThreadStore; use codex_thread_store::ThreadStoreError; use codex_thread_store::UpdateProjectParams as StoreUpdateProjectParams; -use tokio::sync::Semaphore; use tokio::sync::SemaphorePermit; +use super::ThreadListState; + use super::thread_processor::THREAD_LIST_DEFAULT_LIMIT; use super::thread_processor::THREAD_LIST_MAX_LIMIT; use crate::error_code::internal_error; @@ -50,14 +51,14 @@ use crate::outgoing_message::OutgoingMessageSender; pub(crate) struct ProjectRequestProcessor { thread_store: Arc, outgoing: Arc, - thread_list_state_permit: Arc, + thread_list_state_permit: Arc, } impl ProjectRequestProcessor { pub(crate) fn new( thread_store: Arc, outgoing: Arc, - thread_list_state_permit: Arc, + thread_list_state_permit: Arc, ) -> Self { Self { thread_store, @@ -291,12 +292,7 @@ impl ProjectRequestProcessor { async fn acquire_thread_list_state_permit( &self, ) -> Result, JSONRPCErrorError> { - self.thread_list_state_permit - .acquire() - .await - .map_err(|err| { - internal_error(format!("failed to acquire thread list state permit: {err}")) - }) + self.thread_list_state_permit.acquire_exclusive().await } } diff --git a/codex-rs/app-server/src/request_processors/thread_list_state.rs b/codex-rs/app-server/src/request_processors/thread_list_state.rs new file mode 100644 index 0000000000..9d63321add --- /dev/null +++ b/codex-rs/app-server/src/request_processors/thread_list_state.rs @@ -0,0 +1,26 @@ +//! Coordinates exclusive access to thread-list mutations and resumes. + +use super::internal_error; +use codex_app_server_protocol::JSONRPCErrorError; +use tokio::sync::Semaphore; +use tokio::sync::SemaphorePermit; + +pub(crate) struct ThreadListState { + semaphore: Semaphore, +} + +impl Default for ThreadListState { + fn default() -> Self { + Self { + semaphore: Semaphore::new(/*permits*/ 1), + } + } +} + +impl ThreadListState { + pub(super) async fn acquire_exclusive(&self) -> Result, JSONRPCErrorError> { + self.semaphore.acquire().await.map_err(|err| { + internal_error(format!("failed to acquire thread list state permit: {err}")) + }) + } +} diff --git a/codex-rs/app-server/src/request_processors/thread_listener_shutdown_tests.rs b/codex-rs/app-server/src/request_processors/thread_listener_shutdown_tests.rs index fec8e6eb3c..e8164da0c5 100644 --- a/codex-rs/app-server/src/request_processors/thread_listener_shutdown_tests.rs +++ b/codex-rs/app-server/src/request_processors/thread_listener_shutdown_tests.rs @@ -37,7 +37,6 @@ use core_test_support::wait_for_event; use pretty_assertions::assert_eq; use std::collections::HashSet; use test_case::test_case; -use tokio::sync::Semaphore; use tokio::sync::mpsc; async fn test_processor( @@ -81,7 +80,7 @@ async fn test_processor( Default::default(), thread_state_manager, thread_watch_manager, - Arc::new(Semaphore::new(/*permits*/ 1)), + Arc::new(ThreadListState::default()), thread_goal_processor, /*state_db*/ None, /*log_db*/ None, diff --git a/codex-rs/app-server/src/request_processors/thread_processor.rs b/codex-rs/app-server/src/request_processors/thread_processor.rs index 01d5b2c650..75d9ce5a9c 100644 --- a/codex-rs/app-server/src/request_processors/thread_processor.rs +++ b/codex-rs/app-server/src/request_processors/thread_processor.rs @@ -514,7 +514,7 @@ pub(crate) struct ThreadRequestProcessor { pub(crate) pending_thread_unloads: PendingThreadUnloads, pub(super) thread_state_manager: ThreadStateManager, pub(super) thread_watch_manager: ThreadWatchManager, - pub(super) thread_list_state_permit: Arc, + pub(super) thread_list_state_permit: Arc, pub(super) thread_goal_processor: ThreadGoalRequestProcessor, pub(super) state_db: Option, pub(super) log_db: Option, @@ -555,7 +555,7 @@ impl ThreadRequestProcessor { pending_thread_unloads: PendingThreadUnloads, thread_state_manager: ThreadStateManager, thread_watch_manager: ThreadWatchManager, - thread_list_state_permit: Arc, + thread_list_state_permit: Arc, thread_goal_processor: ThreadGoalRequestProcessor, state_db: Option, log_db: Option, @@ -1117,12 +1117,7 @@ impl ThreadRequestProcessor { pub(super) async fn acquire_thread_list_state_permit( &self, ) -> Result, JSONRPCErrorError> { - self.thread_list_state_permit - .acquire() - .await - .map_err(|err| { - internal_error(format!("failed to acquire thread list state permit: {err}")) - }) + self.thread_list_state_permit.acquire_exclusive().await } async fn set_app_server_client_info(