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
This commit is contained in:
Charlie Marsh
2026-11-17 00:58:52 +00:00
committed by copyberry
parent 885a1df5ee
commit 4f556fdd6d
6 changed files with 40 additions and 22 deletions
+2 -2
View File
@@ -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(
@@ -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;
@@ -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<dyn ThreadStore>,
outgoing: Arc<OutgoingMessageSender>,
thread_list_state_permit: Arc<Semaphore>,
thread_list_state_permit: Arc<ThreadListState>,
}
impl ProjectRequestProcessor {
pub(crate) fn new(
thread_store: Arc<dyn ThreadStore>,
outgoing: Arc<OutgoingMessageSender>,
thread_list_state_permit: Arc<Semaphore>,
thread_list_state_permit: Arc<ThreadListState>,
) -> Self {
Self {
thread_store,
@@ -291,12 +292,7 @@ impl ProjectRequestProcessor {
async fn acquire_thread_list_state_permit(
&self,
) -> Result<SemaphorePermit<'_>, 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
}
}
@@ -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<SemaphorePermit<'_>, JSONRPCErrorError> {
self.semaphore.acquire().await.map_err(|err| {
internal_error(format!("failed to acquire thread list state permit: {err}"))
})
}
}
@@ -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,
@@ -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<Semaphore>,
pub(super) thread_list_state_permit: Arc<ThreadListState>,
pub(super) thread_goal_processor: ThreadGoalRequestProcessor,
pub(super) state_db: Option<StateDbHandle>,
pub(super) log_db: Option<LogDbLayer>,
@@ -555,7 +555,7 @@ impl ThreadRequestProcessor {
pending_thread_unloads: PendingThreadUnloads,
thread_state_manager: ThreadStateManager,
thread_watch_manager: ThreadWatchManager,
thread_list_state_permit: Arc<Semaphore>,
thread_list_state_permit: Arc<ThreadListState>,
thread_goal_processor: ThreadGoalRequestProcessor,
state_db: Option<StateDbHandle>,
log_db: Option<LogDbLayer>,
@@ -1117,12 +1117,7 @@ impl ThreadRequestProcessor {
pub(super) async fn acquire_thread_list_state_permit(
&self,
) -> Result<SemaphorePermit<'_>, 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(