Repository navigation
Closed
Labels
Description
Provide environment information
System:
OS: Windows 11 10.0.22631
CPU: (32) x64 AMD Ryzen 9 7950X 16-Core Processor
Memory: 46.84 GB / 63.65 GB
Binaries:
Node: 20.13.1 - C:\P\apps\nodejs\node.EXE
Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 10.5.2 - C:\P\apps\nodejs\npm.CMD
Browsers:
Edge: Chromium (126.0.2592.81)
Internet Explorer: 11.0.22621.3527
npmPackages:
@trpc/client: next => 11.0.0-rc.436+fbb4a2fb4
@trpc/server: next => 11.0.0-rc.436+fbb4a2fb4
typescript: ^5.5.0 => 5.5.3
Describe the bug
The AbortController signal seems ignored by Stream Links with async generators.
We'd expect an interruption on the server side, and on the client side, but packets continue to be produced by the async generator, flow to the client, and the client keeps awaiting and receiving them.
I don't know what the correct behavior would be, but right now the signal seems to be completely ignored on both ends.
Link to reproduction
To reproduce
In summary:
- The client creates a Stream Link, and queries with a
{signal: ac.signal}, like this (minimal):
const trpc = createTRPCClient<AppRouter>({
links: [unstable_httpBatchStreamLink({url: 'http://localhost:3000',})],
});
const ac = new AbortController();
const iterable = await trpc.examples.iterable.query(undefined, { signal: ac.signal });- The server implements a procedure with an async generator, like this (minimal):
iterable: publicProcedure.query(async function* () {
for (let i = 0; i < 8; i++) {
await new Promise((resolve) => setTimeout(resolve, 500));
yield i;
}
}),- We would expect both the server and the client to be aborted, but execution continues on both.
This is the output:

Additional information
No response
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR fixing this bug!
Activity
KATT commentedon Jul 2, 2024
I think if you stop consuming the result and break in your
for awaitit'll do what you expectenricoros commentedon Jul 3, 2024
Tried this, and it stops the client, but the server keeps producing (yielding).
For a client only solution I would not need the AbortController, but the assumption is that by passing the signal there will be some effect, but right now I don't see any effect on it.
Pushed a branch variant-client-quit, with this commit, but the output shows that the server keeps producing as if abort was never called.
KATT commentedon Jul 3, 2024
Interesting. Feels like we should address this indeed.
The code is a bit involved - this link handles streaming and batching requests so one abort shouldn't necessarily abort everything unless all requests are aborted.
It'll likely take me some hours to debug this and think of a good solution, client side cancellation slipped my mind when doing the implementation.
If you need this solved, consider adding a bounty.
enricoros commentedon Jul 3, 2024
Thanks, I understand it's not easy - this is very advanced. For my usage I don't need the batching part, I don't know if there could be a simplification where abort() terminated all the connections of the batch.
The other issue is that after throwing on the server side, the connection seems to stay open (while it closes without throwing) -- maybe it's related to batching too.
I've added a bounty using polar. I understand it's not much, but it's basically my GitHub income for the month (and beats the issues with no bounty! :).
KATT commentedon Jul 5, 2024
The PR has been released as a tmp release, you can use it straight away using the
@tmp-tag of tRPCUpdate your
package.jsonto use the tmp version of tRPC:Then run
npm installor similar. Let me now how it works. It's not a great hack.KATT commentedon Jul 5, 2024
Both
returning in thefor await, andshould work, please try verifying both with a real usecase 🙏
enricoros commentedon Jul 5, 2024
Thanks! What a code change - Impressive. Testing in progress... ⌛
KATT commentedon Jul 5, 2024
I hated how I did it so I ended up spending 2-3 hours refactoring after 😬
enricoros commentedon Jul 6, 2024
This is working incredibly well. I am VERY glad I am basing this AI stack on tRPC, as this is working wonders already.
(update: updated pledge too - it's a lot of great work!)
Evaluation below - in short, this is great and looks like magic to me - only 1 question below.
Abort on the Client-side (rRPC API call / for await ...):
for await ....Abort on the Server-side (tRPC procedure):
Throwing from the server-side raises a TRPCClientError on the client side:
Misc. notes:
error.name = 'AbortError', as per this MDN notenameof the error thrown as'AbortError', so that it's compatible with existing catching codeThanks again for this great work; would love to hear on the server-side question (are there events/signals to catch or pass on to the upstream OpenAI connection).
KATT commentedon Jul 6, 2024
If you pass the
reqfromcreateContextdown you should be able to do this:We could potentially add a signal in
opts.ctx.info.signalor something too, wdyt?Edit: did that in #5872 as it was just a few lines
enricoros commentedon Jul 6, 2024
I'm watching the branch and the commits pouring in, while at the same time I tried to debug it myself (got stuck inside some 'undici' - this is way too unknown for me). I am eager to try this out - looks amazing.
enricoros commentedon Jul 6, 2024
Update: Testing ⌛complete.
Works perfectly. I confirm that the signal passed from the context

request.signalworks really well.As an example, I started 4 OpenAI Generations and interrupted them right at the beginning, and the servers show the following stats:

This shows that the signal is really used from the request, passed to the fetch which terminates the upstream connection, and the user saves tokens - input (context) tokens >> output (generated) tokens.
As of now, the client and server work great. I don't see any other improvement (I didn't check if
error.name = 'AbortError', but it can be worked around too).Thanks, and glad to honor the pledge.
KATT commentedon Jul 10, 2024
Merged this now and we appreciate the pledge - if you want to do a real dent in longevity of tRPC, please consider to sponsor [or get your employer to sponsor] on a monthly basis 🙏
enricoros commentedon Jul 11, 2024
@KATT thanks, this is huge news! When my startup gets funding I'll pay it forward and sponsor the project.
So far, I'm incredibly happy with Streaming via tRPC, especially because it induces correctness upstream via generators/async generators. The new API is so clean, and have infinite room for scalability, which I'm still early in exploring. Cannot wait to remove all my old stream transformation code and manual endpoints. Streaming is such a great tRPC use case because of the flexibility of return types; it's not one .output(), but many object types generated on the fly with no cost.
I wish I had the time to make my tRPC AI library a bit more independent from the rest of the App, because it's a more flexible alternative to the Vercel AI sdk (multi-modal, multi-part, dynamic upstream streaming, 14 vendors, etc.)
KATT commentedon Jul 11, 2024
Thank you @enricoros, this whole interaction with you has been 10/10
github-actions commentedon Jul 13, 2024
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.