http: fix drain event with cork/uncork in ServerResponse

Closed
TH
1y ago
+77-1

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().