Skip to content

WebSocket link silently ignores the operation AbortSignal (removeEventListener with no matching addEventListener) #7431

Closed

Description

@tsushanth

Description

An AbortSignal passed to a WebSocket operation (op.signal, exposed via the signal request option) is never wired to anything. Aborting the caller's signal does not cancel an in-flight request nor stop a subscription. This diverges from httpSubscriptionLink, which honors op.signal.

Root cause

packages/client/src/links/wsLink/wsClient/wsClient.ts: the request() teardown calls signal?.removeEventListener('abort', abort), but there is no matching signal.addEventListener('abort', ...) anywhere in the wsLink client directory (signal appears only at the destructure, the type, and the removeEventListener; no signal.aborted/throwIfAborted either). The removeEventListener removes a listener that was never added. The v11 refactor (PR #6223) plumbed signal into request() but only added the cleanup half.

Steps to reproduce

Subscribe over a WS link with an AbortController, then ac.abort(). Observed: no subscription.stop frame is sent and the subscription keeps running; an in-flight WS query/mutation is likewise not cancelled. Expected: abort tears the operation down (and sends subscription.stop for subscriptions).

Suggested fix

In request(), after obtaining abort, wire the signal: if signal?.aborted tear down immediately; else signal?.addEventListener('abort', <teardown>, { once: true }), where teardown calls abort(), sends subscription.stop for subscriptions, and completes the observer — mirroring the existing return-cleanup.

Activity

  1. cschanhniem commented on Jul 8, 2026

    @cschanhniem

    The WebSocket link ignoring the AbortSignal is an issue in the wsLink's subscription cleanup path. When the caller supplies an AbortSignal via the TRPCClient options or through the link context, the wsLink should register an abort event listener on that signal that triggers the unsubscribe flow. Currently, the wsLink only handles connection-level cleanup (on close/error) and per-call cleanup (when the observable is torn down), but the AbortSignal passed at call time is not wired into either path.

    The fix needs to be in the wsLink's request or subscription function where the observable is created. The AbortSignal's abort event should call the same teardown function that fires when the observer unsubscribes or the WebSocket connection drops. This mirrors how the httpBatchLink handles AbortSignal: it passes the signal to fetch() directly. For wsLink, there is no equivalent automatic propagation, so the signal must be handled manually.

    The affected code is in the wsLink implementation where it creates the observable for a subscription call. Adding opts.signal?.addEventListener('abort', teardown) before returning the observable and cleaning it up in the teardown itself would resolve the leak.

  2. added a commit that references this issue on Aug 3, 2026
    1e05965
  3. added a commit that references this issue on Sep 12, 2026
    30eea5e
  4. added a commit that references this issue on Sep 12, 2026
    ef684c8
  5. github-actions commented on Sep 13, 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.

  6. locked as resolved and limited conversation to collaborators on Sep 13, 2026
  7. added a commit that references this issue on Sep 18, 2026
    6d98da6
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