Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
Someone is attempting to deploy a commit to the trpc Team on Vercel. A member of the Team first needs to authorize it. |
commit: |
|
@tarekwiz is this meant to solve the problem of a client not being able to reconnect to a stream if it (the client) cancelled the stream (e.g reloading page)? An issue that resulting on this kind error on front end:
We applied your code changes as a patch and it didn't solve this issue. |
No this is not meant to fix that issue. This is meant to fix an issue on the server where the server would not call abort on the abortcontroller when the connection is dropped |
@paradoxloop I've not experienced this - could you please try the latest release and see? We made some changes to client's stream as part of #6214. If not, please create a new issue |
|
thanks @KATT just tried the latest release "633" and this does not make the issue go away. Will try to create a MVR early next week however this work is attempt to move away from websockets for a particular tool and there seems to be lots of footguns and the tooling/documentation around SSE is limited (more generally not specifically TRPC) so might have to pause it. |
|
|
Nope the abort controller is never called in the latest next release. This fix is needed. You can reproduce it by using Astro’s fetch implementation
|
Please give us better steps to reproduce it or supply a reproduction repo - we can't merge this as-is without tests or anything as we then can't guarantee we won't break it in the future |
|
In the last PR we made, we deleted the signal on the So there's no signal needed on the readable stream itself. We trigger the abort signal like this: trpc/packages/server/src/adapters/node-http/incomingMessageToRequest.ts Lines 139 to 140 in 09a7686 This should be sufficient. If it isn't in your environment, please add logs to all possible close-events akin to the above and let us know which trigger Docs in Node.js:
Again, we need easier steps to reproduce than "You can reproduce it by using Astro’s fetch implementation" - make a reproduction repro and tell us how to reproduce it in your repo 🙏 |
|
There you go https://github.com/tarekwiz/trpc-sse-bug |
|
Abort signal is not triggered, no, but the stream ends as expected: Abort signal should likely be triggered as well, but if you use generators everywhere it's not an issue |
|
Ok let's fix the abort controller not being aborted |
I'm honestly unsure how to do this nicely - the signal is there to handle aborts and it does that reliably now. We'd have to add a new abort controller inside of Your PR added this for one of our possibly We could do some sort of wrapper of response in function createResponse(
body: BodyInit | null,
init: ResponseInit,
onEndController: AbortController,
) {
const response = new Response(body, init);
// Somehow sniff when `response.body`'s stream has ended - unsure how?
return response;
}Unsure how to do it. Ideas? Also unsure what you need this for? |
|
So basically the current situation is that the function inside the subscription would only reach the "finally" or error state when the server tries to send a message but the connection is closed. We need the abort controller to be able to save up on server resources so that once the request is closed, we can cleanup the resources used by that user. Otherwise we would have to wait for the server to send information to the client. Maybe adding ping would solve that but I haven't tested it. |
|
By the way, when sse.ping is enabled, the finally block is never reached. This took me 2 hours of debugging to find @KATT |
|
Also, how does this look to you function createResponse(
body: BodyInit | null,
init: ResponseInit,
onEndController: AbortController,
) {
// check if body is a stream
if (body instanceof ReadableStream) {
// override the cancel function of the ReadableStream to cancel the abort controller.
body.cancel = async () => {
onEndController.abort();
};
}
return new Response(body, init);
}``` |
Feels wrong but maybe it could work... but the signal would still wouldn't be triggered if the body just ended "normally" |
|
What is a "normal" way of closing a readable stream? |
When the stream is done but wasn't actually cancelled - 43a2b37 |
|
Hmmm not sure tbh.. |
|
This could maybe work: |
|
Yeah that looks solid. |
|
There's more to it most likely, but feel to do a PR using this code, I don't know when I'll have more time for this |
|
Will close this PR now. The issues around Please open a feature request with your desire to have the abort controller to be invoked in the Fetch adapter. Or go straight to a PR. I personally don't see the need for this to exist, but I might be missing something. |
|
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. |
🎯 Changes
Added AbortController to the createReadableStream function, allowing it to abort once the request is cancelled.
This is to fix an issue with SSE where the server would never send an abort signal when the client closes the connection.
#6193
✅ Checklist