Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket - Opening a connection to a non-existent server fire a error but keeps CONNECTING state indefinitely #3506

Closed
alexojegu opened this issue Aug 25, 2024 · 0 comments · Fixed by #3507
Labels
bug Something isn't working

Comments

@alexojegu
Copy link

alexojegu commented Aug 25, 2024

Bug Description

WebSocket - Opening a connection to a non-existent server or a stopped server generates an error but maintains the CONNECTING state indefinitely so it is not possible to close the connection or reopen it if you are cheking the state is not CONNECTING or OPEN.

Reproducible By

import { ErrorEvent } from "undici-types";

let webSocket: WebSocket;

function openConnection() {
    if (webSocket && webSocket.readyState <= WebSocket.OPEN) {
        console.log("WebSocket state: " + webSocket.readyState);
        return;
    }

    webSocket = new WebSocket("ws://localhost:1");
    console.log("WebSocket created");

    webSocket.addEventListener("error", (event) => handleError(event));
}

function handleError({ message }: ErrorEvent) {
    console.log(message);

    if (message === "Connection was closed before it was established.") {
        openConnection();
        return;
    }

    webSocket.close();
}

openConnection();

Expected Behavior

Fire a error and change state to CLOSED.

Logs & Screenshots

WebSocket created
Received network error or non-101 status code.
Connection was closed before it was established.
WebSocket state: 0

Environment

$ uname -a
Linux desktop 6.10.3-arch1-2 #1 SMP PREEMPT_DYNAMIC Tue, 06 Aug 2024 07:21:19 +0000 x86_64 GNU/Linux
$ node --version
v20.17.0
@alexojegu alexojegu added the bug Something isn't working label Aug 25, 2024
KhafraDev added a commit to KhafraDev/undici that referenced this issue Aug 25, 2024
Fixes nodejs#3506

set websocket readyState on fail

Fixes nodejs#3506
KhafraDev added a commit that referenced this issue Aug 25, 2024
Fixes #3506

set websocket readyState on fail

Fixes #3506
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant