Skip to content

WebSocket allows more than one CONNECTING connection to the same (host, port) #4743

@domenic

Description

@domenic

Bug Description

RFC 6455 requires that only one WebSocket connection to a given (host, port) pair be in the CONNECTING state. Multiple connections must be queued:

https://datatracker.ietf.org/doc/html/rfc6455#section-4.1


   2.  If the client already has a WebSocket connection to the remote
       host (IP address) identified by /host/ and port /port/ pair, even
       if the remote host is known by another name, the client MUST wait
       until that connection has been established or for that connection
       to have failed.  There MUST be no more than one connection in a
       CONNECTING state.  If multiple connections to the same IP address
       are attempted simultaneously, the client MUST serialize them so
       that there is no more than one connection at a time running
       through the following steps.

Undici does not follow this behavior.

Reproducible By

Failing web platform test: https://github.com/web-platform-tests/wpt/blob/master/websockets/constructor/014.html

Minimal Node.js repro:

const { WebSocket } = require("undici");
const { createServer } = require("http");

const server = createServer();

server.on("upgrade", async (req, socket) => {
  // Sleep 2 seconds during handshake
  await new Promise(r => setTimeout(r, 2000));

  const key = req.headers["sec-websocket-key"];
  const hash = require("crypto")
    .createHash("sha1")
    .update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
    .digest("base64");

  socket.write(
    "HTTP/1.1 101 Switching Protocols\r\n" +
    "Upgrade: websocket\r\n" +
    "Connection: Upgrade\r\n" +
    `Sec-WebSocket-Accept: ${hash}\r\n\r\n`
  );
  socket.end();
});

server.listen(0, () => {
  const port = server.address().port;
  const times = [];

  for (let i = 0; i < 2; i++) {
    const ws = new WebSocket(`ws://localhost:${port}/`);
    ws.onopen = () => {
      times.push(Date.now());
      ws.close();
      if (times.length === 2) {
        const diff = times[1] - times[0];
        console.log(`Time between onopen events: ${diff}ms`);
        console.log(diff > 1000 ? "PASS: Connections were serialized" : "FAIL: Connections were parallel");
        server.close();
      }
    };
  }
});

Expected Behavior

Time between onopen events: (some larger number)
PASS: Connections were serialized

Logs & Screenshots

Time between onopen events: 1ms
FAIL: Connections were parallel

Environment

  • Node v25.2.1
  • undici 7.18.2

Additional context

I might be able to emulate this with some custom dispatcher? But I'm hoping my users can already pass in a custom dispatcher, and dispatchers are hard to compose...

The ws npm package gets this correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions