Repository navigation
[v6.x] fix(websocket): fire close when the connection fails before it opens - #5998
Open
christianaurichzm wants to merge 1 commit into
Open
christianaurichzm wants to merge 1 commit into
christianaurichzm wants to merge 1 commit into
Conversation
A WebSocket whose connection fails before it is established (a refused TCP connection, a non-101 response or an invalid opening handshake) fires `error` and never `close`, and its readyState stays CONNECTING. The spec closes the connection after failing it, so `close` has to follow `error`, with code 1006 and readyState CLOSED. onSocketClose is only attached once the handshake succeeds, so failWebsocketConnection now closes a connection that is still CONNECTING itself. The fetch that close() aborts no longer fails the connection a second time, which used to fire a second `error`. This matches main, where it was fixed by nodejs#3565 and nodejs#3566 after v6.x branched. Refs: nodejs#3546 Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
christianaurichzm
force-pushed
the
backport-websocket-close-v6
branch
from
November 19, 2026 20:40
cda2048 to
cf3c0fc
Compare
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.
This relates to...
#3546 was fixed on main by #3565 and #3566, but that landed after v6.x branched, so Node 22's global
WebSocketstill has the bug.Rationale
I ran into this with Node 22 connecting to OBS's websocket server while OBS was closed.
errorfires,closenever does, andreadyStatestays at 0, so anything waiting onclosejust hangs.6.29.0 prints
error 0. 7.x, 8.x and this branch printerror 3and thenclose 1006 3.Changes
v6 doesn't have the handler refactor from main, so this isn't a cherry-pick.
failWebsocketConnectionnow firesclosewith 1006 itself when the connection was still connecting, sinceonSocketCloseonly gets attached after the handshake.I also had to skip the second failure that comes from the aborted fetch when
close()is called while connecting. It was already firingerrortwice there, and with this change the extra one would have landed afterclose.Added
test/websocket/issue-3546.js, all three cases fail without the fix.Features
N/A
Bug Fixes
#3546 on v6.x
Breaking Changes and Deprecations
N/A
Status