Sentry-javascript: withScope memory leak, very obvious when using scope.setTag in @sentry/node

Created on 12 Aug 2019  ·  3Comments  ·  Source: getsentry/sentry-javascript

Package + Version

  • [ ] @sentry/browser
  • [x] @sentry/node
  • [ ] raven-js
  • [ ] raven-node _(raven for node)_
  • [ ] other:

Version:

5.5.0

Description

Creating a new issue since #1762 was closed.
There exists a small memory leak in @sentry/node. Calling setTag with some long strings makes it a lot larger.
This is identifiable because it's not only a memory leak, but according to my DO droplet metrics, it causes a permanent increase in CPU usage & Bandwidth public that can only be fixed by restarting the server.
Pic shows what happens when I added setTag (July 11) and when I removed it (Aug 9).
System memory (1)

Needs Reproduction

Most helpful comment

@kamilogorek the offending code is here: https://github.com/ParabolInc/action/blob/0bde4b002aa3d53fc00f1febcb39185079d827f2/packages/server/utils/sendToSentry.ts#L28-L35

from the looks of it, the scope isn't getting GC'd.

it may be unpopular, but all i really want is a clean API call that takes an exception & variables. e.g. Sentry.captureExpection(error, {tags: {foo: 1}}). that would get rid of the scopes & associated memory leaks.

All 3 comments

Are you able to provide some repro-case for this? setTag with multiple concurrent requests will for sure increase a memory footprint, but once a scope is GC'd, memory should drop to the baseline.

@kamilogorek the offending code is here: https://github.com/ParabolInc/action/blob/0bde4b002aa3d53fc00f1febcb39185079d827f2/packages/server/utils/sendToSentry.ts#L28-L35

from the looks of it, the scope isn't getting GC'd.

it may be unpopular, but all i really want is a clean API call that takes an exception & variables. e.g. Sentry.captureExpection(error, {tags: {foo: 1}}). that would get rid of the scopes & associated memory leaks.

withScope at the end of it's lifecycle performs a popScope operation which get rid of the layer from the internal array, thus there's no strong reference to it - GC takes care of it automatically.
fwfw you can change your call to:

Sentry.withScope((scope) => {
  scope.setUser(user)
  scope.setTags(tags)
  Sentry.captureException(error)
});
Was this page helpful?
0 / 5 - 0 ratings