Skip to content

Commit 63b516b

Browse files
committed
http: fix drain event with cork/uncork in ServerResponse
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: #60432
1 parent 6176222 commit 63b516b

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/_http_outgoing.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,20 @@ OutgoingMessage.prototype.uncork = function uncork() {
287287
callbacks.push(buf[n + 2]);
288288
}
289289
}
290-
this._send(crlf_buf, null, callbacks.length ? (err) => {
290+
const ret = this._send(crlf_buf, null, callbacks.length ? (err) => {
291291
for (const callback of callbacks) {
292292
callback(err);
293293
}
294294
} : null);
295295

296296
this[kChunkedBuffer].length = 0;
297297
this[kChunkedLength] = 0;
298+
299+
// If we successfully flushed and had pending drain, emit it
300+
if (ret && this[kNeedDrain]) {
301+
this[kNeedDrain] = false;
302+
this.emit('drain');
303+
}
298304
};
299305

300306
OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {

0 commit comments

Comments
 (0)