Skip to content

WebSocket adapter: a subscription's cleanup error after the client stops it never reaches onError #7887

Closed

Description

@KATT

🤖 When a client stops a WebSocket subscription (unsubscribe(), abort, or disconnect), the subscription's cleanup can throw: an observable teardown, or a generator's finally. That error is dropped. The server's onError isn't called, and nothing is logged.

const t = initTRPC.create();
const router = t.router({
  onTick: t.procedure.subscription(() =>
    observable<number>((emit) => {
      emit.next(1);
      return () => {
        throw new Error('teardown'); // a bug in cleanup code
      };
    }),
  ),
});
applyWSSHandler({ wss, router, onError: ({ error }) => console.error(error) });

// client, over wsLink
const sub = client.onTick.subscribe(undefined, { onData() {} });
sub.unsubscribe(); // the teardown throws on the server; onError is never called

Expected: onError is called with the cleanup error, as it is when the subscription completes on its own.

Actual: onError isn't called. This also happens with an async function* whose finally throws once the stop wakes it, and when the client disconnects.

Root cause: ws.ts:363-371 races the pending iterator.next() against the stop and breaks on 'abort'. The cleanup error rejects that next() after the race is decided, and nothing handles the rejection.

Activity

  1. added a commit that references this issue on Nov 4, 2026
    83391b5
  2. github-actions commented on Nov 5, 2026

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  3. locked as resolved and limited conversation to collaborators on Nov 5, 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions