Skip to content

http.ClientRequest with Upgrade header sometimes hangs on first request #29701

@davidje13

Description

@davidje13
  • 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.listen call to server.listen(0, '127.0.0.1', resolve)
  • Change http.ClientRequest block 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.listen and 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.macosIssues and PRs related to the macOS platform / OSX.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions