Skip to content

Commit 9db02dc

Browse files
addaleaxjasnell
authored andcommitted
async_hooks,http: fix socket reuse with Agent
Under very specific circumstances the `http` implementation could be brought to crash, because the Agent did not re-assign the async id field properly after setting up a socket for reuse. Fixes: #13325 PR-URL: #13348 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Andreas Madsen <[email protected]>
1 parent 837ecc0 commit 9db02dc

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

lib/_http_agent.js

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
168168
var socket = this.freeSockets[name].shift();
169169
// Assign the handle a new asyncId and run any init() hooks.
170170
socket._handle.asyncReset();
171+
socket[async_id_symbol] = socket._handle.getAsyncId();
171172
debug('have free socket');
172173

173174
// don't leak
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
5+
const http = require('http');
6+
7+
// Regression test for https://github.com/nodejs/node/issues/13325
8+
// Checks that an http.Agent properly asyncReset()s a reused socket handle, and
9+
// re-assigns the fresh async id to the reused `net.Socket` instance.
10+
11+
// Make sure a single socket is transpartently reused for 2 requests.
12+
const agent = new http.Agent({
13+
keepAlive: true,
14+
keepAliveMsecs: Infinity,
15+
maxSockets: 1
16+
});
17+
18+
const server = http.createServer(common.mustCall((req, res) => {
19+
req.once('data', common.mustCallAtLeast(() => {
20+
res.writeHead(200, {'Content-Type': 'text/plain'});
21+
res.write('foo');
22+
}));
23+
req.on('end', common.mustCall(() => {
24+
res.end('bar');
25+
}));
26+
}, 2)).listen(0, common.mustCall(() => {
27+
const port = server.address().port;
28+
const payload = 'hello world';
29+
30+
// First request. This is useless except for adding a socket to the
31+
// agent’s pool for reuse.
32+
const r1 = http.request({
33+
agent, port, method: 'POST'
34+
}, common.mustCall((res) => {
35+
// Remember which socket we used.
36+
const socket = res.socket;
37+
const asyncIdAtFirstRequest = socket[async_id_symbol];
38+
assert.ok(asyncIdAtFirstRequest > 0, `${asyncIdAtFirstRequest} > 0`);
39+
// Check that request and response share their socket.
40+
assert.strictEqual(r1.socket, socket);
41+
42+
res.on('data', common.mustCallAtLeast(() => {}));
43+
res.on('end', common.mustCall(() => {
44+
// setImmediate() to give the agent time to register the freed socket.
45+
setImmediate(common.mustCall(() => {
46+
// The socket is free for reuse now.
47+
assert.strictEqual(socket[async_id_symbol], -1);
48+
49+
// Second request. To re-create the exact conditions from the
50+
// referenced issue, we use a POST request without chunked encoding
51+
// (hence the Content-Length header) and call .end() after the
52+
// response header has already been received.
53+
const r2 = http.request({
54+
agent, port, method: 'POST', headers: {
55+
'Content-Length': payload.length
56+
}
57+
}, common.mustCall((res) => {
58+
const asyncId = res.socket[async_id_symbol];
59+
assert.ok(asyncId > 0, `${asyncId} > 0`);
60+
assert.strictEqual(r2.socket, socket);
61+
// Empty payload, to hit the “right” code path.
62+
r2.end('');
63+
64+
res.on('data', common.mustCallAtLeast(() => {}));
65+
res.on('end', common.mustCall(() => {
66+
// Clean up to let the event loop stop.
67+
server.close();
68+
agent.destroy();
69+
}));
70+
}));
71+
72+
// Schedule a payload to be written immediately, but do not end the
73+
// request just yet.
74+
r2.write(payload);
75+
}));
76+
}));
77+
}));
78+
r1.end(payload);
79+
}));

0 commit comments

Comments
 (0)