Skip to content

wsClient: reconnect errors are swallowed, and a throwing onClose or onError stops reconnects #7890

Closed

Description

@KATT

🤖 Two related createWSClient bugs on reconnect.

1. A connectionParams error on reconnect is never surfaced

let token: string | null = 'ok';
const wsClient = createWSClient({
  url,
  connectionParams: () => {
    if (!token) throw new Error('token refresh failed');
    return { token };
  },
});
const client = createTRPCClient<AppRouter>({ links: [wsLink({ client: wsClient })] });
client.onTick.subscribe(undefined, {
  onConnectionStateChange: (s) => console.log(s.state, s.error?.cause),
});
// later: token = null, and the server drops the connection

Expected: as on the first connect, the connection state goes to connecting with an Initialization error whose cause is the thrown error, and the client keeps retrying with backoff.

Actual: the error is dropped. The state flips between pending and connecting / "WebSocket closed" forever, and nothing ever shows token refresh failed.

Root cause: tryReconnect in wsClient.ts:302 wraps each attempt in a bare catch {} and retries. The first-connect path in open() (wsClient.ts:130-137) reports the same failure on connectionState.

2. A throwing onClose (or onError) stops reconnects

const wsClient = createWSClient({
  url,
  onClose: () => {
    throw new Error('bug in my onClose');
  },
});
client.onTick.subscribe(undefined, { onData: console.log });
// the server drops the connection

Expected: the subscription reconnects and resumes. The callback's error is still reported, for example as an uncaught exception.

Actual: the subscription never reconnects. With a throwing onError, the client hangs on the first failed connect, and even wsClient.close() never settles.

Root cause: the close and error listeners (wsClient.ts:373-397) call the user callback before this.reconnect(...), so a throw skips it. With ws (an EventEmitter), the throw also skips the other listeners for that event. One of these is asyncWsOpen's error listener, so the open promise never settles.

Activity

  1. added a commit that references this issue on Nov 4, 2026
    f11900e
  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