You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
export const appRouter = router({
loopBased: procedure.subscription(async function* ({ signal }) {
try {
while (!signal?.aborted) {
console.log("loop", signal, signal?.aborted);
yield "new data";
await new Promise((r) => setTimeout(r, 1000));
}
} catch (error) {
console.error("done", error);
}
}),
});
As far as I can tell, the signal should get aborted when the connection breaks. Instead, if I close the tab or refresh the page, I see next.js reporting that the SSE request finished, but trpc keeps running this loop. The console log confirms that abort is false.
We're experiencing related issues. Resources are never getting cleaned up because our return method on the AsyncIterable is not being called after connection closes. This is causing a memory leak and our servers to OOM every couple of hours.
FYI, not sure why, but I can't seem to verify the fix on StackBlitz, but when running the project locally it works well, seems like the request's events aren't properly propagated in that environment somehow
@KATT yeah I was just testing it on stackblitz and the issue was still there. I'm seeing if manually purging all packages and running the install command again with 621 fixes it...
@KATT I can confirm that the issue is fixed in my project. I'd still prefer some further investigation since stackblitz still shows the bug, and I think as long as it's reproducible the issue should stay open
on the other hand, while the bug happened locally, I could still see next.js terminate the request once the page closed. stackblitz doesn't show that log line. so maybe it is an issue of their internal proxy not terminating the requests...
Is that a change from the latest release? Because the latest release didn't seem to be working for me on fly.io I had to make the change with the three different handlers above.
I ended up moving to my own SSE server and client implementation so I won't be able to repro any further changes to this in my own infrastructure. I recommend testing on fly.io or another proxied provider before closing this.
I ended up moving to my own SSE server and client implementation so I won't be able to repro any further changes to this in my own infrastructure. I recommend testing on fly.io or another proxied provider before closing this.
How come? Was there anything you were missing in ours? Happy to hop on a call to understand this better
Spent a lot of time on SSE this week and got some more improvements coming, sad to see you go 😅
I am also encountering an issue that might be related to this
(node:14844) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [Socket]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
at genericNodeError (node:internal/errors:983:15)
at wrappedFn (node:internal/errors:537:14)
at _addListener (node:events:592:17)
at Socket.addListener (node:events:610:10)
at Readable.on (node:internal/streams/readable:1128:35)
at Socket.socketListenerWrap [as on] (node:_http_server:1201:54)
at Socket.once (node:events:654:8)
at incomingMessageToRequest (file:///node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/@trpc/server/dist/adapters/node-http/incomingMessageToRequest.mjs:92:17)
at fastifyRequestHandler (file:///node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/@trpc/server/dist/adapters/fastify/fastifyRequestHandler.mjs:27:17)
at Object.handler (file:///packages/backend/dist/app/index.js:83:20)
All I have to do is just call a procedure 10 times, and this error will be visible on the console
#6204 fixed the memory leak but when we enabled pings, it returned (possibly via another mechanism).
We enabled pings because we are seeing extremely high numbers of ELB 502s (orders of magnitude above Target 5XXs) after we upgraded to TRPC11 with SSE, and we thought it's because the connection is idle for too long. Curiously, it also didn't fix the ELB 502s rate.
This really should be sufficient to do detection of canceled requests.
I don't think #6228 changes anything - what it aims to fix should have been a symptom of us not triggering the abort signal correctly, but the stream itself should be canceled as long as you listen to the abort signals as defined in the docs.
We've also added support on the client to do automatic reconnection on the client as EventSource itself isn't really great at detecting stale connections
(kill -9 of a server is enough to enough for Chrome's EventSource to hang in my testing)
We might add some defaults for ping/pong and timeouts
Feedback is very welcome in the 🎏-rfc-streaming channel on Discord.
experimental.sseSubscriptions: {} has moved to sse: {} (release note)
Apologies to anyone who have encountered this issue. It should be addressed in latest release (11.0.0-rc.633), but I will keep this issue open until someone has confirmed this did it for them.
This really should be sufficient to do detection of canceled requests.
I don't think #6228 changes anything - what it aims to fix should have been a symptom of us not triggering the abort signal correctly, but the stream itself should be canceled as long as you listen to the abort signals as defined in the docs.
* We've also added support on the client to do [automatic reconnection](https://trpc.io/docs/client/links/httpSubscriptionLink#timeout) on the client as `EventSource` itself isn't really great at detecting stale connections
* (`kill -9` of a server is enough to enough for Chrome's `EventSource` to hang in my testing)
* We might add some defaults for ping/pong and timeouts
* Feedback is very welcome in the [🎏-rfc-streaming](https://trpc.io/discord) channel on Discord.
* `experimental.sseSubscriptions: {}` has moved to `sse: {}` ([release note](https://trpc.io/docs/migrate-from-v10-to-v11#moves-experimentalssesubscriptions---sse-non-breaking))
Apologies to anyone who have encountered this issue. It should be addressed in latest release (11.0.0-rc.633), but I will keep this issue open until someone has confirmed this did it for them.
I have updated to the latest next release and tried to replicate the previous scenarios that were failing. The MaxListenersExceededWarning is no longer being triggered on my side.
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.
Activity
yspreen commentedon Nov 6, 2024
here's the code:
As far as I can tell, the signal should get aborted when the connection breaks. Instead, if I close the tab or refresh the page, I see next.js reporting that the SSE request finished, but trpc keeps running this loop. The console log confirms that abort is false.
adri1wald commentedon Nov 7, 2024
We're experiencing related issues. Resources are never getting cleaned up because our
returnmethod on theAsyncIterableis not being called after connection closes. This is causing a memory leak and our servers to OOM every couple of hours.KATT commentedon Nov 8, 2024
Thanks so much for reporting this with a good repro.
We just merged a PR that should fix this in #6204.
Please let us know how it goes and open a new issue if it's still a problem or if you experience other issues. Thanks
KATT commentedon Nov 8, 2024
FYI, not sure why, but I can't seem to verify the fix on StackBlitz, but when running the project locally it works well, seems like the request's events aren't properly propagated in that environment somehow
yspreen commentedon Nov 8, 2024
@KATT yeah I was just testing it on stackblitz and the issue was still there. I'm seeing if manually purging all packages and running the install command again with 621 fixes it...
yspreen commentedon Nov 8, 2024
it certainly feels like this issue is still 100% reproducible on stackblitz
KATT commentedon Nov 8, 2024
Keeping it open until you've checked
yspreen commentedon Nov 8, 2024
@KATT I can confirm that the issue is fixed in my project. I'd still prefer some further investigation since stackblitz still shows the bug, and I think as long as it's reproducible the issue should stay open
yspreen commentedon Nov 8, 2024
I just updated the stackblitz repo to use the new version.
yspreen commentedon Nov 8, 2024
a good way to cause the error is simply to hit refresh in stackblitz. that doubles the frequency of calls
yspreen commentedon Nov 8, 2024
on the other hand, while the bug happened locally, I could still see next.js terminate the request once the page closed. stackblitz doesn't show that log line. so maybe it is an issue of their internal proxy not terminating the requests...
10 remaining items
yspreen commentedon Nov 9, 2024
Is that a change from the latest release? Because the latest release didn't seem to be working for me on fly.io I had to make the change with the three different handlers above.
yspreen commentedon Nov 11, 2024
I ended up moving to my own SSE server and client implementation so I won't be able to repro any further changes to this in my own infrastructure. I recommend testing on fly.io or another proxied provider before closing this.
KATT commentedon Nov 11, 2024
How come? Was there anything you were missing in ours? Happy to hop on a call to understand this better
Spent a lot of time on SSE this week and got some more improvements coming, sad to see you go 😅
SiNONiMiTY commentedon Nov 12, 2024
I am also encountering an issue that might be related to this
All I have to do is just call a procedure 10 times, and this error will be visible on the console
Using the Fastify Adapter
adri1wald commentedon Nov 12, 2024
#6204 fixed the memory leak but when we enabled pings, it returned (possibly via another mechanism).
We enabled pings because we are seeing extremely high numbers of ELB 502s (orders of magnitude above Target 5XXs) after we upgraded to TRPC11 with SSE, and we thought it's because the connection is idle for too long. Curiously, it also didn't fix the ELB 502s rate.
tarekwiz commentedon Nov 13, 2024
I can confirm this happens with the fetch adapter as well for Astro.
The abortController never gets aborted when the browser tab exits (request drops)
KATT commentedon Nov 13, 2024
Thanks for all the details everyone, we've now updated the code to:
trpc/packages/server/src/adapters/node-http/incomingMessageToRequest.ts
Lines 139 to 140 in 5551276
This really should be sufficient to do detection of canceled requests.
Other new changes (changelog):
EventSourceitself isn't really great at detecting stale connectionskill -9of a server is enough to enough for Chrome'sEventSourceto hang in my testing)experimental.sseSubscriptions: {}has moved tosse: {}(release note)Apologies to anyone who have encountered this issue. It should be addressed in latest release (
11.0.0-rc.633), but I will keep this issue open until someone has confirmed this did it for them.SiNONiMiTY commentedon Nov 14, 2024
I have updated to the latest
nextrelease and tried to replicate the previous scenarios that were failing. TheMaxListenersExceededWarningis no longer being triggered on my side.KATT commentedon Nov 14, 2024
I will close this now then as there's been a confirmed fix.
Don't hesitate to reopen issues if you experience any issues (even if you can't make a good repro, I know that is hard if it's deployment dependent)
github-actions commentedon Nov 15, 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.