Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/browser/src/tracing/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,16 @@ export const browserTracingIntegration = ((options: Partial<BrowserTracingOption
function emitFinish(): void {
if (optionalWindowDocument && ['interactive', 'complete'].includes(optionalWindowDocument.readyState)) {
client.emit('idleSpanEnableAutoFinish', idleSpan);
// Once the finish signal has been emitted, the listener is no longer needed. Removing it here (rather than
// relying on `{ once: true }`) also covers the common case where the document is already loaded when the span
// starts, so the listener never fires and would otherwise leak together with the `idleSpan` it closes over.
optionalWindowDocument.removeEventListener('readystatechange', emitFinish);
}
}

// Enable auto finish of the pageload span if users are not explicitly ending it
if (isPageloadSpan && !enableReportPageLoaded && optionalWindowDocument) {
optionalWindowDocument.addEventListener('readystatechange', () => {
emitFinish();
});
optionalWindowDocument.addEventListener('readystatechange', emitFinish);

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.

Wouldn't { once: true } suffice?

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.

Actually never mind, you want to remove it once it is called down there as well.


emitFinish();
}
Expand Down
46 changes: 46 additions & 0 deletions packages/browser/test/tracing/browserTracingIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,52 @@ describe('browserTracingIntegration', () => {

expect(getCurrentScope().getScopeData().transactionName).toBe('test pageload span');
});

it('removes the readystatechange listener once the auto-finish signal is emitted', () => {
const addEventListenerSpy = vi.spyOn(WINDOW.document!, 'addEventListener');
const removeEventListenerSpy = vi.spyOn(WINDOW.document!, 'removeEventListener');

const client = new BrowserClient(
getDefaultBrowserClientOptions({
tracesSampleRate: 1,
integrations: [browserTracingIntegration({ instrumentPageLoad: false })],
}),
);
setCurrentClient(client);
client.init();

// The document is already loaded (readyState 'complete') in the test environment, so the auto-finish signal is
// emitted synchronously and the listener must be cleaned up right away instead of leaking with the idle span.
startBrowserTracingPageLoadSpan(client, { name: 'test span' });

const addedListener = addEventListenerSpy.mock.calls.find(([type]) => type === 'readystatechange')?.[1];
expect(addedListener).toBeDefined();
expect(removeEventListenerSpy).toHaveBeenCalledWith('readystatechange', addedListener);
});

it('removes the readystatechange listener when the document finishes loading later', () => {
const readyStateSpy = vi.spyOn(WINDOW.document!, 'readyState', 'get').mockReturnValue('loading');
const removeEventListenerSpy = vi.spyOn(WINDOW.document!, 'removeEventListener');

const client = new BrowserClient(
getDefaultBrowserClientOptions({
tracesSampleRate: 1,
integrations: [browserTracingIntegration({ instrumentPageLoad: false })],
}),
);
setCurrentClient(client);
client.init();

startBrowserTracingPageLoadSpan(client, { name: 'test span' });

// While loading, no auto-finish signal is emitted yet, so the listener is still attached.
expect(removeEventListenerSpy).not.toHaveBeenCalledWith('readystatechange', expect.any(Function));

readyStateSpy.mockReturnValue('complete');
WINDOW.document!.dispatchEvent(new dom.window.Event('readystatechange'));

expect(removeEventListenerSpy).toHaveBeenCalledWith('readystatechange', expect.any(Function));
});
});

it('sets source to "custom" if name is changed in beforeStartSpan', () => {
Expand Down
Loading