-
-
Notifications
You must be signed in to change notification settings - Fork 694
fix: WebSocket readyState should not revert from CLOSED to CLOSING #4752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
When calling close() on a CONNECTING WebSocket, the readyState was correctly set to CLOSED during the close event handler, but incorrectly reverted to CLOSING after the handler returned. The issue was that failWebsocketConnection() called onSocketClose() synchronously, which set readyState to CLOSED and fired the close event. After returning, closeWebSocketConnection() would then overwrite the state back to CLOSING. Fix by using queueMicrotask() to defer onSocketClose(), ensuring the close event fires after closeWebSocketConnection() completes. Also add a guard to prevent duplicate onSocketClose() calls. Fixes: #4742
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4752 +/- ##
==========================================
+ Coverage 93.22% 93.26% +0.03%
==========================================
Files 109 109
Lines 34024 34010 -14
==========================================
- Hits 31720 31718 -2
+ Misses 2304 2292 -12 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@KhafraDev that does not link directly to anything on my machine. |
|
Not sure why, this is what I was linking to (domenic's comment):
|
|
I'm not convinced by that argument, unless needed by the spec. The problem is that adding macroticks everywhere will necessarily slow things down and increase memory consumption. cc @domenic |
|
It is needed by the spec? The spec is very explicit! |
Address review feedback to queue the close event as a task at a higher level (in the handler) using setImmediate instead of queueMicrotask in failWebsocketConnection. This is more spec-compliant as the WebSocket spec uses "queue a task" which maps to a macrotask (setImmediate) rather than a microtask. Also updates the test to properly await the close event since it's now queued asynchronously.
Summary
readyStateincorrectly reverting from CLOSED (3) to CLOSING (2) after close event firesqueueMicrotask()to deferonSocketClose()call, ensuring close event fires after state transition completesonSocketClose()callsTest plan
close-connectingtest now passes (was failing with "assert_unreached")close-connecting-asynctest passesFixes: #4742