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(fetch): Fix memory leak when handling endless streaming #13809

Merged
merged 5 commits into from
Oct 3, 2024

Conversation

soapproject
Copy link
Contributor

This is a quick fix to address a memory overflow issue caused by the recursive approach when handling endless streams (e.g., CCTV).

However, this is not a perfect solution, as this approach still does not trigger the onFinishedResolving callback for streams that never terminate.

Fixes GH-13806

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • [] Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

The lints didn't pass but they are unrelated to the changes in this commit?
image

This is a quick fix to address a memory overflow issue caused by the recursive approach when handling endless streams (e.g., CCTV).
However, this is not a perfect solution, as this approach still does not trigger the onFinishedResolving callback for streams that never terminate.

Fixes getsentryGH-13806
@mydea mydea requested a review from lforst September 26, 2024 11:03
adjust format
@AbhiPrasad AbhiPrasad self-requested a review September 26, 2024 12:59
@lforst lforst self-assigned this Sep 30, 2024
@lforst
Copy link
Member

lforst commented Sep 30, 2024

Hi, thanks for opening the PR! Would you mind outlining the thought process behind the releaseLock() call? I think I get it but want to be sure we all know what's going on because this is a super hot path in the SDK.

@soapproject
Copy link
Contributor Author

Hi, actually the improvement was referenced from GTP.
But I think it is correct to use releaseLock, after read the ReadableStream doc:
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader

getReader locks the stream and returns either a ReadableStreamDefaultReader or a ReadableStreamBYOBReader.
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/releaseLock
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader/releaseLock

So we need to release the lock and de-activate the reader to ensure that res.body is not locked.

Maybe we can experiment with calling resolveResponse multiple times without releasing the lock to see what happens?

@soapproject
Copy link
Contributor Author

btw, we've encountered another issue when working with CCTV streaming. Even after leaving the page and calling videojs.dispose(), the stream continues to fetch data in the background.

I think this might require opening a new issue, but could this behavior be related to the code in fetch.ts?

@lforst
Copy link
Member

lforst commented Oct 2, 2024

I have updated this PR to:

  • not update the span duration in every case we cancel the operation
  • add a maximum timeout of 90s (this is arbitrary)
  • remove some unnecessary async/awaits
  • cancel the body (and implicitly the reader) to stop putting stuff into memory as it streams in

@lforst lforst requested a review from chargome October 2, 2024 14:30
@@ -158,7 +175,8 @@ async function streamHandler(response: Response): Promise<void> {
return;
}

await resolveResponse(clonedResponseForResolving, () => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
resolveResponse(clonedResponseForResolving, () => {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we want leave this floating?

Copy link
Member

Choose a reason for hiding this comment

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

It is fine to have floating promises if you are certain that they cannot reject - i think this is the case here!

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.

Memory leak with endless streaming (example: CCTV)
3 participants