-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(browser-utils): Stop leaking DOM instrumentation listeners on mismatched removals #24727
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
Open
Lms24
wants to merge
1
commit into
develop
Choose a base branch
from
fix/dom-instrumentation-listener-leak
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,94 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { instrumentDOM } from '../../src/instrumentation/dom'; | ||
| import { WINDOW } from '../../src/types'; | ||
|
|
||
| // @ts-expect-error - idk | ||
| WINDOW.XMLHttpRequest = undefined; | ||
|
|
||
| describe('instrumentXHR', () => { | ||
| it('it does not throw if XMLHttpRequest is a key on window but not defined', () => { | ||
| describe('instrumentDOM', () => { | ||
| const { addEventListener: nativeAdd, removeEventListener: nativeRemove } = EventTarget.prototype; | ||
|
|
||
| // `instrumentDOM` patches `EventTarget.prototype` and isn't idempotent, so restore the native methods after every test. | ||
| afterEach(() => { | ||
| EventTarget.prototype.addEventListener = nativeAdd; | ||
| EventTarget.prototype.removeEventListener = nativeRemove; | ||
| }); | ||
|
|
||
| /** Runs `instrumentDOM` and returns a function counting the click listeners actually attached to `document`. */ | ||
| function instrumentAndTrackDocumentClickListeners(): () => number { | ||
| const documentClickListeners = { capture: new Set<unknown>(), bubble: new Set<unknown>() }; | ||
|
|
||
| const phase = (options?: boolean | EventListenerOptions): Set<unknown> => | ||
| (typeof options === 'boolean' ? options : !!options?.capture) | ||
| ? documentClickListeners.capture | ||
| : documentClickListeners.bubble; | ||
|
|
||
| // Installed before `instrumentDOM` so these sit underneath the SDK and also see the listeners it attaches itself. | ||
| EventTarget.prototype.addEventListener = function (type, listener, options) { | ||
| if (this === document && type === 'click') { | ||
| phase(options).add(listener); | ||
| } | ||
| return nativeAdd.call(this, type, listener, options); | ||
| }; | ||
|
|
||
| EventTarget.prototype.removeEventListener = function (type, listener, options) { | ||
| if (this === document && type === 'click') { | ||
| phase(options).delete(listener); | ||
| } | ||
| return nativeRemove.call(this, type, listener, options); | ||
| }; | ||
|
|
||
| instrumentDOM(); | ||
|
|
||
| return () => documentClickListeners.capture.size + documentClickListeners.bubble.size; | ||
| } | ||
|
|
||
| it('does not throw if XMLHttpRequest is a key on window but not defined', () => { | ||
| expect(instrumentDOM).not.toThrow(); | ||
| }); | ||
|
|
||
| it('does not leak document click listeners when removeEventListener uses mismatched capture options', () => { | ||
| const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); | ||
|
|
||
| // baseline listenercount is 1 which comes from the SDK's global click handler registered | ||
| // in instrumentDOM(). | ||
| const baseline = countDocumentClickListeners(); | ||
|
|
||
| const never = (): void => {}; | ||
| const onCapture = (): void => {}; | ||
| const onBubble = (): void => {}; | ||
|
|
||
| for (let i = 0; i < 20; i++) { | ||
| document.addEventListener('click', onCapture, true); | ||
| document.addEventListener('click', onBubble); | ||
| document.removeEventListener('click', never); | ||
| document.removeEventListener('click', never); | ||
| document.removeEventListener('click', onCapture, true); | ||
| document.removeEventListener('click', onBubble); | ||
| } | ||
|
|
||
| expect(countDocumentClickListeners() - baseline).toBe(0); | ||
| }); | ||
|
|
||
| it('keeps its handler attached while listeners remain, even after removing listeners that were never added', () => { | ||
| const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); | ||
| const baseline = countDocumentClickListeners(); | ||
|
|
||
| const never = (): void => {}; | ||
| const onClick = (): void => {}; | ||
|
|
||
| document.addEventListener('click', onClick); | ||
| document.removeEventListener('click', never); | ||
| document.removeEventListener('click', onClick, true); | ||
|
|
||
| // `onClick` plus the SDK's handler for it | ||
| expect(countDocumentClickListeners() - baseline).toBe(2); | ||
|
|
||
| document.removeEventListener('click', onClick); | ||
|
|
||
| expect(countDocumentClickListeners() - baseline).toBe(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: i think this creates another subtle leakage: listeners that are added with
{ once: true }or aborted by passing a{ signal }are automatically cleaned up by the browser and don't go throughremoveEventListener, thus remaining in these sets indefinitely.Can we track if either of these are set, then remove the listener from the set if either the signal aborts or the
{ once: true }listener has fired? WDYT?