Skip to content

fix(chat): bail refreshMessagesUntilTurn poll on session switch - #209

Merged
jackwener merged 1 commit into
mainfrom
yuejing/fe-bug-hunt-4
Jun 24, 2026
Merged

fix(chat): bail refreshMessagesUntilTurn poll on session switch#209
jackwener merged 1 commit into
mainfrom
yuejing/fe-bug-hunt-4

Conversation

@jackwener

Copy link
Copy Markdown
Member

PR-FE-BUG-HUNT-4 (LOW from kenji-led bug-hunt 2026-06-24 round 1).

Bug

`refreshMessagesUntilTurn` polls `readMessages` every 40ms for up to 1200ms while waiting for a freshly-sent user turn to land in the session journal. The setState was already gated on `activeIdRef.current === sessionId`, but the IPC call itself kept firing until the deadline — burning up to 30 IPC roundtrips on a session the user no longer cared about.

Fix

Check `activeIdRef.current !== sessionId` at the top of each loop iteration AND immediately after the `await`. Bail early so the polling cycle stops, not just the setState. Same guard on the deadline-fallthrough `refreshMessages(sessionId)`.

Invisible when the user stays on the session (common case). Saves bandwidth + CPU on fast session-switch scenarios.

Diff

`1 file changed, 13 insertions(+), 3 deletions(-)` — `apps/desktop/src/renderer/main.tsx:2048-2065`. No conflict with the other in-flight PRs.

Verification

Local disk still at 100%, couldn't run `pnpm install && pnpm test` end-to-end. Static logic change, mechanical pattern.

PR-FE-BUG-HUNT-4 (LOW from kenji-led bug-hunt 2026-06-24 round 1):
`refreshMessagesUntilTurn` polls `readMessages` every 40ms for up to
1200ms while waiting for a freshly-sent user turn to land in the
session journal. The setState was already gated on
`activeIdRef.current === sessionId`, but the IPC call itself kept
firing until the deadline — burning bandwidth and CPU on a session
the user no longer cared about.

Fix: check `activeIdRef.current !== sessionId` at the top of each
loop iteration AND immediately after the await. Bail early so the
polling cycle stops, not just the setState. Same guard on the
deadline-fallthrough `refreshMessages(sessionId)`.

Behavior change is invisible when the user stays on the same session
(common case). Saves up to 30 IPC roundtrips per session-switch
during the post-send window when the user navigates fast.

Single function, single file. No conflict with #202 / #204 / #206 /
#207 since none of those touch `refreshMessagesUntilTurn`.
@jackwener
jackwener merged commit 7940540 into main Jun 24, 2026
@jackwener
jackwener deleted the yuejing/fe-bug-hunt-4 branch June 24, 2026 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant