Repository navigation
fix(websocket): attach stream listeners before the socket opens - #1189
Merged
Merged
Conversation
EhabY
force-pushed
the
fix/ws-listeners-before-open
branch
from
November 12, 2026 15:29
5e3228b to
3da7eb4
Compare
EhabY
marked this pull request as ready for review
November 12, 2026 16:06
EhabY
force-pushed
the
fix/ws-listeners-before-open
branch
from
November 12, 2026 16:19
3da7eb4 to
cb77fe0
Compare
EhabY
force-pushed
the
fix/ws-listeners-before-open
branch
from
November 12, 2026 18:08
cb77fe0 to
326b989
Compare
Merged
EhabY
added a commit
that referenced
this pull request
Nov 13, 2026
streamBuildLogs awaits socket.opened and chains socket.closed since the OneWayWebSocket change in #1189. The awaitBuild tests mocked the socket without either, so streamBuildLogs threw, awaitBuild logged "Could not stream the build logs" and never closed the socket. Move the tasks panel's open socket mock to testHelpers as createMockOneWaySocket and use it for every build log socket.
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.
Streams now return before their socket opens, so every listener is attached before any event can fire.
wsemits frames that arrive with the handshake on aprocess.nextTick, ahead of any awaited continuation, so awaitingopenbefore subscribing dropped the first message on every connect and reconnect: agent metadata, the workspace watch, and log backlogs. The same timing kept theReconnectingWebSocketopenhandler from ever running, so watchers never reachedCONNECTEDand their backoff never reset on open. Both are fixed.OneWayWebSocketexposesopenedandclosedpromises. Log streams attach all listeners up front and thenawait socket.opened, so they still reject on a failed handshake without printing anything, while errors after open (even ones arriving with the handshake) are reported. The tasks panel subscribes viastream.closed.then(...), which cannot miss an early close.ReconnectingWebSocket.connect()now numbers its attempts, so a restart while a factory is pending always connects the latest attempt and closes superseded sockets, and adisconnect()during a certificate refresh is no longer undone when the refresh succeeds. The SSE fallback only served servers older than 2.22 (#620), so it is removed now that 2.29.0 is the minimum (#1186), together with theeventsourcedependency and the logger's protocol parameter.Closes #1141.
Plan and decision log
Problem (verified)
ws, a listener attached a few awaits afteropendropped 20/20 messages; attached before open, 0/20.eventsource5.1.5 had the same gap: 20/20 dropped after 3+ promise hops.ReconnectingWebSocket.connect(), thencreate(), then the caller'sawait client.watchX()beforeaddEventListener.closethat arrives with the handshake (a finished build: backlog, then close) was lost too, and the tasks panel refreshes on that close.ReconnectingWebSocketopenlistener was attached afteropenhad fired. The test fake hid this because its factory resolved beforefireOpen().Options considered
setImmediate(about 5 lines). Rejected: it depends on every caller subscribing within the same await chain, which is an ordering assumption rather than a guarantee.watchAgentMetadatareturnsnew OneWayWebSocket(...)), what partysocket/reconnecting-websocket do, and how matter.js fixed this exactwsbug.Ordering guarantee
await createHttpAgent), and listeners are attached in the same microtask chain (ReconnectingWebSocketright after the factory, callers right afterawait client.watchX()). I/O callbacks andprocess.nextTickwork scheduled from a microtask run only after that chain drains, so no event can precede its listener.Decisions
await client.watchX()now means "initialized", not "connected"; no caller relied on "connected".opened/closedpromises onOneWayWebSocket, created in the constructor, rather thanonClosecallbacks or open-flag tracking. A promise settles once and late subscribers still see it, which mirrorsWebSocketStream.openflag. Attaching it afterawait socket.openedmissed protocol errors in bytes delivered with the handshake (verified against a raw server: the error line was missing before, present now).reconnect()while a factory was pending raced: the older socket could open first and get the newer one cancelled, or be overwritten and leaked. Stale factory results are closed and stale rejections ignored. This predates the PR, but the faster factories made the window more visible.ReconnectingWebSocketroutes error and close throughhandleConnectionError(401, certificate refresh, unrecoverable codes, retry) without forwarding them to consumers. That keeps the behavior consumers saw before, and an error followed by a close gives a single retry. Only the certificate path awaits; it now re-checks forDISCONNECTEDafter the refresh (raised in review).EventStreamLogger's"WS" | "SSE"parameter.UnidirectionalStreamis kept:OneWayWebSocketandReconnectingWebSocketboth implement it, and consumers and test fakes depend only on the interface.CoderApi(message on connect, then on reconnect; state reachesCONNECTED), oneReconnectingWebSockettest where a superseded factory resolves late, and one wheredisconnect()runs during a certificate refresh; all fail on the old code. The existing handshake-rejection test now goes throughstreamBuildLogsand also checks that nothing is printed after a failed handshake.Validation
pnpm typecheck,pnpm lint,pnpm format:checkandpnpm buildpass; the build no longer bundleseventsource.pnpm test: 3163 passed, 6 skipped.Generated by Coder Agents on behalf of @EhabY.