-
-
Notifications
You must be signed in to change notification settings - Fork 35.1k
Description
- Version: 12.10.0 (also checked on 12.9.1)
- Platform: macOS 10.14.6 — Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64
- Subsystem: http
Connecting to a newly started http.Server with a http.ClientRequest using Connection: Upgrade occasionally (~0.2% probability) hangs indefinitely. The request never reaches the server (no connection event is fired), but strangely the same issue does not appear when using a raw net.Socket on the client and sending equivalent data. The issue also does not appear if the server is bound to 127.0.0.1 rather than the default 0.0.0.0.
The following code replicates the issue reliably; typically it will freeze after ~500 iterations:
const http = require('http');
async function test() {
const server = http.createServer();
server.on('connection', () => {
process.stdout.write(`${Date.now()} | - connection\n`);
});
server.on('upgrade', (req, socket) => {
socket.end('HTTP/1.1 404 Not Found\r\n\r\n');
});
await new Promise((resolve) => server.listen(0, resolve));
const { port } = server.address();
await new Promise((resolve) => {
const req = new http.ClientRequest({
port,
headers: {
Connection: 'Upgrade',
Upgrade: 'x'
}
});
req.once('close', resolve);
req.end();
});
await new Promise((resolve) => server.close(resolve));
}
(async () => {
for (let i = 0; ; i++) {
process.stdout.write(`${Date.now()} | Attempt ${i}\n`);
await test();
}
})();Changes which make the issue disappear:
- Change
server.listencall toserver.listen(0, '127.0.0.1', resolve) - Change
http.ClientRequestblock to:await new Promise((resolve) => { const s = new net.Socket(); s.on('close', resolve); s.on('data', () => {}); s.connect(port, 'localhost', () => { s.write([ 'GET / HTTP/1.1', 'Connection: Upgrade', 'Upgrade: websocket', '', '', ].join('\r\n')); s.end(); }); });
- Remove headers, and change related server event (
server.on('request', (req, res) => res.end());)
Changes which do not make the issue disappear:
- adding a delay between
server.listenand the request (tested with up to 1 second)
When the issue appears, even the server-side connection event does not fire.
This was found while debugging a suite of tests with occasional failures. Initially reported to websockets/ws#1635