Skip to content

Conversation

@mcollina
Copy link
Member

Summary

  • Fixes WebSocket readyState incorrectly reverting from CLOSED (3) to CLOSING (2) after close event fires
  • Uses queueMicrotask() to defer onSocketClose() call, ensuring close event fires after state transition completes
  • Adds guard to prevent duplicate onSocketClose() calls

Test plan

Fixes: #4742

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
@mcollina mcollina requested a review from KhafraDev January 22, 2026 08:45
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.22%. Comparing base (761fce9) to head (09a6cb9).

Files with missing lines Patch % Lines
lib/web/websocket/websocket.js 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4752      +/-   ##
==========================================
- Coverage   93.22%   93.22%   -0.01%     
==========================================
  Files         109      109              
  Lines       34024    34032       +8     
==========================================
+ Hits        31720    31726       +6     
- Misses       2304     2306       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@KhafraDev
Copy link
Member

#4745 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebSocket transitions back from CLOSED to CLOSING

4 participants