Skip to content

fix: Memoize AsyncLocalStorage instance #8831

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

Merged
merged 1 commit into from
Aug 17, 2023
Merged

Conversation

lforst
Copy link
Contributor

@lforst lforst commented Aug 17, 2023

Attempts to fix the memory leak reported in #8829 by memoizing the AsyncLocalStorage instance we create when setting the async context strategy.

Note for reviewers: Can you think of any problems this may cause?

@github-actions
Copy link
Contributor

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 75.16 KB (+0.41% 🔺)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.17 KB (+0.2% 🔺)
@sentry/browser - Webpack (gzipped) 21.85 KB (+0.19% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 69.73 KB (+0.27% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 28.18 KB (+0.1% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped) 20.18 KB (+0.05% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 219.89 KB (+0.38% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 84.78 KB (+0.13% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 59.86 KB (+0.04% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 31.04 KB (+0.09% 🔺)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 75.18 KB (+15.57% 🔺)
@sentry/react - Webpack (gzipped) 21.88 KB (+0.18% 🔺)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 92.97 KB (+0.35% 🔺)
@sentry/nextjs Client - Webpack (gzipped) 50.7 KB (+0.19% 🔺)

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Hmm I'm not sure but wouldn't this mean we potentially reuse the same asyncLocalStorage instance across multiple requests which we actually want to isolate? Don't have a ton of context around ALS so not sure if this concern makes sense

@lforst
Copy link
Contributor Author

lforst commented Aug 17, 2023

Hmm I'm not sure but wouldn't this mean we potentially reuse the same asyncLocalStorage instance across multiple requests

Well yes, but that is theoretically not an issue. What matters is that localStorage.run(obj) is called for each separate isolate. To give you some insight, whenever you run localStorage.run(obj) you set what localStorage.get() returns in that particular async context. It shouldn't matter that it's the same AsyncLocalStorage instance.

I am trying to come up with a case where we might run into problems here, but I am struggling. I guess the baseline where this potentially messes up always involves calling Sentry.init() multiple times (which is already kinda funky and not supported) and then doing weird stuff with hubs.

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Thanks for explaining, this makes sense to me now! I'd say we give it a try and if the issue author is comfortable with trying this out locally first, it'd be even better.

@lforst lforst requested review from timfish and AbhiPrasad August 17, 2023 08:30
@lforst
Copy link
Contributor Author

lforst commented Aug 17, 2023

TIL the leak is likely because AsyncLocalStorage objects need to be disabled in order to be garbage collected: docs

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

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

:shipit:

@timfish
Copy link
Collaborator

timfish commented Aug 17, 2023

Wouldn't this stop us or customers from overriding the async strategy after it's been set by @sentry/node?

@timfish
Copy link
Collaborator

timfish commented Aug 17, 2023

Wouldn't this stop us or customers from overriding the async strategy after it's been set by @sentry/node?

No sorry, I misundersttod the changes!

@lforst lforst merged commit e5a3885 into develop Aug 17, 2023
@lforst lforst deleted the lforst-fix-als-mem-leak branch August 17, 2023 11:52
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.

4 participants