Skip to content

[node][7.113.0] tracingHandler of memory leak #11945

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

Closed
3 tasks done
ProsperBao opened this issue May 8, 2024 · 7 comments
Closed
3 tasks done

[node][7.113.0] tracingHandler of memory leak #11945

ProsperBao opened this issue May 8, 2024 · 7 comments
Labels
Package: node Issues related to the Sentry Node SDK Stale

Comments

@ProsperBao
Copy link

ProsperBao commented May 8, 2024

Is there an existing issue for this?

How do you use Sentry?

Self-hosted/on-premise

Which SDK are you using?

@sentry/node

SDK Version

7.113.0

Framework Version

nitro latest

Link to Sentry event

No response

SDK Setup

Sentry.init({
    dsn: '',
    environment: 'debug',
    sampleRate: 1.0,
    integrations: [
      ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
    ],
    tracesSampler: (samplingContext) => {
      if (samplingContext.parentSampled !== undefined) {
        return samplingContext.parentSampled
      }
      else {
        // return 0.0005
        return 1
      }
    },
  })

Steps to Reproduce

reproducible example

  1. pnpm build:preview
  2. pnpm test
  3. open chrome://inspect/#devices

This is the memory footprint at the beginning and code

image

image

execute pnpm test, then wait for the number to reach 5000.

image

image

you can see that the memory footprint has not decreased

many instances created by sentry are not released
image

Then I looked at the source code, Modified my code
image
image

image

It's very clean,

I think here in the code but less clear action, I don't know if changing here will cause other effects

setImmediate(() => {

Expected Result

There should be no risk of memory leaks

Actual Result

Many internal instances of sentry are not released

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 May 8, 2024
@github-actions github-actions bot added the Package: node Issues related to the Sentry Node SDK label May 8, 2024
@Lms24
Copy link
Member

Lms24 commented May 8, 2024

Hi @FuBaooo thanks for writing in! I took a look at your reproduction example (admittedly didn't run it). I have a feeling that the leak is happening because the Sentry.Handlers.requestHandler() is missing in your setup. I'd highly recommend adding the requesthandler because it takes care of isolating scopes for each request. This is crucial to avoid leaking data between concurrent requests. Most importantly, I think there's a good chance that the leak is cleaned up.

Since we don't have a guide (or official support) for Nitro, I'm gonna link you to our express documentation which shows how to use the handler in express. You probably need to register the handler similarly to the tracingHandler in your setup. Also please note, the order between tracingHandler and requestHandler is important, as pointed out here.

Can you give this a try and report back if it fixes your mem leak?

Also this issue might be related to #10790, where users also reported a memory leak. Did you use an older version of the SDK before encountering the leak? If yes, which exact version was that? We're still trying to narrow down the responsible change that caused the leaks reported in this issue.

Finally, we're currently in the final stretch of releasing a new major version of the SDK which will use OpenTelemetry for instrumentation of Node frameworks. So chances are, you won't encounter this leak at all anymore once our new major is stable and you upgraded.

@ProsperBao
Copy link
Author

@Lms24 Sorry, I do use requestHandler in my actual project, I've added it to the repository, and I don't think requestHandler has to do with memory leaks.

I think it's the lack of getCurrentScope()?.clear() in res.once('finish').
I added getCurrentScope()?.clear() directly to 'node_modules/@sentry/node/esm/handers', the memory leak was solved.

image

To fix the memory leak, I executed this action in my code:

image

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 May 8, 2024
@ProsperBao
Copy link
Author

@Lms24 I made sure I registered the requestHandler first and then the tracingHandler in the right order.

Extra note: The numbers attached to the front of the file are the nitro registration order.

image

@ProsperBao
Copy link
Author

@Lms24 I haven't used the old sdk, this is the latest version

@Lms24
Copy link
Member

Lms24 commented May 8, 2024

Thanks for checking! Now at least we know that the request handler doesn't seem to play a part in this. I need to backlog this for the moment due to v8 being the top priority. Which is unfortunate for everyone still on v7, so sorry for that.

Also thanks for the reproduction, it'll be for sure helpful once we take another look at this.

@mydea
Copy link
Member

mydea commented May 15, 2024

Hey, we've released v8.0.0 of the SDK, which has a new instrumentation API. Could you give that a try, and see if the memory leak is gone? You can find info on how to update here: https://docs.sentry.io/platforms/javascript/guides/node/migration/v7-to-v8/

@getsantry getsantry bot moved this to Waiting for: Community in GitHub Issues with 👀 3 May 15, 2024
@getsantry
Copy link

getsantry bot commented Jun 6, 2024

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry bot added the Stale label Jun 6, 2024
@getsantry getsantry bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: node Issues related to the Sentry Node SDK Stale
Projects
Archived in project
Development

No branches or pull requests

3 participants