From ffb8a6a9c3fd6454450ee8c40772a96ca0762d2a Mon Sep 17 00:00:00 2001 From: Alex Lyn Date: Fri, 30 Jan 2026 10:29:59 +0800 Subject: [PATCH] agent: fix misleading tokio::select! biased comment in do_read_stream The previous comment incorrectly implied that `biased` prevents data loss and the exit notifier would never be polled before all buffered data is read. And the detailed info can be seen from the document: https://docs.rs/tokio/latest/src/tokio/macros/select.rs.html#67 Tokio's `biased` only makes polling order deterministic(top-to-bottom) when multiple branches are ready in the same poll, and it makes fairness the caller's responsibility. Output can still be truncated if the exit notification becomes ready while `read_stream` is pending. This change updates the comment to reflect the actual semantics and caveats. No functional behavior change. Signed-off-by: Alex Lyn --- src/agent/src/rpc.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index 03fffaf50f..143c3a2a4c 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -718,11 +718,17 @@ impl AgentService { // - Waker: https://doc.rust-lang.org/std/task/struct.Waker.html // - Tokio select!: https://docs.rs/tokio/latest/tokio/macro.select.html let data = tokio::select! { - // Poll the futures in the order they appear from top to bottom - // it is very important to avoid data loss. If there is still - // data in the buffer and read_stream branch will return - // Poll::Ready so that the term_exit_notifier will never polled - // before all data were read. + // Use `biased` to make the polling order deterministic (top-to-bottom). + // This ensures that *when multiple branches are ready at the same time*, + // we prefer reading pending output over reacting to the exit notification. + // + // Note: `biased` does NOT guarantee that we won't lose output. If the exit + // notification becomes ready while `read_stream` is still pending, the + // exit branch may be selected and we may stop reading before draining the + // remaining buffered data. + // + // Detailed information, please refer to Tokio doc for more information: + // https://docs.rs/tokio/latest/src/tokio/macros/select.rs.html#67 biased; v = &mut read_fut => v?,