Skip to content

Conversation

@ThierryMT
Copy link

Fixes #60432

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.

The Problem:

  • When corked, write() buffers data in kChunkedBuffer instead of sending immediately
  • If write() returns false, kNeedDrain is set to true
  • When uncork() is called, it flushes the buffer by calling _send()
  • After flushing, uncork() never checked if drain was needed or emitted the event
  • This left the response waiting for a drain event that would never fire

The Fix:
This commit ensures that when uncork() successfully flushes buffered data and a drain was needed, the drain event is emitted immediately.

Changes:

  • Modified OutgoingMessage.prototype.uncork() in lib/_http_outgoing.js
  • Capture return value from final _send() call
  • Check if drain was pending after successful flush
  • Emit drain event when appropriate

Testing:
The issue could be reproduced by using cork() with multiple large writes and checking for drain events. With this fix, the drain event now fires correctly after uncork().

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Oct 27, 2025
@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 27, 2025
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Member

@ThierryMT can you add a test?

@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 27, 2025
@nodejs-github-bot
Copy link
Collaborator

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.

Fixes: nodejs#60432
@ThierryMT ThierryMT force-pushed the fix-drain-event-cork-uncork branch from 63b516b to 972f733 Compare October 27, 2025 17:26
Add test to verify that the drain event is properly emitted when
using cork() and uncork() with ServerResponse.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

drain event is unreliable when using cork/uncork with ServerResponse

4 participants