Skip to content

Commit ce69fc9

Browse files
committed
http: emit drain on socket takeover and avoid stale HWM reuse
When OutgoingMessage transitions from pre-socket buffering (Path B) to socket-connected writing (Path A), the backpressure domain changes — subsequent writes go directly to the socket, which enforces its own backpressure via socket.write() return values. The OM should emit drain at this transition point to signal that its buffer is clear and the caller can resume writing under the socket backpressure regime. Previously, _flush() gated drain emission on writableLength === 0 which included socket.writableLength. This conflated two independent backpressure domains: the OM pre-socket buffer and the socket kernel write queue. When the socket had a higher writableHighWaterMark than the OM (e.g. agent-reused socket from a prior request), the socket was never backpressured and never emitted drain, causing a permanent deadlock. Additionally, avoid reusing a pooled socket in http.Agent when its writableHighWaterMark differs from the request highWaterMark, so that the user backpressure threshold is respected for the common case of the built-in Agent. Signed-off-by: Naman Trivedi <trivenay@amazon.com> Fixes: #64680 Refs: #64653 Refs: #62936
1 parent 99a7ef6 commit ce69fc9

5 files changed

Lines changed: 133 additions & 5 deletions

lib/_http_agent.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,16 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
394394
const sockLen = freeLen + this.sockets[name].length;
395395

396396
// Reusing a socket from the pool.
397+
// If the caller specified a highWaterMark that differs from the pooled
398+
// socket's writableHighWaterMark, sync the socket's HWM so that
399+
// backpressure semantics match what the caller requested.
400+
if (socket && options.highWaterMark != null &&
401+
socket.writableHighWaterMark !== options.highWaterMark) {
402+
debug('sync reused socket HWM (socket=%d, request=%d)',
403+
socket.writableHighWaterMark, options.highWaterMark);
404+
socket._writableState.highWaterMark = options.highWaterMark;
405+
}
406+
397407
if (socket) {
398408
asyncResetHandle(socket);
399409
this.reuseSocket(socket, req);

lib/_http_outgoing.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,10 @@ OutgoingMessage.prototype._flush = function _flush() {
12081208
if (this.finished) {
12091209
// This is a queue to the server or client to bring in the next this.
12101210
this._finish();
1211-
} else if (this[kNeedDrain] && this.writableLength === 0) {
1211+
} else if (this[kNeedDrain]) {
1212+
// _flushOutput() handed all buffered data to the socket; the OM's
1213+
// backpressure concern is resolved. Subsequent writes go directly
1214+
// to the socket where socket-level backpressure takes over.
12121215
this[kNeedDrain] = false;
12131216
this.emit('drain');
12141217
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
// Regression test: when a pooled socket's writableHighWaterMark differs from
4+
// the new request's highWaterMark, the agent must sync the socket's HWM so
5+
// that backpressure semantics match what the caller requested.
6+
//
7+
// See: https://github.com/nodejs/node/issues/64680
8+
9+
const common = require('../common');
10+
const assert = require('assert');
11+
const http = require('http');
12+
13+
const server = http.createServer(common.mustCall((req, res) => {
14+
req.resume();
15+
req.on('end', () => res.end('ok'));
16+
}, 2));
17+
18+
server.listen(0, common.mustCall(() => {
19+
const port = server.address().port;
20+
const agent = new http.Agent({ keepAlive: true });
21+
22+
// Request A: creates socket with HWM=1MB.
23+
http.request({
24+
host: 'localhost', port, method: 'POST', agent,
25+
highWaterMark: 1024 * 1024,
26+
}, common.mustCall((res) => {
27+
res.resume();
28+
res.on('end', common.mustCall(() => {
29+
// Wait for socket to return to pool.
30+
setTimeout(common.mustCall(requestB), 100);
31+
}));
32+
})).end('x');
33+
34+
function requestB() {
35+
const freeCount = Object.values(agent.freeSockets).flat().length;
36+
assert.strictEqual(freeCount, 1);
37+
38+
// Request B: HWM=10KB — agent must sync the reused socket's HWM.
39+
const reqB = http.request({
40+
host: 'localhost', port, method: 'POST', agent,
41+
highWaterMark: 10 * 1024,
42+
}, common.mustCall((res) => {
43+
res.resume();
44+
res.on('end', common.mustCall(() => {
45+
server.close();
46+
}));
47+
}));
48+
49+
reqB.on('socket', common.mustCall((socket) => {
50+
// Socket HWM must be synced to the request's value.
51+
assert.strictEqual(socket.writableHighWaterMark, 10 * 1024);
52+
}));
53+
54+
reqB.end('y');
55+
}
56+
}));

test/parallel/test-http-outgoing-drain-writable-length.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ const server = http.createServer(common.mustCall((req, res) => {
3737
assert.strictEqual(res.writableNeedDrain, true);
3838

3939
res.on('drain', common.mustCall(() => {
40-
assert.strictEqual(
41-
res.writableLength, 0,
42-
`'drain' fired with writableLength=${res.writableLength}`,
43-
);
40+
// After the changeover from pre-socket buffering to socket-connected
41+
// writing, drain fires once the OM's own buffer (outputData) has been
42+
// handed off to the socket. The socket may still have data queued
43+
// in libuv — that's the socket's backpressure domain, handled by
44+
// subsequent write() calls returning false via Path A.
45+
assert.strictEqual(res.outputSize, 0);
4446
res.end();
4547
server.close();
4648
}));
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
// Regression test: when _flush() hands buffered data to a socket whose
4+
// writableHighWaterMark is higher than the OutgoingMessage's kHighWaterMark,
5+
// drain must still fire. Previously, _flush() gated drain emission on
6+
// writableLength === 0, which included socket.writableLength — but the
7+
// socket was never backpressured (data < socket HWM), so drain never fired.
8+
//
9+
// See: https://github.com/nodejs/node/issues/64680
10+
11+
const common = require('../common');
12+
const assert = require('assert');
13+
const http = require('http');
14+
15+
// Server that delays reading to keep socket.writableLength > 0 during flush.
16+
const server = http.createServer(common.mustCall((req, res) => {
17+
setTimeout(() => {
18+
req.resume();
19+
req.on('end', () => res.end('ok'));
20+
}, 500);
21+
}, 2));
22+
23+
server.listen(0, common.mustCall(() => {
24+
const port = server.address().port;
25+
const agent = new http.Agent({ keepAlive: true });
26+
27+
// Request A: creates socket with HWM=2MB.
28+
http.request({
29+
host: 'localhost', port, method: 'POST', agent,
30+
highWaterMark: 2 * 1024 * 1024,
31+
}, common.mustCall((res) => {
32+
res.resume();
33+
res.on('end', common.mustCall(() => {
34+
// Wait for socket to return to pool.
35+
setTimeout(common.mustCall(() => {
36+
// Request B: default HWM (64KB), reuses socket (HWM=2MB).
37+
// Write 500KB: above OM HWM (64KB), below socket HWM (2MB).
38+
const reqB = http.request({
39+
host: 'localhost', port, method: 'POST', agent,
40+
}, common.mustCall((res2) => {
41+
res2.resume();
42+
res2.on('end', common.mustCall(() => {
43+
server.close();
44+
}));
45+
}));
46+
47+
const result = reqB.write(Buffer.alloc(500 * 1024));
48+
assert.strictEqual(result, false);
49+
50+
// Drain must fire — no deadlock.
51+
reqB.on('drain', common.mustCall(() => {
52+
reqB.end();
53+
}));
54+
}), 100);
55+
}));
56+
})).end('x');
57+
}));

0 commit comments

Comments
 (0)