Skip to content

WebSocket events fire synchronously during close() instead of asynchronously #4741

Open

Description

@domenic

Bug Description

When close() is called on a WebSocket in the CONNECTING state, the error and close events fire synchronously during the close() call, rather than being queued to fire asynchronously after close() returns.

Reproducible By

const { WebSocket } = require('undici');

const ws = new WebSocket('wss://echo.websocket.events/');

let closeReturned = false;

ws.addEventListener('error', () => {
  console.log('error event fired, closeReturned =', closeReturned);
});

ws.addEventListener('close', () => {
  console.log('close event fired, closeReturned =', closeReturned);
  process.exit(0);
});

console.log('Calling close()...');
ws.close();
closeReturned = true;
console.log('close() returned, closeReturned =', closeReturned);

setTimeout(() => {
  console.log('Timeout - no events fired');
  process.exit(1);
}, 5000);

Expected Behavior

Calling close()...
close() returned, closeReturned = true
error event fired, closeReturned = true
close event fired, closeReturned = true

See: https://jsbin.com/butegexadi/edit?html,js,output

This follows from the "queue a task" steps in in the spec: https://websockets.spec.whatwg.org/#:~:text=cleanly%2C-,the%20user%20agent%20must%20queue%20a%20task

Relevant WPTs:

Logs & Screenshots

Actual behavior:

Calling close()...
error event fired, closeReturned = false
close event fired, closeReturned = false

Environment

  • Node v25.2.1
  • undici 7.18.2

Activity

  1. added a commit that references this issue on Jan 17, 2026
    4c1ec04
  2. KhafraDev commented on Apr 24, 2026

    @KhafraDev
    Member

    I am actually going to go insane if 1 more person thinks that their coding agent of choice can correctly fix this in one-shot with no understanding of the issue.

  3. mcollina commented on Apr 24, 2026

    @mcollina
    SponsorMember

    @KhafraDev I do have a broad understanding of the issue and I do not understand how you want it fixed / we do not agree on the fix. It would be best if you took care of iz

  4. KhafraDev commented on Apr 24, 2026

    @KhafraDev
    Member

    I wasn't referring to you :) There have been 3 PRs that are completely AI generated that are all wrong for the same reason. It's honestly getting really annoying getting notifications for this issue when people (ie. #5078) cannot take the time to review the changes themselves or even respond without using ai.

  5. added
    websocketPull requests or issues related to websocket and its standard
    on May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwebsocketPull requests or issues related to websocket and its standard

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions