Conversation
…ll at once A caller loading a large scene (one element per message, no send-side batching) can burst thousands of WebSocket messages at once. Dispatching each one synchronously inside onmessage - real work per message: protobuf decode plus a Three.js scene mutation - pinned the main thread for the whole burst, which made the tab look/become unresponsive and, past the browser's own patience, drop the WebSocket connection entirely. ViewerConnection now queues incoming messages and drains them, in order, in up to FRAME_BUDGET_MS (8ms) slices per requestAnimationFrame, picking up where it left off on the next frame. The queue uses a head index instead of Array.shift() per message to avoid an O(n^2) drain, and a throw from one message's dispatch is caught so it can't strand every message queued after it. dispose() cancels a pending drain and drops whatever is still queued. Adds tests/viewer_connection.test.ts, covering the multi-frame drain itself and dispose() cancelling a pending one. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This branch has not been deployed
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.
Summary
onmessage(protobuf decode plus a Three.js scene change), which blocked the main thread for the whole burst. The tab froze and could eventually drop the WebSocket connection.ViewerConnectionnow queues incoming messages and drains them in order, in slices of up toFRAME_BUDGET_MS(8 ms) perrequestAnimationFrame. The queue tracks a head index instead of callingArray.shift(), so draining isn't O(n²). An error while handling one message is caught so it can't block the messages after it.dispose()cancels any pending drain and drops the queue.Stacked on #31; merge that first. Next in stack: #33.
Test plan
tests/viewer_connection.test.ts(a drain spread over several frames;dispose()cancelling a pending drain)🤖 Generated with Claude Code