http: fix keep-alive socket reuse race in requestOnFinish · nodejs/node@ee02966 · GitHub
Skip to content

Commit ee02966

Browse files
martinslotamarco-ippolito
authored andcommitted
http: fix keep-alive socket reuse race in requestOnFinish
When the HTTP response ends before the request's 'finish' event fires, responseOnEnd() and requestOnFinish() can both call responseKeepAlive(), corrupting the socket that the agent may have already handed to another request. This commit adds a !req.destroyed guard to requestOnFinish() so the second call is skipped when the socket has already been released. Fixes: #60001 PR-URL: #61710 Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent 9f45080 commit ee02966

3 files changed

Lines changed: 219 additions & 1 deletion

File tree

lib/_http_client.js

Lines changed: 5 additions & 1 deletion
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
'use strict';
2+
3+
// Regression test for a keep-alive socket reuse race condition.
4+
//
5+
// The race is between responseOnEnd() and requestOnFinish(), both of which
6+
// can call responseKeepAlive(). The window is: req.end() has been called,
7+
// the socket write has completed (writableFinished true), but the write
8+
// callback that emits the 'finish' event has not fired yet.
9+
//
10+
// With plain HTTP the window is normally too narrow to hit. This test
11+
// widens it by delaying every client-socket write *callback* by a few
12+
// milliseconds (the actual I/O is not delayed, so writableFinished becomes
13+
// true while the 'finish'-emitting callback is still pending).
14+
//
15+
// With Expect: 100-continue, the server responds quickly while the client
16+
// delays req.end() just slightly (setTimeout 0), creating the perfect
17+
// timing for the response to arrive in that window.
18+
//
19+
// On unpatched Node, the double responseKeepAlive() call corrupts the
20+
// socket by stripping a subsequent request's listeners and emitting a
21+
// spurious 'free' event, causing requests to hang / time out.
22+
23+
const common = require('../common');
24+
const assert = require('assert');
25+
const http = require('http');
26+
27+
const REQUEST_COUNT = 100;
28+
const agent = new http.Agent({ keepAlive: true, maxSockets: 1 });
29+
30+
// Delay every write *callback* on the client socket so that
31+
// socket.writableLength drops to 0 (writableFinished becomes true) before
32+
// the callback that ultimately emits the 'finish' event fires. With
33+
// HTTPS the TLS layer provides this gap naturally; for plain HTTP we
34+
// need to create it artificially.
35+
const patchedSockets = new WeakSet();
36+
function patchSocket(socket) {
37+
if (patchedSockets.has(socket)) return;
38+
patchedSockets.add(socket);
39+
const delay = 5;
40+
const origWrite = socket.write;
41+
socket.write = function(chunk, encoding, cb) {
42+
if (typeof encoding === 'function') {
43+
cb = encoding;
44+
encoding = null;
45+
}
46+
if (typeof cb === 'function') {
47+
const orig = cb;
48+
cb = (...args) => setTimeout(() => orig(...args), delay);
49+
}
50+
return origWrite.call(this, chunk, encoding, cb);
51+
};
52+
}
53+
54+
const server = http.createServer(common.mustCall((req, res) => {
55+
req.on('error', common.mustNotCall());
56+
res.writeHead(200);
57+
res.end();
58+
}, REQUEST_COUNT));
59+
60+
server.listen(0, common.mustCall(() => {
61+
const { port } = server.address();
62+
63+
async function run() {
64+
try {
65+
for (let i = 0; i < REQUEST_COUNT; i++) {
66+
await sendRequest(port);
67+
}
68+
} finally {
69+
agent.destroy();
70+
server.close();
71+
}
72+
}
73+
74+
run().then(common.mustCall());
75+
}));
76+
77+
function sendRequest(port) {
78+
let timeout;
79+
const promise = new Promise((resolve, reject) => {
80+
function done(err) {
81+
clearTimeout(timeout);
82+
if (err)
83+
reject(err);
84+
else
85+
resolve();
86+
}
87+
88+
const req = http.request({
89+
port,
90+
host: '127.0.0.1',
91+
method: 'POST',
92+
agent,
93+
headers: {
94+
'Content-Length': '0',
95+
'Expect': '100-continue',
96+
},
97+
}, common.mustCall((res) => {
98+
assert.strictEqual(res.statusCode, 200);
99+
res.resume();
100+
res.once('end', done);
101+
res.once('error', done);
102+
}));
103+
104+
req.on('socket', patchSocket);
105+
106+
timeout = setTimeout(() => {
107+
const err = new Error('request timed out');
108+
req.destroy(err);
109+
done(err);
110+
}, common.platformTimeout(5000));
111+
112+
req.once('error', done);
113+
114+
setTimeout(() => req.end(Buffer.alloc(0)), 0);
115+
});
116+
return promise.finally(() => clearTimeout(timeout));
117+
}
Lines changed: 97 additions & 0 deletions

0 commit comments

Comments
 (0)