Skip to content

Commit 544d7c2

Browse files
committed
fix: call explicitly unregister
1 parent 5f4d435 commit 544d7c2

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/compat/dispatcher-weakref.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class CompatFinalizer {
2828
})
2929
}
3030
}
31+
32+
unregister (key) {}
3133
}
3234

3335
module.exports = function () {

lib/fetch/request.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ const { getMaxListeners, setMaxListeners, getEventListeners, defaultMaxListeners
3535
const kAbortController = Symbol('abortController')
3636

3737
const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
38+
// Currently FinalizationRegistry has a problem and will explicitly call unregister.
39+
// https://github.com/nodejs/node/issues/49344
40+
// https://github.com/nodejs/node/issues/47748
41+
// It will be removed in the future.
42+
// Note: The unregister key is abort.
43+
requestFinalizer.unregister(abort)
3844
signal.removeEventListener('abort', abort)
3945
})
4046

@@ -371,6 +377,17 @@ class Request {
371377
const abort = function () {
372378
const ac = acRef.deref()
373379
if (ac !== undefined) {
380+
// Currently, there is a problem with FinalizationRegistry.
381+
// https://github.com/nodejs/node/issues/49344
382+
// https://github.com/nodejs/node/issues/47748
383+
// In the case of abort, the first step is to unregister from it.
384+
// If you can refer to it, it is still registered.
385+
// It will be removed in the future.
386+
requestFinalizer.unregister(abort)
387+
// Unsubscribe a listener.
388+
// FinalizationRegistry will no longer be called, so this must be done.
389+
this.removeEventListener('abort', abort)
390+
374391
ac.abort(this.reason)
375392
}
376393
}
@@ -388,7 +405,11 @@ class Request {
388405
} catch {}
389406

390407
util.addAbortListener(signal, abort)
391-
requestFinalizer.register(ac, { signal, abort })
408+
// The third argument must be a registry key to be unregistered.
409+
// Without it, you cannot unregister.
410+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
411+
// abort is used as the unregister key. ( Because it is unique. )
412+
requestFinalizer.register(ac, { signal, abort }, abort)
392413
}
393414
}
394415

0 commit comments

Comments
 (0)