http: fix drain event with cork/uncork#64038
Open
davidje13 wants to merge 1 commit into
Open
Conversation
When using cork() and uncork() with ServerResponse, the drain event was not reliably emitted after uncorking. This occurred because the uncork() method did not check if a drain was pending (kNeedDrain flag) after flushing the chunked buffer. This fix ensures that when uncork() successfully flushes buffered data and a drain was needed, the drain event is emitted immediately. This commit is a copy of PR nodejs#60437 (abandoned) with minor linting fixes. Fixes: nodejs#60432 Signed-off-by: David Evans <davidje13@users.noreply.github.com>
Collaborator
|
Review requested:
|
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.
Fixes #60432
This is a copy of PR #60437, so credit for the fix goes to @ThierryMT. Sadly that PR was abandoned and I wasn't able to get a response from the author. I've just applied their changes to the latest main, patched up the lint errors, and applied the suggested change to the tests. I've signed the certificate of origin on the basis that the test is based on the reproduction I provided in the original bug report #60432 (hence, created in part by me), but let me know if this is too tenuous and I can write a new test from scratch instead. The fix itself is so trivial that I'm not sure there's a way to write it which isn't effectively the same code.
Original PR description:
When using
cork()anduncork()withServerResponse, thedrainevent was not reliably emitted after uncorking. This occurred because theuncork()method did not check if a drain was pending (kNeedDrainflag) after flushing the chunked buffer.The Problem:
write()buffers data inkChunkedBufferinstead of sending immediatelywrite()returnsfalse,kNeedDrainis set totrueuncork()is called, it flushes the buffer by calling_send()uncork()never checked if drain was needed or emitted the eventdrainevent that would never fireThe Fix:
This commit ensures that when
uncork()successfully flushes buffered data and a drain was needed, thedrainevent is emitted immediately.Changes:
OutgoingMessage.prototype.uncork()inlib/_http_outgoing.js_send()calldrainevent when appropriateTesting:
The issue could be reproduced by using
cork()with multiple large writes and checking fordrainevents. With this fix, thedrainevent now fires correctly afteruncork().