Repository navigation
http: emit 'drain' after flushing coalesced chunks - #66782
Open
junyup0503 wants to merge 1 commit into
Open
junyup0503 wants to merge 1 commit into
junyup0503 wants to merge 1 commit into
Conversation
Since chunked writes are coalesced while the message is corked, write() decides backpressure from the byte length of the buffered chunks and sets kNeedDrain. When uncork() flushes them, net.Socket accounts string chunks in UTF-16 code units, so with multi-byte text it can stay below its own highWaterMark. It then never needs to drain and never emits 'drain'. If the flushed writes complete asynchronously, the check at the end of uncork() also runs while bytes are still pending, so the response stalls with writableNeedDrain === true and writableLength === 0. Re-check the drain condition once the flushed data has been written, unless the message or its socket has been destroyed in the meantime. Fixes: nodejs#66781 Refs: nodejs#64987 Assisted-by: AI coding agent Signed-off-by: junyup0503 <97156898+junyup0503@users.noreply.github.com>
Collaborator
|
Review requested:
|
Contributor
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. |
The socket reference is captured when uncork() runs, so it is null if the message had no socket yet and the write was buffered in outputData. Re-reading this[kSocket] in the callback checks the socket the data was actually flushed to. In practice _flush() usually clears kNeedDrain before this callback runs, so this is a defensive guard rather than a fix for an observed crash. |
par333k
reviewed
Oct 16, 2026
Comment on lines
+382
to
+384
| // Re-check once the flushed data has been written. | ||
| if (!err && this.writableNeedDrain && !socket.destroyed && | ||
| this.writableLength === 0) { |
There was a problem hiding this comment.
Suggested change
| // Re-check once the flushed data has been written. | |
| if (!err && this.writableNeedDrain && !socket.destroyed && | |
| this.writableLength === 0) { | |
| const currentSocket = this[kSocket]; | |
| if (!err && this.writableNeedDrain && currentSocket && | |
| !currentSocket.destroyed && this.writableLength === 0) { |
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.
Since #64987, chunked writes made while the response is auto-corked are coalesced, and
write()returnsfalsebased on the byte length of the buffered chunks. Whenuncork()flushes them,net.Socketcounts string chunks in UTF-16 code units, so with multi-byte text the socket can stay below its ownhighWaterMarkand never emits'drain'. If the flushed writes complete asynchronously, the check at the end ofuncork()also runs while bytes are still pending, so the response stalls withwritableNeedDrain === trueandwritableLength === 0.This re-checks the drain condition in the write callback of the flushed data, unless the message or its socket has been destroyed. It does not change the accounting itself (e.g. encoding the coalesced strings to
Buffers), to avoid adding cost to the write path that #64987 optimized.The new test uses a
Duplexthat counts strings likenet.Socketand completes writes asynchronously, so it does not depend on OS socket buffer sizes. It hangs onmainwithout the change and passes with it. It also checks that'drain'is not emitted after the response is destroyed from a write callback.parallel/test-http*,parallel/test-stream*,parallel/test-net*,sequential/test-http*andmake lint-jspass locally.Fixes: #66781
Refs: #64987
I used an AI coding agent to help investigate and write this change. I reproduced the issue and verified the fix and the tests myself.