Skip to content

WebSocket: "close" is not emitted on error #3697

Closed
@kettanaito

Description

Bug Description

When a WebSocket connection is closed due to an error, the close event doesn't get emitted.

Reproducible By

Run this in Node.js REPL (assumes undici is installed):

const { WebSocket } = require('undici')

let ws = new WebSocket('wss://example.com/non-existing-url')
ws.onerror = () => console.log('ERROR!')
ws.onclose = () => console.log('CLOSE!')

Expected Behavior

The WebSocket instance must dispatch two events in the following order:

ERROR!
CLOSE!

Actual behavior

It doesn't. Only the error event gets dispatched. The close event never gets dispatched, which I believe to be incorrect.

ERROR!

Logs & Screenshots

I can also verify that the browser implementation of WebSocket behaves correctly:

Screenshot 2024-10-07 at 11 38 33

Environment

I believe the environment is irrelevant here.

  • undici 6.6.2

Additional context

The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent for example).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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