mirror of
https://github.com/openai/codex.git
synced 2026-11-18 15:01:48 +00:00
Run write_stdin concurrently across terminal sessions (#33645)
## What changed - Allow parallel `write_stdin` tool calls to interact with independent terminal sessions. - Serialize reads and writes within each session so its output buffer and process lifecycle remain consistent. - Prevent active sessions from being pruned while a `write_stdin` interaction is in progress. ## Testing - Added unified exec and code mode coverage that uses two terminal sessions concurrently and verifies output from both. GitOrigin-RevId: 618f5503f4c4670421f680c0f0c27414bc6b2ddd
This commit is contained in:
1 parent
f722a7ebcf
commit
f64233d142
5 files changed
+154
-4
No files matched your search
@@ -82,6 +82,7 @@ pub(crate) struct UnifiedExecProcess {
|
||||
output_closed_notify: Arc<Notify>,
|
||||
cancellation_token: CancellationToken,
|
||||
output_drained: Arc<Notify>,
|
||||
interaction_lock: Arc<Mutex<()>>,
|
||||
state_tx: watch::Sender<ProcessState>,
|
||||
state_rx: watch::Receiver<ProcessState>,
|
||||
output_task: Option<JoinHandle<()>>,
|
||||
@@ -123,6 +124,7 @@ impl UnifiedExecProcess {
|
||||
output_closed_notify,
|
||||
cancellation_token,
|
||||
output_drained,
|
||||
interaction_lock: Arc::new(Mutex::new(())),
|
||||
state_tx,
|
||||
state_rx,
|
||||
output_task: None,
|
||||
@@ -178,6 +180,10 @@ impl UnifiedExecProcess {
|
||||
Arc::clone(&self.output_drained)
|
||||
}
|
||||
|
||||
pub(super) fn interaction_lock(&self) -> Arc<Mutex<()>> {
|
||||
Arc::clone(&self.interaction_lock)
|
||||
}
|
||||
|
||||
pub(super) fn has_exited(&self) -> bool {
|
||||
let state = self.state_rx.borrow().clone();
|
||||
match &self.process_handle {
|
||||
|
||||
@@ -659,6 +659,19 @@ impl UnifiedExecProcessManager {
|
||||
) -> Result<ExecCommandToolOutput, UnifiedExecError> {
|
||||
let process_id = request.process_id;
|
||||
|
||||
// Different terminal sessions can be polled concurrently, but reads and
|
||||
// writes against one terminal must not overlap because they share a
|
||||
// draining output buffer and process lifecycle.
|
||||
let locked_process = {
|
||||
let store = self.process_store.lock().await;
|
||||
let entry = store
|
||||
.processes
|
||||
.get(&process_id)
|
||||
.ok_or(UnifiedExecError::UnknownProcessId { process_id })?;
|
||||
Arc::clone(&entry.process)
|
||||
};
|
||||
let _interaction_guard = locked_process.interaction_lock().lock_owned().await;
|
||||
|
||||
let PreparedProcessHandles {
|
||||
process,
|
||||
output_buffer,
|
||||
@@ -674,7 +687,9 @@ impl UnifiedExecProcessManager {
|
||||
process_id,
|
||||
tty,
|
||||
..
|
||||
} = self.prepare_process_handles(process_id).await?;
|
||||
} = self
|
||||
.prepare_process_handles(process_id, &locked_process)
|
||||
.await?;
|
||||
let mut status_after_write = None;
|
||||
|
||||
if !request.input.is_empty() {
|
||||
@@ -842,12 +857,16 @@ impl UnifiedExecProcessManager {
|
||||
async fn prepare_process_handles(
|
||||
&self,
|
||||
process_id: i32,
|
||||
expected_process: &Arc<UnifiedExecProcess>,
|
||||
) -> Result<PreparedProcessHandles, UnifiedExecError> {
|
||||
let mut store = self.process_store.lock().await;
|
||||
let entry = store
|
||||
.processes
|
||||
.get_mut(&process_id)
|
||||
.ok_or(UnifiedExecError::UnknownProcessId { process_id })?;
|
||||
if !Arc::ptr_eq(&entry.process, expected_process) {
|
||||
return Err(UnifiedExecError::UnknownProcessId { process_id });
|
||||
}
|
||||
entry.last_used = Instant::now();
|
||||
let OutputHandles {
|
||||
output_buffer,
|
||||
@@ -1355,14 +1374,23 @@ impl UnifiedExecProcessManager {
|
||||
return None;
|
||||
}
|
||||
|
||||
let meta: Vec<(i32, Instant, bool)> = store
|
||||
let mut meta: Vec<(i32, Instant, bool)> = store
|
||||
.processes
|
||||
.iter()
|
||||
.map(|(id, entry)| (*id, entry.last_used, entry.process.has_exited()))
|
||||
.collect();
|
||||
|
||||
if let Some(process_id) = Self::process_id_to_prune_from_meta(&meta) {
|
||||
return store.remove(process_id);
|
||||
while let Some(process_id) = Self::process_id_to_prune_from_meta(&meta) {
|
||||
// Do not prune processes being held by write_stdin.
|
||||
if let Some(interaction_lock) = store
|
||||
.processes
|
||||
.get(&process_id)
|
||||
.map(|entry| entry.process.interaction_lock())
|
||||
&& let Ok(_interaction_guard) = interaction_lock.try_lock_owned()
|
||||
{
|
||||
return store.remove(process_id);
|
||||
}
|
||||
meta.retain(|(id, _, _)| *id != process_id);
|
||||
}
|
||||
|
||||
None
|
||||
|
||||
Reference in new issue
Block a user