WebSocket allows more than one CONNECTING connection to the same (host, port)
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 879
- Avg merge
- 2d 16h
- Merged PRs (30d)
- 68
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the RFC 6455 requirement and running the failing web-platform test at websockets/constructor/014.html. Trace Undici's WebSocket connection setup and verify that two connections to the same host and port are serialized. Done means the test and the supplied reproduction show a delay between the two onopen events rather than parallel handshakes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100