Skip to content

Commit 2ea056a

Browse files
committed
http: adds a regression test for issue #60001 against an HTTPS server
1 parent 71b36ee commit 2ea056a

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
// HTTPS widens this window because the TLS layer introduces async
11+
// indirection between the actual write completion and the JS callback.
12+
//
13+
// With Expect: 100-continue, the server responds quickly while the client
14+
// delays req.end() just slightly (setTimeout 0), creating the perfect
15+
// timing for the response to arrive in that window.
16+
//
17+
// On unpatched Node, the double responseKeepAlive() call corrupts the
18+
// socket by stripping a subsequent request's listeners and emitting a
19+
// spurious 'free' event, causing requests to hang / time out.
20+
21+
const common = require('../common');
22+
23+
if (!common.hasCrypto)
24+
common.skip('missing crypto');
25+
26+
const assert = require('assert');
27+
const https = require('https');
28+
const fixtures = require('../common/fixtures');
29+
30+
const REQUEST_COUNT = 100;
31+
const agent = new https.Agent({ keepAlive: true, maxSockets: 1 });
32+
33+
const key = fixtures.readKey('agent1-key.pem');
34+
const cert = fixtures.readKey('agent1-cert.pem');
35+
const server = https.createServer({ key, cert }, common.mustCall((req, res) => {
36+
req.on('error', common.mustNotCall());
37+
res.writeHead(200);
38+
res.end();
39+
}, REQUEST_COUNT));
40+
41+
server.listen(0, common.mustCall(() => {
42+
const { port } = server.address();
43+
44+
async function run() {
45+
try {
46+
for (let i = 0; i < REQUEST_COUNT; i++) {
47+
await sendRequest(port);
48+
}
49+
} finally {
50+
agent.destroy();
51+
server.close();
52+
}
53+
}
54+
55+
run().then(common.mustCall());
56+
}));
57+
58+
function sendRequest(port) {
59+
let timeout;
60+
const promise = new Promise((resolve, reject) => {
61+
function done(err) {
62+
clearTimeout(timeout);
63+
if (err)
64+
reject(err);
65+
else
66+
resolve();
67+
}
68+
69+
const req = https.request({
70+
port,
71+
host: '127.0.0.1',
72+
rejectUnauthorized: false,
73+
method: 'POST',
74+
agent,
75+
headers: {
76+
'Content-Length': '0',
77+
Expect: '100-continue',
78+
},
79+
}, common.mustCall((res) => {
80+
assert.strictEqual(res.statusCode, 200);
81+
res.resume();
82+
res.once('end', done);
83+
res.once('error', done);
84+
}));
85+
86+
timeout = setTimeout(() => {
87+
const err = new Error('request timed out');
88+
req.destroy(err);
89+
done(err);
90+
}, common.platformTimeout(5000));
91+
92+
req.once('error', done);
93+
94+
setTimeout(() => req.end(Buffer.alloc(0)), 0);
95+
});
96+
return promise.finally(() => clearTimeout(timeout));
97+
}

0 commit comments

Comments
 (0)