fix: prevent SSE event loss in connectEvents onerror handler#122
Open
fallleave001 wants to merge 1 commit into
Open
fix: prevent SSE event loss in connectEvents onerror handler#122fallleave001 wants to merge 1 commit into
fallleave001 wants to merge 1 commit into
Conversation
The old onerror handler unconditionally called settle() + es.close() on every error, then manually reconnected after 1s. This created a race: 1. A recoverable network glitch triggers onerror 2. settle() resolves the Promise immediately -> handleSend proceeds to send the prompt 3. es.close() kills the SSE connection -> all events emitted during the 1s reconnect window (agent_start, message_*, prompt_done, agent_end) are lost 4. The UI stays stuck in 'waiting for model' until reconcileAgentState (15s polling) recovers The fix distinguishes between fatal errors (readyState === CLOSED, e.g. 404/500) where manual reconnect is needed, and recoverable errors (CONNECTING) where EventSource auto-reconnects on its own. For recoverable errors we no longer settle() or close() - the 1.5s timeout in the Promise constructor still ensures handleSend won't hang.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
connectEventsonerrorhandler unconditionally calledsettle()+es.close()on every error, then manually reconnected after 1s. Thiscreated a race condition:
onerrorsettle()resolves the Promise immediately →handleSendproceedsto send the prompt
es.close()kills the SSE connection → all events emitted duringthe 1s reconnect window (
agent_start,message_*,prompt_done,agent_end) are lostreconcileAgentState(15s polling) recoversFix
Distinguish between fatal errors (
readyState === CLOSED, e.g. 404/500)where manual reconnect is needed, and recoverable errors (
CONNECTING)where EventSource auto-reconnects on its own.
For recoverable errors we no longer
settle()orclose()— the 1.5stimeout in the Promise constructor still ensures
handleSendwon'thang. This eliminates the 1s event-loss window that was the root cause
of the stuck UI.
Testing
DevTools) during an active run → SSE auto-reconnects, no events lost,
streaming continues
in, UI recovers via reconcile polling if needed