fix: prevent AbortController GC when redirect is 'error' #4750
+197
−3
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.
Summary
Fixes #4627
When
fetch()is called withredirect: 'error', therequestObject(theRequestinstance) could be garbage collected while the fetch was still in progress. TherequestObjectholds an internalAbortControllervia thekAbortControllerproperty. WhenrequestObjectwas GC'd, theAbortControllerwas also collected, triggering aFinalizationRegistrycallback that removed the abort listener from the user's signal. This caused abort signals to have no effect, resulting in connection leaks for SSE streams.Changes
The fix stores a reference to
requestObjectinfetchParams, which stays alive for the entire duration of the fetch operation. This prevents premature garbage collection of therequestObjectand its internalAbortController.requestObjectto thefetching()functionrequestObjectparameter tofetching()function signaturerequestObjectinfetchParamsobjectTest Plan
test/fetch/issue-4627.jsthat requires--expose-gcflagredirect: 'error', triggers GC, aborts, and verifies no data is received after abortabort should work with redirect: error✅abort should work without redirect: error (control test)✅