Skip to content

feat(browser): Add unregisterOriginalCallbacks option to browserApiErrorsIntegration #16412

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 4 commits into from
May 28, 2025

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented May 28, 2025

This PR adds an option to fix a very unfortunate, effective double invocation of callbacks on objects inheriting from our EventTarget.addEventListener instrumentation (to be found in browserApiErrorsIntegration.

By default (without the SDK), adding the same listener to an event target twice results in the listener only being added once and hence only being invoked once:

const myClickListener = () => {
  console.log('clicked');
};

btn.addEventListener('click', myClickListener);
btn.addEventListener('click', myClickListener);

If however, the SDK is initialized between the two calls, the second call doesn't actually register myClickListener but a wrapped version of the listener. Hence, the identities of the listeners differ and they're added twice, resulting in them being invoked twice:

const myClickListener = () => {
  console.log('clicked');
};

btn.addEventListener('click', myClickListener);

Sentry.init(...)

btn.addEventListener('click', myClickListener);

This is the reason why sometimes, our SDK can indeed cause for example double button clicks, as reported in #16398.

To fix this, this PR introduces an unregisterOriginalCallbacks option to the browserApiErrorsIntegration which affected users can switch on. Doing so will make our instrumentation of addEventListener also call removeEventListener on for the original callback. Applied to the example above, it will essentially restore the idempotency of addEventListener. At the same time, it feels very risky to do this so for the time being, we'll keep this (and document it as) opt-in.

Usage:

Sentry.init({
  // ...
  integrations: [
    Sentry.browserApiErrorsIntegration({
      unregisterOriginalCallbacks: true,
    }),
  ],
});

Docs PR: getsentry/sentry-docs#13859

fixes #16398

@Lms24 Lms24 changed the title test(browser): Add test demonstrating EventTarget double instrumentation test(browser): Add test demonstrating EventTarget double invocation of event listeners May 28, 2025
@Lms24 Lms24 self-assigned this May 28, 2025
@Lms24 Lms24 changed the title test(browser): Add test demonstrating EventTarget double invocation of event listeners feat(browser): Add unregisterOriginalCallbacks option to browserApiErrorsIntegration May 28, 2025
@Lms24 Lms24 marked this pull request as ready for review May 28, 2025 13:59
Copy link
Contributor

github-actions bot commented May 28, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.99 kB +0.26% +60 B 🔺
@sentry/browser - with treeshaking flags 23.76 kB +0.25% +59 B 🔺
@sentry/browser (incl. Tracing) 38.29 kB +0.16% +61 B 🔺
@sentry/browser (incl. Tracing, Replay) 76.43 kB +0.08% +56 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 69.55 kB +0.1% +64 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 81.19 kB +0.08% +57 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 93.27 kB +0.06% +51 B 🔺
@sentry/browser (incl. Feedback) 40.73 kB +0.13% +50 B 🔺
@sentry/browser (incl. sendFeedback) 28.7 kB +0.21% +60 B 🔺
@sentry/browser (incl. FeedbackAsync) 33.59 kB +0.17% +55 B 🔺
@sentry/react 25.76 kB +0.18% +46 B 🔺
@sentry/react (incl. Tracing) 40.29 kB +0.15% +57 B 🔺
@sentry/vue 28.34 kB +0.27% +74 B 🔺
@sentry/vue (incl. Tracing) 40.13 kB +0.21% +84 B 🔺
@sentry/svelte 24.01 kB +0.27% +64 B 🔺
CDN Bundle 25.31 kB +0.24% +60 B 🔺
CDN Bundle (incl. Tracing) 38.47 kB +0.16% +58 B 🔺
CDN Bundle (incl. Tracing, Replay) 74.33 kB +0.09% +60 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 79.75 kB +0.06% +47 B 🔺
CDN Bundle - uncompressed 73.9 kB +0.3% +215 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 113.88 kB +0.19% +215 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 227.85 kB +0.1% +215 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 240.67 kB +0.09% +215 B 🔺
@sentry/nextjs (client) 41.96 kB +0.11% +46 B 🔺
@sentry/sveltekit (client) 38.79 kB +0.16% +61 B 🔺
@sentry/node 149.87 kB - -
@sentry/node - without tracing 98.12 kB - -
@sentry/aws-serverless 123.88 kB - -

View base workflow run

@Lms24 Lms24 requested review from a team, chargome, andreiborza and mydea and removed request for a team May 28, 2025 14:02
@Lms24 Lms24 merged commit 7116874 into develop May 28, 2025
307 of 309 checks passed
@Lms24 Lms24 deleted the lms/test-browser-demonstrate-double-listener branch May 28, 2025 14:53
Lms24 added a commit to getsentry/sentry-docs that referenced this pull request May 30, 2025
…serApiErrors` integration page (#13859)

Documents a new option added via
getsentry/sentry-javascript#16412. Also added a
troubleshooting section as adding this option was the result of
investigating a user-reported SDK issue
(getsentry/sentry-javascript#16398)

Co-authored-by: Alex Krawiec <[email protected]>
antonpirker pushed a commit to getsentry/sentry-docs that referenced this pull request Jun 6, 2025
…serApiErrors` integration page (#13859)

Documents a new option added via
getsentry/sentry-javascript#16412. Also added a
troubleshooting section as adding this option was the result of
investigating a user-reported SDK issue
(getsentry/sentry-javascript#16398)

Co-authored-by: Alex Krawiec <[email protected]>
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.

Issue with Dynamic Media video player
2 participants