fix(fetch): keep cloned request abort linkage alive across GC - #5768
Open
pacocartones wants to merge 1 commit into
Open
fix(fetch): keep cloned request abort linkage alive across GC#5768pacocartones wants to merge 1 commit into
pacocartones wants to merge 1 commit into
Conversation
A cloned (or constructor-copied) Request follows the source request's internal AbortSignal. That linkage was reachable only through weak references: the dependent controller lived in a WeakRef inside dependentControllerMap and in the abort listener closure, and the source request's controller was reachable only while the source request object stayed alive. Once the source request was dropped and a GC ran, the controller chain was collected and aborting the original controller no longer aborted the clone, so the in-flight fetch hung until its timeout. Mirror the constructor's nodejs#1926 strong-reference approach for the follow linkage: keep the dependent controller alive on the clone, and keep the source request's (flattened) controller chain alive for the follower's lifetime. This covers Request.clone(), new Request(request), and arbitrarily deep chains, without reintroducing a FinalizationRegistry on clones and without changing the abort firing order from nodejs#3169. Fixes: nodejs#4068
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5768 +/- ##
==========================================
+ Coverage 93.50% 93.52% +0.02%
==========================================
Files 110 110
Lines 39072 39141 +69
==========================================
+ Hits 36534 36608 +74
+ Misses 2538 2533 -5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4068.
Problem
A cloned
Requestloses abort propagation after garbage collection. The clone-follow wiring (from #3169) made the dependent controller reachable only throughWeakRefs, so GC collects it andacRef.deref()returnsundefined, silently dropping propagation. Deeper: a clone follows the source request's internal signal, whose controller is held only by the sourceRequestobject; when the source is dropped (req = req.clone()), that controller is collected too. Same fornew Request(existingRequest).Confirmed still reproducing at HEAD (undici 8.10.1): the repro
fetchhangs because aborting the original after a GC never rejects the clone.Fix
Mirror the constructor's proven strong-reference strategy (#1926) for the follow linkage, without altering the abort wiring (preserving #3169's firing order) and without adding a
FinalizationRegistryto clones (respecting #4320):clone()keeps the dependent controller alive (clonedRequestObject[kAbortController] = ac).clone()and the constructor keep the flattened source controller chain alive via a newkAbortSourceControllerssymbol, so arbitrarily deep clone chains survive even when every intermediateRequestis collected. Refs release when the follower itself is GC'd (no leak — verified againstlong-lived-abort-controller.js).Test
test/issue-4068.js(run undernode --expose-gc, deterministicforceGc()with 10 passes): bothclone()andnew Request(req)cases — RED time out (hang); GREEN 3/3 pass in ~150ms.fetch/request32,fetch/abort,request-signal,long-lived-abort-controllerall green;npm run lintgreen.