fix(opencode): handle client disconnect in SSE event route writes#18538
Open
zaxbysauce wants to merge 1 commit intoanomalyco:devfrom
Open
fix(opencode): handle client disconnect in SSE event route writes#18538zaxbysauce wants to merge 1 commit intoanomalyco:devfrom
zaxbysauce wants to merge 1 commit intoanomalyco:devfrom
Conversation
Wrap writeSSE calls in try/catch within the for-await queue drain loops in both event.ts and global.ts route handlers. When a client disconnects abruptly (or writeSSE throws due to backpressure), the unhandled error propagates out of the for-await loop, corrupting server state and causing subsequent requests to fail with 'Unexpected EOF' errors. The fix catches writeSSE failures, logs the disconnection, and returns cleanly — allowing the finally block to call stop() which clears the heartbeat interval and unsubscribes from the bus.
Contributor
|
The following comment was made by an LLM, it may be inaccurate: Related PR FoundPR #15147: fix(opencode): handle client disconnect in SSE stream writes This is the original PR that addressed the same bug (client disconnect handling in SSE endpoints). The current PR (18538) is explicitly described as a rebase of that fix onto the refactored codebase where the SSE routes were moved from inline This is not a duplicate in the traditional sense—rather, PR #15147 is the predecessor fix that the current PR builds upon after the codebase refactoring. |
6 tasks
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.
Issue for this PR
Closes #15149
Type of change
What does this PR do?
The
/eventand/global/eventSSE endpoints use an AsyncQueue drained by afor awaitloop that callsstream.writeSSE(). When a client disconnects abruptly — or when rapid-fire bus events cause writeSSE backpressure — the thrown error propagates out of thefor awaitloop unhandled. This corrupts server state, causing subsequent requests to fail with "Unexpected EOF" errors and killing active sessions.This is the same bug as the original PR #15147, rebased onto the current
devbranch where the SSE routes were refactored from inlineserver.tshandlers into separateroutes/event.tsandroutes/global.tsfiles using theAsyncQueuepattern.The fix wraps each
writeSSEcall inside thefor awaitdrain loop in a try/catch. On failure, it logs the disconnection and returns cleanly, letting thefinallyblock runstop()to clear the heartbeat interval and unsubscribe from the bus. Two files changed, identical pattern in both.How did you verify your code works?
bash.ts(fire-and-forgetctx.metadata()on every stdout chunk) throughSession.updatePart()→Bus.publish(PartUpdated)→AsyncQueue.push()→stream.writeSSE()failure → unhandled throw → session deathserver.tsinline SSE handler from the original PRScreenshots / recordings
Not a UI change.
Checklist
If you do not follow this template your PR will be automatically rejected.