fix: queue WebSocket error/close events when closing CONNECTING socket - #4835
Closed
veeceey wants to merge 2 commits into
Closed
fix: queue WebSocket error/close events when closing CONNECTING socket#4835veeceey wants to merge 2 commits into
veeceey wants to merge 2 commits into
Conversation
When close() is called on a WebSocket still in the CONNECTING state, the error and close events were firing synchronously during the close() call. The spec requires these to be queued as tasks so they fire after close() returns. Wrapping the onSocketClose() call in queueMicrotask() ensures the events fire asynchronously, matching browser behavior. Fixes: nodejs#4741
Contributor
Author
Manual test resultsRepro from the issue: Existing test suites pass:
|
Per domenic's feedback on nodejs#4745, the spec requires these events to be queued as a task, not a microtask. setTimeout(fn, 0) correctly queues a macrotask, matching the spec's "queue a task" semantics.
Member
|
Thank you @Uzlopak |
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.
When
close()is called on a WebSocket that's still in the CONNECTING state,failWebsocketConnectioncallshandler.onSocketClose()synchronously, which fires the error and close events during theclose()call itself. The spec says these should be queued as tasks and fire asynchronously afterclose()returns.This wraps the
onSocketClose()call inqueueMicrotask()for the CONNECTING state path so events fire after close() returns, matching browser behavior.Before:
After:
Relevant WPTs:
websockets/interfaces/WebSocket/close/close-connecting.htmlwebsockets/interfaces/WebSocket/close/close-connecting-async.any.jsFixes #4741