nest: Possible memory leak when using server side events
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
I use Server-Sent Events for one of my routes and then push db changes for subscribed users. The functionality works fine, but as soon as the event count is 10k for example, the error appears:
(node:43243) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 101 drain listeners added to [SseStream]. Use emitter.setMaxListeners() to increase limit
Minimum reproduction code
https://gist.github.com/ggagosh/0850fd0100f17e2bc040095a917c0342
Steps to reproduce
No response
Expected behavior
I suppose should work without any warning
Package
- I don’t know. Or some 3rd-party package
-
@nestjs/common -
@nestjs/core -
@nestjs/microservices -
@nestjs/platform-express -
@nestjs/platform-fastify -
@nestjs/platform-socket.io -
@nestjs/platform-ws -
@nestjs/testing -
@nestjs/websockets - Other (see below)
Other package
No response
NestJS version
^9.0.0
Packages versions
"@nestjs/common": "^9.0.0",
"@nestjs/config": "2.3.1",
"@nestjs/core": "^9.0.0",
"@nestjs/event-emitter": "^1.4.1",
"@nestjs/graphql": "^11.0.5",
"@nestjs/jwt": "10.0.3",
"@nestjs/mongoose": "^9.2.2",
"@nestjs/passport": "9.0.3",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "6.3.0",
"@nestjs/terminus": "^9.2.2",
Node.js version
18.16.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 2
- Comments: 21 (6 by maintainers)
Commits related to this issue
- test(core): add a sse failed test to reproduce #11601 — committed to zhengjitf/nest by zhengjitf 2 months ago
I have the same issue, has anyone found some workaround/solution to this problem? In my case, I can reproduce the issue by sending HTTP requests and immediately cancelling them, after a couple of cancelled requests the warning pops up.
I have the same issue:
(node:167175) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [SseStream]. Use emitter.setMaxListeners() to increase limitSeems like some sort of issue with usd adding
this.once('drain')here. Interestingly it doesn’t happen when using rxjs directly viaintervalStack Trace
I was able to fix my issue by completing the observable when the request socket is closed.
You can use an AbortController and signal it to tell your other code to stop emitting events.
Hey, this is the example branch
I take the interval example from NestJS documentation, I just change the interval time to 0, because first I think the warning was caused by the amount of data I try to return.
In my case, I can reproduce this by aborting the client connection. Basically, start a SSE stream on the browser and then hit F5 to reload the webpage. This makes the browser disconnect and then the server will show this error.
I call
subject.complete()example nest application you can use to reproduce this behavior. after running the application, use Postman or any other similar application and enter
localhost:3000/sse, then check the console where the warning appears.then I don’t think that that code is enough to reproduce your issue, right? Please share the full code along with the steps to reproduce.
why reproductions are required