Skip to content

Commit ed686fc

Browse files
authored
fix: correct firing order of abort events (#3169)
1 parent 3f927b8 commit ed686fc

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lib/web/fetch/request.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
3838
signal.removeEventListener('abort', abort)
3939
})
4040

41+
const dependentControllerMap = new WeakMap()
42+
4143
function buildAbort (acRef) {
4244
return abort
4345

@@ -57,6 +59,21 @@ function buildAbort (acRef) {
5759
this.removeEventListener('abort', abort)
5860

5961
ac.abort(this.reason)
62+
63+
const controllerList = dependentControllerMap.get(ac.signal)
64+
65+
if (controllerList !== undefined) {
66+
if (controllerList.size !== 0) {
67+
for (const ref of controllerList) {
68+
const ctrl = ref.deref()
69+
if (ctrl !== undefined) {
70+
ctrl.abort(this.reason)
71+
}
72+
}
73+
controllerList.clear()
74+
}
75+
dependentControllerMap.delete(ac.signal)
76+
}
6077
}
6178
}
6279
}
@@ -754,11 +771,16 @@ class Request {
754771
if (this.signal.aborted) {
755772
ac.abort(this.signal.reason)
756773
} else {
774+
let list = dependentControllerMap.get(this.signal)
775+
if (list === undefined) {
776+
list = new Set()
777+
dependentControllerMap.set(this.signal, list)
778+
}
779+
const acRef = new WeakRef(ac)
780+
list.add(acRef)
757781
util.addAbortListener(
758-
this.signal,
759-
() => {
760-
ac.abort(this.signal.reason)
761-
}
782+
ac.signal,
783+
buildAbort(acRef)
762784
)
763785
}
764786

test/wpt/status/fetch.status.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
"api": {
33
"abort": {
44
"general.any.js": {
5-
"note": "TODO(@KhafraDev): Clone aborts with original controller can probably be fixed",
65
"fail": [
76
"Already aborted signal rejects immediately",
87
"Underlying connection is closed when aborting after receiving response - no-cors",
98
"Stream errors once aborted. Underlying connection closed.",
10-
"Readable stream synchronously cancels with AbortError if aborted before reading",
11-
"Clone aborts with original controller"
9+
"Readable stream synchronously cancels with AbortError if aborted before reading"
1210
]
1311
},
1412
"cache.https.any.js": {

0 commit comments

Comments
 (0)