Skip to content

Commit

Permalink
lib: settle signals when abortcontroller is GCed
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Nov 22, 2024
1 parent 5ba3b54 commit 2423237
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ function lazyMessageChannel() {
}

const clearTimeoutRegistry = new SafeFinalizationRegistry(clearTimeout);
const abortControllerSignalFinalizer = new SafeFinalizationRegistry(({ signalWeakRef }) => {
// WIP on https://docs.google.com/document/d/1LvmsBLV85p-PhSGvTH-YwgD6onuhh1VXLg8jPlH32H4/edit?pli=1&tab=t.0#heading=h.7ut6obnf9fz0
// Remove ac.signal from composed signals
// If not source signal remains in the composed signal, let it be GCed
console.log('ac was collected', signalWeakRef.deref());

Check failure on line 91 in lib/internal/abort_controller.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'console'. Use `const console = require('internal/console/global');` instead of the global

Check failure on line 91 in lib/internal/abort_controller.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'console' is not defined

const signal = signalWeakRef.deref();
if (signal === undefined) {
return;
}

signal[kDependantSignals]?.forEach((ref) => {
const dependantSignal = ref.deref();

if (dependantSignal === undefined) {
return;
}

dependantSignal[kSourceSignals]?.delete(signalWeakRef);

if (dependantSignal[kSourceSignals]?.size === 0) {
gcPersistentSignals.delete(dependantSignal);
}
});
});

const dependantSignalsCleanupRegistry = new SafeFinalizationRegistry((signalWeakRef) => {
const signal = signalWeakRef.deref();
if (signal === undefined) {
Expand Down Expand Up @@ -434,6 +460,9 @@ class AbortController {
*/
get signal() {
this.#signal ??= new AbortSignal(kDontThrowSymbol);

abortControllerSignalFinalizer.register(this, { signal: new SafeWeakRef(this.#signal) });

return this.#signal;
}

Expand Down

0 comments on commit 2423237

Please sign in to comment.