Skip to content

fix(client): wire the operation AbortSignal in the WebSocket link - #7434

Merged
Nick-Lucas merged 3 commits into
trpc:mainfrom
ATKasem:fix/wslink-abort-signal
Sep 12, 2026
Merged

Nick-Lucas merged 3 commits into
trpc:mainfrom
ATKasem:fix/wslink-abort-signal

Conversation

@ATKasem

@ATKasem ATKasem commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #7431

🎯 Changes

The WebSocket link's request observable never listened to the operation's AbortSignal. Its teardown called signal?.removeEventListener('abort', abort), but nothing ever registered that listener β€” abort was the function returned by batchSend, not a listener. Aborting an operation's signal did nothing: the request was never torn down, the subscription kept running, and its server-side listener leaked.

Register an abort listener that completes the observer. Completing runs the existing teardown, which sends subscription.stop for subscriptions, so an aborted operation now ends cleanly. The matching listener is removed on teardown, and an already-aborted signal is handled synchronously.

βœ… Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

Summary by CodeRabbit

  • Bug Fixes

    • Improved WebSocket request handling when an abort signal is triggered, including signals that were already aborted.
    • Ensured active subscriptions stop promptly and clean up their event listeners after cancellation, preventing further message delivery.
  • Tests

    • Added coverage confirming that aborting a WebSocket message subscription removes its server-side listener and stops subsequent processing.

@ATKasem
ATKasem requested a review from a team as a code owner July 14, 2026 00:42
@vercel

vercel Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
next-prisma-starter Ready Ready Preview Sep 12, 2026 11:53am UTC
og-image Ready Ready Preview Sep 12, 2026 11:53am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

  • Run on-demand review

On-demand reviews are free for the next 8 days. After that, they cost $0.25 per reviewed file.

Or wait 45 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

βš™οΈ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: fc5beaeb-c2a1-44a2-b1e1-09bf5538a125

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between e6f3903 and 37d8480.

πŸ“’ Files selected for processing (1)
  • packages/client/src/links/wsLink/wsClient/wsClient.ts
πŸ“ Walkthrough

Walkthrough

WebSocket requests now register an AbortSignal handler. The handler completes the observable and performs existing teardown for already-aborted and future-aborted signals. A test verifies server-side listener removal after aborting.

Changes

WebSocket abort handling

Layer / File(s) Summary
Abort wiring and subscription cleanup
packages/client/src/links/wsLink/wsClient/wsClient.ts, packages/tests/server/websockets.test.ts
WsClient.request handles abort events, removes the matching listener during teardown, preserves subscription stopping, and tests server-side listener cleanup.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AbortController
  participant WsClient
  participant ServerSubscription
  AbortController->>WsClient: abort signal
  WsClient->>WsClient: complete observer and run teardown
  WsClient->>ServerSubscription: stop subscription
  ServerSubscription-->>WsClient: remove server:msg listener
Loading

Possibly related issues

  • trpc/trpc#7439: Covers the missing AbortSignal listener that this change adds to WsClient.request.

Possibly related PRs

  • trpc/trpc#7440: Directly relates to WsClient.request abort handling.
  • trpc/trpc#7442: Modifies the same abort handling and WebSocket tests.
  • trpc/trpc#7444: Modifies WsClient.request listener registration and teardown.

Suggested reviewers: katt

πŸš₯ Pre-merge checks | βœ… 5
βœ… Passed checks (5 passed)
Check name Status Explanation
Title check βœ… Passed The title clearly identifies the WebSocket link fix for operation AbortSignal handling.
Description check βœ… Passed The description explains the bug, implementation, cleanup behavior, and related tests, and it includes the required checklist.
Linked Issues check βœ… Passed The changes implement issue #7431 by registering abort handling, supporting already-aborted signals, completing observers, and removing listeners during teardown.
Out of Scope Changes check βœ… Passed The code and test changes remain focused on wiring AbortSignal cancellation into the WebSocket link.
Docstring Coverage βœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/@trpc/client@7434

@trpc/next

npm i https://pkg.pr.new/@trpc/next@7434

@trpc/openapi

npm i https://pkg.pr.new/@trpc/openapi@7434

@trpc/react-query

npm i https://pkg.pr.new/@trpc/react-query@7434

@trpc/server

npm i https://pkg.pr.new/@trpc/server@7434

@trpc/tanstack-react-query

npm i https://pkg.pr.new/@trpc/tanstack-react-query@7434

@trpc/upgrade

npm i https://pkg.pr.new/@trpc/upgrade@7434

commit: 37d8480

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/tests/server/websockets.test.ts (1)

297-304: πŸ“ Maintainability & Code Quality | πŸ”΅ Trivial | πŸ’€ Low value

Type the mock explicitly and simplify the callback.

As per coding guidelines: "Use proper TypeScript typing for mocks". Provide the explicit signature to vi.fn() to improve type safety. You can also pass the mock directly to the callback to simplify the code.

♻️ Proposed refactor
-  const onStartedMock = vi.fn();
+  const onStartedMock = vi.fn<() => void>();

   ctx.client.onMessageObservable.subscribe(undefined, {
     signal: ac.signal,
-    onStarted() {
-      onStartedMock();
-    },
+    onStarted: onStartedMock,
   });
πŸ€– Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/tests/server/websockets.test.ts` around lines 297 - 304, Update
onStartedMock in the websocket subscription test to use an explicit vi.fn
signature matching the onStarted callback, then pass the mock directly as the
callback instead of wrapping it in an onStarted function.

Source: Coding guidelines

πŸ€– Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/tests/server/websockets.test.ts`:
- Around line 297-304: Update onStartedMock in the websocket subscription test
to use an explicit vi.fn signature matching the onStarted callback, then pass
the mock directly as the callback instead of wrapping it in an onStarted
function.

ℹ️ Review info
βš™οΈ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 1d974282-9d3f-4d98-b768-5d734ed67a06

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 340811b and 3582b54.

πŸ“’ Files selected for processing (2)
  • packages/client/src/links/wsLink/wsClient/wsClient.ts
  • packages/tests/server/websockets.test.ts

@ATKasem

ATKasem commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Just a heads-up in case it's helpful: the failing unit and integration tests check seems to be coming from @trpc/upgrade's transforms.test.ts (the optimistic-update hooks case), which looks unrelated to this PR's WebSocket change. I noticed it's also red on some other open PRs, so I think it may be failing on main rather than something here β€” but please let me know if I'm missing something and I'm happy to dig in further.

@ATKasem
ATKasem force-pushed the fix/wslink-abort-signal branch from e9e7fbf to e6f3903 Compare August 3, 2026 21:16
ATKasem and others added 3 commits September 12, 2026 12:51
The wsLink request observable removed an `abort` listener that was never
added, so aborting an operation's signal never tore the request down: the
subscription kept running and its server-side listener leaked. Add an
`abort` listener that completes the observer (running the existing teardown,
which sends `subscription.stop`), and remove the matching listener on cleanup.

Fixes trpc#7431
Use an explicit vi.fn signature and pass onStarted directly in the
abort-signal subscription test.
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 13, 2026

This branch had an error being deployed

1 failed and 4 active deployments
Preview – www β€” 37d8480e Deployed Sep 12, 2026 by vercel[bot]
Preview – trpc-next-app-dir β€” 37d8480e Deployed Sep 12, 2026 by vercel[bot]
Preview – next-prisma-starter β€” 37d8480e Deployed Sep 12, 2026 by vercel[bot]
Preview – examples-todomvc β€” 37d8480e Deployed Sep 12, 2026 by vercel[bot]
Preview – og-image β€” 37d8480e Deployed Sep 12, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants