Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): possible memory leak when using server side events #13453

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

zhengjitf
Copy link

Closes: #11601

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #11601

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@coveralls
Copy link

Pull Request Test Coverage Report for Build 872c4fb8-4a0a-4449-b755-a6d8ef9c9301

Details

  • 4 of 4 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 92.138%

Totals Coverage Status
Change from base Build e8e3c154-2e4b-4603-8729-3ff01d3ab650: 0.02%
Covered Lines: 6739
Relevant Lines: 7314

💛 - Coveralls

@micalevisk
Copy link
Member

@zhengjitf can you share a minimum reproduction of this potential bug?

@zhengjitf
Copy link
Author

@zhengjitf can you share a minimum reproduction of this potential bug?

@micalevisk Sorry for not having much description for this PR, a minimum is mentioned from #11601 (comment), and I committed a unit test to reproduce this issue before fixing it, (it might be not intuitive?).

I’d like to describe my changes, along with investigation:

The MaxListenersExceededWarning comes out when the line 120 code runs too frequently.

if (!this.write(message, 'utf-8', cb)) {
this.once('drain', cb);
} else {

Then I noticed there is a limitation intention in the following code, but it doesn't work, which I understand is to limit only emitting the value when meanwhile the previous writing is completed (the previous drain event is emitted), for this intention it should be replaced with exhaustMap, but I think concatMap is more reasonable, in a serialized fashion waiting for the previous writing completed, as a result, will only one listener binds drain event simultaneously.

debounce(
message =>
new Promise<void>(resolve =>
stream.writeMessage(message, () => resolve()),
),
),

Based on my understanding, this issue will happen when trying to write a message but meanwhile the previous writing is not completed, this leads to binding more listeners to the drain event until exceeds the max listener limitation (default as 10).

A realistic case is mentioned in #11601:

I use Server-Sent Events for one of my routes and then push db changes for subscribed users.

For this case, the issue is likely to happen when the db changes frequently and each pushed event data is large.

PS: I didn't run into this issue, I'm trying to implement a SSE route with POST method and did some investigation in the issues panel and source code, then found this issue and hope to make some efforts to address it.

I would appreciate it if any misunderstanding or mistakes from me are pointed out.

Copy link

@benjGam benjGam left a comment

Choose a reason for hiding this comment

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

It seems to be a clever alternative to do not overload the process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible memory leak when using server side events
4 participants