-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(v10/browser): Release the XHR virtualError once the request completed
#24307
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
+102
−7
Merged
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
94 changes: 94 additions & 0 deletions
94
packages/browser-utils/test/instrument/xhrRetention.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { setFlagsFromString } from 'node:v8'; | ||
| import { runInNewContext } from 'node:vm'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { instrumentXHR } from '../../src/instrument/xhr'; | ||
| import { WINDOW } from '../../src/types'; | ||
|
|
||
| // Own file on purpose: instrumentation handlers live in a module-level registry that is never torn | ||
| // down, so a handler from another test would retain the requests this one needs to see collected. | ||
|
|
||
| const win = WINDOW as typeof WINDOW & { XMLHttpRequest?: typeof XMLHttpRequest }; | ||
| const originalXMLHttpRequest = win.XMLHttpRequest; | ||
|
|
||
| function collectGarbage(): void { | ||
| setFlagsFromString('--expose-gc'); | ||
| const gc = runInNewContext('gc') as () => void; | ||
| setFlagsFromString('--no-expose-gc'); | ||
| gc(); | ||
| gc(); | ||
| } | ||
|
|
||
| class MockXMLHttpRequest { | ||
| public readyState: number = 0; | ||
| public status: number = 200; | ||
| private _listeners: Array<() => void> = []; | ||
|
|
||
| public addEventListener(_type: string, listener: () => void): void { | ||
| this._listeners.push(listener); | ||
| } | ||
|
|
||
| public removeEventListener(_type: string, listener: () => void): void { | ||
| this._listeners = this._listeners.filter(registered => registered !== listener); | ||
| } | ||
|
|
||
| public dispatch(): void { | ||
| // the SDK detaches its own listener while it runs, so iterate over a copy | ||
| for (const listener of this._listeners.slice()) { | ||
| // the browser calls listeners with the request as `this`, which is what puts the request into | ||
| // the stack frames of anything the listener calls | ||
| listener.call(this); | ||
| } | ||
| } | ||
|
|
||
| public open(_method: string, _url: string): void {} | ||
| public send(): void {} | ||
| public setRequestHeader(_header: string, _value: string): void {} | ||
| } | ||
|
|
||
| describe('instrumentXHR memory retention', () => { | ||
| afterEach(() => { | ||
| win.XMLHttpRequest = originalXMLHttpRequest; | ||
| }); | ||
|
|
||
| it('does not retain completed requests that were chained from readystatechange', async () => { | ||
| win.XMLHttpRequest = MockXMLHttpRequest as unknown as typeof XMLHttpRequest; | ||
| instrumentXHR(); | ||
|
|
||
| let firstRequest: WeakRef<MockXMLHttpRequest> | undefined; | ||
| // the request that never finishes stands in for the in-flight one, which the browser roots as a | ||
| // pending activity | ||
| let inFlightRequest: MockXMLHttpRequest | undefined; | ||
|
|
||
| const openRequest = (remaining: number): void => { | ||
| const xhr = new MockXMLHttpRequest(); | ||
| xhr.open('GET', 'http://example.com'); | ||
|
|
||
| if (!firstRequest) { | ||
| firstRequest = new WeakRef(xhr); | ||
| } | ||
|
|
||
| if (remaining === 0) { | ||
| inFlightRequest = xhr; | ||
| return; | ||
| } | ||
|
|
||
| xhr.addEventListener('readystatechange', function (this: MockXMLHttpRequest) { | ||
| if (this.readyState === 4) { | ||
| openRequest(remaining - 1); | ||
| } | ||
| }); | ||
|
|
||
| xhr.readyState = 4; | ||
| xhr.dispatch(); | ||
| }; | ||
|
|
||
| openRequest(4); | ||
|
|
||
| // a WeakRef created in the current job is never cleared during that job | ||
| await new Promise(resolve => setTimeout(resolve, 0)); | ||
| collectGarbage(); | ||
|
|
||
| expect(inFlightRequest).toBeDefined(); | ||
| expect(firstRequest?.deref()).toBeUndefined(); | ||
| }); | ||
| }); | ||
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.
Retention test misses proxy leak path
Medium Severity
The new regression test only drives the
addEventListenerbranch, whereremoveEventListeneralready drops the handler that closed overvirtualError. The leak this fix targets is theonreadystatechangeproxy keeping that closure—and the prior request—alive. This test can pass withoutvirtualError = undefinedand may not lock in the fix. Flagged because the Testing Conventions in the review rules require a fix PR's test to fail without the change and pass with it.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 50233f0. Configure here.