Skip to content

Commit

Permalink
lib: settle signals when controller's signal is GCed
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Nov 26, 2024
1 parent 4f62ab5 commit 86f7b92
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,25 @@ const dependantSignalsCleanupRegistry = new SafeFinalizationRegistry((signalWeak
}
});
});

const gcPersistentSignals = new SafeSet();

const finalizer = new SafeFinalizationRegistry(({ sourceSignalRef, composedSignalRef }) => {
// TODO: remove ref from source signal
// TODO: remove composed signal from gcPersistentSignals
const composedSignal = composedSignalRef.deref();
if (composedSignal !== undefined) {
composedSignal[kSourceSignals].delete(sourceSignalRef);
gcPersistentSignals.delete(composedSignal);
}

// TODO: remove ref from dependant signal
const sourceSignal = sourceSignalRef.deref();
if (sourceSignal !== undefined) {
sourceSignal[kDependantSignals].delete(composedSignalRef);
}
});

const kAborted = Symbol('kAborted');
const kReason = Symbol('kReason');
const kCloneData = Symbol('kCloneData');
Expand Down Expand Up @@ -258,6 +275,9 @@ class AbortSignal extends EventTarget {
resultSignal[kSourceSignals].add(signalWeakRef);
signal[kDependantSignals].add(resultSignalWeakRef);
dependantSignalsCleanupRegistry.register(resultSignal, signalWeakRef);
// when the source signal - coming from the controller - is gced, we need to remove it from the dependant

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

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Comments should not begin with a lowercase character
// signals of the composite signal
finalizer.register(signal, { sourceSignalRef: signalWeakRef, composedSignalRef: resultSignalWeakRef});

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

View workflow job for this annotation

GitHub Actions / lint-js-and-md

A space is required before '}'
} else if (!signal[kSourceSignals]) {
continue;
} else {
Expand Down Expand Up @@ -293,6 +313,7 @@ class AbortSignal extends EventTarget {
// listener, then we don't want it to be gc'd while the listener
// is attached and the timer still hasn't fired. So, we retain a
// strong ref that is held for as long as the listener is registered.

gcPersistentSignals.add(this);
}
}
Expand Down Expand Up @@ -434,6 +455,7 @@ class AbortController {
*/
get signal() {
this.#signal ??= new AbortSignal(kDontThrowSymbol);

return this.#signal;
}

Expand Down

0 comments on commit 86f7b92

Please sign in to comment.