diff --git a/codex-rs/utils/pty/src/lib.rs b/codex-rs/utils/pty/src/lib.rs index cd98107ca..38e86bd2a 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>, output_tx: broadcast::Sender>, + initial_output_rx: broadcast::Receiver>, killer: Box, reader_handle: JoinHandle<()>, writer_handle: JoinHandle<()>, @@ -62,7 +63,6 @@ impl ExecCommandSession { exit_code: Arc>>, pair: PtyPairWrapper, ) -> (Self, broadcast::Receiver>) { - 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::>(128); let (output_tx, _) = broadcast::channel::>(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,