Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

fix: race on rx subscription (#7921)

jif-oaif152b16ed9ea
diff --git a/codex-rs/utils/pty/src/lib.rs b/codex-rs/utils/pty/src/lib.rs
index cd98107ca0..38e86bd2a6 100644
--- a/codex-rs/utils/pty/src/lib.rs
+++ b/codex-rs/utils/pty/src/lib.rs
@@ -54,6 +54,7 @@ impl ExecCommandSession {
pub fn new(
writer_tx: mpsc::Sender<Vec<u8>>,
output_tx: broadcast::Sender<Vec<u8>>,
+ initial_output_rx: broadcast::Receiver<Vec<u8>>,
killer: Box<dyn portable_pty::ChildKiller + Send + Sync>,
reader_handle: JoinHandle<()>,
writer_handle: JoinHandle<()>,
@@ -62,7 +63,6 @@ impl ExecCommandSession {
exit_code: Arc<StdMutex<Option<i32>>>,
pair: PtyPairWrapper,
) -> (Self, broadcast::Receiver<Vec<u8>>) {
- let initial_output_rx = output_tx.subscribe();
(
Self {
writer_tx,
@@ -177,6 +177,8 @@ pub async fn spawn_pty_process(
let (writer_tx, mut writer_rx) = mpsc::channel::<Vec<u8>>(128);
let (output_tx, _) = broadcast::channel::<Vec<u8>>(256);
+ // Subscribe before starting the reader thread.
+ let initial_output_rx = output_tx.subscribe();
let mut reader = pair.master.try_clone_reader()?;
let output_tx_clone = output_tx.clone();
@@ -242,6 +244,7 @@ pub async fn spawn_pty_process(
let (session, output_rx) = ExecCommandSession::new(
writer_tx,
output_tx,
+ initial_output_rx,
killer,
reader_handle,
writer_handle,