-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Run specific code outside of Sentry domain/hub/scope #7031
Comments
Hey @nwalters512, thanks for writing in! This is a tricky problem, and we're not sure how to exactly solve it at the moment in part because there is no way to write a We can see here that the req and res event emitters are put on the domain: sentry-javascript/packages/node/src/handlers.ts Lines 185 to 187 in 7532522
I think this is the source of your issue. I do have a question though - why are you putting these objects on the event emitters? Could you take advantage of |
It's a common and officially documented pattern to place request-specific context on |
I suppose I can ask the same question though: is there a reason that Sentry uses the deprecated Domains API as opposed to |
@nwalters512 AFAIK ALS was not available when You can find a more detailed discussion here: #4071 . Later edit: also this: #3660 . |
@nwalters512 thanks for your patience. With the |
Problem Statement
My web server maintains a pool of long-lived resources. These resources are created on application startup and used in some of the requests. However, if we detect that a resource is unhealthy during a request, we'll remove it and create a new one.
These resources each maintain a long-lived socket. As best as I can tell, Sentry's hub/scope mechanisms maintain a reference to the incoming HTTP request and associate it with this socket. If the request object is very large (e.g. it contains a lot of data on
res.locals
), this data will be retained effectively forever, even once the request finishes. This manifests as a memory leak.I'm not entirely sure of how Sentry's internals work, but this appears to be related to Sentry's usage of Node domains to track request context across async operations. If I take a heap snapshot at runtime, I can see that the response is ultimately retained in a domain's members, and if I disable Sentry completely, the response is no longer retained in any domain.
I've created a small repo to illustrate this issue: https://github.com/nwalters512/sentry-domain-memory-leak. Follow these steps to see the issue:
yarn install
docker pull ubuntu
node --inspect index.js
http://localhost:80/
in your browser.index.js
, and repeat the above steps. Note that memory consumption should fall back to ~10MB either immediately or after triggering a garbage collection.I'm writing this as a feature request because I have a relatively clear idea of what I want from Sentry: the ability to run a piece of code completely outside of Sentry's domains/hubs/scopes. AFAICT from the docs, there's not currently a way to do this.
Solution Brainstorm
From an API standpoint, something like this would be nice:
Not knowing much about Sentry's internals, I can't really guess as to exactly how this could be implemented.
The text was updated successfully, but these errors were encountered: