Skip to content

Commit a82ec9a

Browse files
tsctxcrysmags
authored andcommitted
fix: call explicitly unregister (nodejs#2534)
1 parent 5ffc7cd commit a82ec9a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,18 @@ class Request {
371371
const abort = function () {
372372
const ac = acRef.deref()
373373
if (ac !== undefined) {
374+
// Currently, there is a problem with FinalizationRegistry.
375+
// https://github.com/nodejs/node/issues/49344
376+
// https://github.com/nodejs/node/issues/47748
377+
// In the case of abort, the first step is to unregister from it.
378+
// If the controller can refer to it, it is still registered.
379+
// It will be removed in the future.
380+
requestFinalizer.unregister(abort)
381+
382+
// Unsubscribe a listener.
383+
// FinalizationRegistry will no longer be called, so this must be done.
384+
this.removeEventListener('abort', abort)
385+
374386
ac.abort(this.reason)
375387
}
376388
}
@@ -388,7 +400,11 @@ class Request {
388400
} catch {}
389401

390402
util.addAbortListener(signal, abort)
391-
requestFinalizer.register(ac, { signal, abort })
403+
// The third argument must be a registry key to be unregistered.
404+
// Without it, you cannot unregister.
405+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
406+
// abort is used as the unregister key. (because it is unique)
407+
requestFinalizer.register(ac, { signal, abort }, abort)
392408
}
393409
}
394410

0 commit comments

Comments
 (0)