WebSockets do not fire 'close' event if the connection failed to be established #3546
Description
opened on Sep 4, 2024
Consider the following code:
const webSocket = new undici.WebSocket("wss://invalid-domain.example.com/");
webSocket.onopen = () => { console.log("open"); };
webSocket.onclose = () => { console.log("close"); };
webSocket.onerror = () => { console.log("error"); };
It outputs:
error
However, a standard-compliant WebSocket implementation would output:
error
close
This is because establish a WebSocket connection algorithm would invoke fail the WebSocket Connection algorithm in step 11.1, which would Close the WebSocket Connection. The WebSocket standard states:
When the WebSocket connection is closed, possibly cleanly, the user agent must queue a task to run the following substeps:
...
3. Fire an event named close at the WebSocket object.
This bug is reproducible on commit 4b8958a.
Activity