Skip to content

fix(websocket): avoid double-closing canceled stream readers#5105

Merged
tsctx merged 1 commit intonodejs:mainfrom
colinaaa:fix/websocketstream-readable-cancel-close-5104
Apr 24, 2026
Merged

fix(websocket): avoid double-closing canceled stream readers#5105
tsctx merged 1 commit intonodejs:mainfrom
colinaaa:fix/websocketstream-readable-cancel-close-5104

Conversation

@colinaaa
Copy link
Copy Markdown
Contributor

@colinaaa colinaaa commented Apr 24, 2026

This relates to...

Closes #5103
Closes #5104

Rationale

WebSocketStream could throw ERR_INVALID_STATE when readable.cancel() closed the internal controller before the later clean-close path tried to close it again. This keeps the readable shutdown path idempotent and prevents the uncaught exception.

Changes

  • reuse readableStreamClose() in the clean WebSocketStream socket close path instead of calling ReadableStreamDefaultController.close() directly
  • add a regression test that cancels the readable stream before a clean close handshake and asserts no uncaught exception escapes

Features

N/A

Bug Fixes

  • fix a WebSocketStream double-close path that could throw TypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closed
  • cover the cancel-then-clean-close sequence with a websocket stream regression test

Breaking Changes and Deprecations

N/A

Status

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.15%. Comparing base (67ca48c) to head (d9b7fef).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5105      +/-   ##
==========================================
+ Coverage   93.13%   93.15%   +0.01%     
==========================================
  Files         110      110              
  Lines       35826    35826              
==========================================
+ Hits        33368    33375       +7     
+ Misses       2458     2451       -7     

☔ 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.

@mcollina mcollina requested review from KhafraDev and tsctx and removed request for KhafraDev April 24, 2026 08:04
@tsctx tsctx merged commit 94c9dea into nodejs:main Apr 24, 2026
36 checks passed
@github-actions github-actions Bot mentioned this pull request May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants