Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Pending Blob conversions can prevent Close frames from being written while falsely recording them as sent, causing incorrect clean-close reporting and dropping protocol-error status before socket destruction.
2 open issues
What changed in this PR
This PR addresses #6236 by routing WebSocket Close frames through the send queue so they do not overtake pending Blob messages.
Changes:
- Adds support for queuing already serialized frames.
- Exposes the send queue to the connection close helper, retaining a direct-write fallback.
- Adds a regression test for Blob and text messages sent immediately before closing.
| File | Description |
|---|---|
test/websocket/close.js |
Tests message delivery before the close handshake. |
lib/web/websocket/websocket.js |
Shares the send queue with the connection handler. |
lib/web/websocket/sender.js |
Adds addFrame() for serialized frames. |
lib/web/websocket/connection.js |
Queues Close frames behind pending sends when a queue exists. |
Review effort: Balanced
| if (object.sendQueue) { | ||
| // Queue the Close frame behind any pending sends so queued | ||
| // messages are not overtaken (and lost) by the Close frame. | ||
| object.sendQueue.addFrame(frame.createFrame(opcodes.CLOSE)) |
| if (object.sendQueue) { | ||
| // Queue the Close frame behind any pending sends so queued | ||
| // messages are not overtaken (and lost) by the Close frame. | ||
| object.sendQueue.addFrame(frame.createFrame(opcodes.CLOSE)) |
mcollina
left a comment
There was a problem hiding this comment.
Can we do this without adding a queue?
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6237 +/- ##
==========================================
+ Coverage 91.71% 91.72% +0.01%
==========================================
Files 113 113
Lines 44835 44872 +37
==========================================
+ Hits 41121 41160 +39
+ Misses 3714 3712 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ws.close() wrote the Close frame directly to the socket, so it could overtake a pending Blob send still being read in the send queue; the server then closed the connection without ever receiving that message. Graceful closes now go through the existing SendQueue with a new sendHints.close hint, so the Close frame is written after already queued messages. Failure-triggered closes keep writing directly, as the socket is destroyed immediately afterwards. Fixes nodejs#6236.
dad31f8 to
9aad130
Compare
|
Yes, good call. Reworked in 9aad130: the graceful close now goes through the existing (Disclosure: this change was drafted with AI assistance and reviewed/tested by me.) |

Fixes #6236.
Problem
ws.send(blob)followed byws.close()writes the Close frame before the Blob (and before anything queued after it). The close path inconnection.jswrote the Close frame straight to the socket, bypassing theSendQueuethat data frames go through, so it overtook any queued sends (a Blob send waits onblob.arrayBuffer()). A server that follows RFC 6455 stops reading once it has the Close frame, so those messages are silently lost while the client'scloseevent reports code 1000,wasClean: true.Fix
Queue the Close frame behind pending sends instead of writing it directly:
SendQueue.addFrame(frame, cb)enqueues an already serialized frame behind any pending sends.SendQueuecreated in#onConnectionEstablished.closeWebSocketConnectionuses the queue when one exists, falling back to the direct socket write otherwise.Only the client-initiated close path changes; the receiver's Close-frame reply is untouched.
Tests
Added
Close frame is queued behind pending blob sends (#6236)totest/websocket/close.js:send(new Blob(...)),send('after'),close(1000)from an open connection, asserting the server receives both messages before the close handshake completes. The test fails without the fix (Close overtakes the queue, both messages are lost) and passes with it.node --test test/websocket/close.js: 8 passed.borp -p "test/websocket/**/*.js"): 179 passed, 0 failed.AI assistance disclosure: this PR was prepared with the help of an AI coding assistant; the change was reviewed and tested locally before submission.