fix: race on rx subscription (#7921)

Fix race where the PTY was sending first chunk before the subscription
to the broadcast
This commit is contained in:
jif-oai authored and GitHub committed 2025-12-12 12:40:54 +01:00
1 parent b99ce883fe
commit f152b16ed9
1 file changed
+4 -1
+4 -1
View File
@@ -54,6 +54,7 @@ impl ExecCommandSession {
pub fn new( pub fn new(
writer_tx: mpsc::Sender<Vec<u8>>, writer_tx: mpsc::Sender<Vec<u8>>,
output_tx: broadcast::Sender<Vec<u8>>, output_tx: broadcast::Sender<Vec<u8>>,
initial_output_rx: broadcast::Receiver<Vec<u8>>,
killer: Box<dyn portable_pty::ChildKiller + Send + Sync>, killer: Box<dyn portable_pty::ChildKiller + Send + Sync>,
reader_handle: JoinHandle<()>, reader_handle: JoinHandle<()>,
writer_handle: JoinHandle<()>, writer_handle: JoinHandle<()>,
@@ -62,7 +63,6 @@ impl ExecCommandSession {
exit_code: Arc<StdMutex<Option<i32>>>, exit_code: Arc<StdMutex<Option<i32>>>,
pair: PtyPairWrapper, pair: PtyPairWrapper,
) -> (Self, broadcast::Receiver<Vec<u8>>) { ) -> (Self, broadcast::Receiver<Vec<u8>>) {
let initial_output_rx = output_tx.subscribe();
( (
Self { Self {
writer_tx, writer_tx,
@@ -177,6 +177,8 @@ pub async fn spawn_pty_process(
let (writer_tx, mut writer_rx) = mpsc::channel::<Vec<u8>>(128); let (writer_tx, mut writer_rx) = mpsc::channel::<Vec<u8>>(128);
let (output_tx, _) = broadcast::channel::<Vec<u8>>(256); 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 mut reader = pair.master.try_clone_reader()?;
let output_tx_clone = output_tx.clone(); let output_tx_clone = output_tx.clone();
@@ -242,6 +244,7 @@ pub async fn spawn_pty_process(
let (session, output_rx) = ExecCommandSession::new( let (session, output_rx) = ExecCommandSession::new(
writer_tx, writer_tx,
output_tx, output_tx,
initial_output_rx,
killer, killer,
reader_handle, reader_handle,
writer_handle, writer_handle,