Skip to content

fix(browser): Fix memory leak in addEventListener instrumentation - #5147

Merged
lforst merged 6 commits into
7.xfrom
lforst/event-fixes
May 24, 2022
Merged

fix(browser): Fix memory leak in addEventListener instrumentation#5147
lforst merged 6 commits into
7.xfrom
lforst/event-fixes

Conversation

@lforst

@lforstlforst commented May 20, 2022

Copy link
Copy Markdown
Contributor

We currently have a memory leak in the instrumentation of addEventListener in @sentry/browser. It happens when users provide an object as event listener to addEventListener. The handleEvent function gets wrapped and instrumented each time the listener is attached somewhere.

This PR:

  • Aims to get rid of the memory leak
  • Adds some integration tests that tests for potential issues with the instrumentation of addEventListener in the future

Fixes#2957
Ref: https://getsentry.atlassian.net/browse/WEB-916

Reproduction of the memory leak

<!DOCTYPE html><htmllang="en"><head><scriptsrc="https://browser.sentry-cdn.com/6.19.7/bundle.min.js"></script></head><body><h1>Listener memory leak repro</h1><p>
Instructions:<br/>
1) Open console<br/>
2) Press "add listener" a bunch of times<br/>
3) Press "trigger listener"<br/>
4) Repeat step 2 and 3<br/>
5) Examine trace<br/></p><buttononclick="addListener()">add listener</button><buttonid="trigger">trigger listener</button><script>Sentry.init({dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",});constlistener={handleEvent(){console.trace("listener called");},};functionaddListener(){console.log("adding listener");document.getElementById("trigger").addEventListener("click",listener);}</script></body></html>

@github-actions

github-actionsBot commented May 20, 2022

Copy link
Copy Markdown
Contributor

size-limit report 📦

PathSize
@sentry/browser - ES5 CDN Bundle (gzipped + minified)19.32 KB (-4.07% 🔽)
@sentry/browser - ES5 CDN Bundle (minified)59.9 KB (-7.3% 🔽)
@sentry/browser - ES6 CDN Bundle (gzipped + minified)18.18 KB (-3.63% 🔽)
@sentry/browser - ES6 CDN Bundle (minified)53.58 KB (-7.57% 🔽)
@sentry/browser - Webpack (gzipped + minified)19.87 KB (-14.49% 🔽)
@sentry/browser - Webpack (minified)63.03 KB (-22.87% 🔽)
@sentry/react - Webpack (gzipped + minified)19.89 KB (-14.53% 🔽)
@sentry/nextjs Client - Webpack (gzipped + minified)43.7 KB (-9.06% 🔽)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified)25.33 KB (-2.87% 🔽)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified)23.88 KB (-2.46% 🔽)

@lforst
lforstforce-pushed the lforst/event-fixes branch from b3f526f to 9818be5CompareMay 23, 2022 13:43
@lforst
lforst marked this pull request as ready for review May 23, 2022 13:44
@lforst
lforst requested review from Lms24 and kamilogorekMay 23, 2022 13:44

@Lms24Lms24 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome stuff!

@kamilogorekkamilogorek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Simple fix, yet so much value-added through great tests. Thanks 🙏

Sign up for freeto 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

@lforst@kamilogorek@Lms24@AbhiPrasad