Skip to content

Commit 8489072

Browse files
authored
fix(browser): Call removeEventListener twice only when necessary (#3016)
We were handling an edge case (of an event handler being attached in both its wrapped and pre-wrapped states) as if it were happening in all cases. This checks for the edge case first, and only handles it when necessary.
1 parent 75d3225 commit 8489072

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

packages/browser/src/integrations/trycatch.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export class TryCatch implements Integration {
203203
});
204204

205205
fill(proto, 'removeEventListener', function(
206-
original: () => void,
206+
originalRemoveEventListener: () => void,
207207
// eslint-disable-next-line @typescript-eslint/no-explicit-any
208208
): (this: any, eventName: string, fn: EventListenerObject, options?: boolean | EventListenerOptions) => () => void {
209209
return function(
@@ -230,12 +230,16 @@ export class TryCatch implements Integration {
230230
* then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible
231231
* to get rid of the initial handler and it'd stick there forever.
232232
*/
233+
const wrappedEventHandler = (fn as unknown) as WrappedFunction;
233234
try {
234-
original.call(this, eventName, ((fn as unknown) as WrappedFunction).__sentry_wrapped__, options);
235+
const originalEventHandler = wrappedEventHandler?.__sentry_wrapped__;
236+
if (originalEventHandler) {
237+
originalRemoveEventListener.call(this, eventName, originalEventHandler, options);
238+
}
235239
} catch (e) {
236240
// ignore, accessing __sentry_wrapped__ will throw in some Selenium environments
237241
}
238-
return original.call(this, eventName, fn, options);
242+
return originalRemoveEventListener.call(this, eventName, wrappedEventHandler, options);
239243
};
240244
});
241245
}

0 commit comments

Comments
 (0)