Skip to content

Commit

Permalink
lib: propagate aborted state to dependent signals before firing events
Browse files Browse the repository at this point in the history
  • Loading branch information
jazelly committed Sep 8, 2024
1 parent 67357ba commit d1719ac
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
43 changes: 39 additions & 4 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
// in https://github.com/mysticatea/abort-controller (MIT license)

const {
ArrayPrototypePush,
ObjectAssign,
ObjectDefineProperties,
ObjectDefineProperty,
PromiseResolve,
SafeFinalizationRegistry,
SafeSet,
SetPrototypeForEach,
Symbol,
SymbolToStringTag,
WeakRef,
Expand Down Expand Up @@ -372,18 +374,51 @@ ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {

defineEventHandler(AbortSignal.prototype, 'abort');

// https://dom.spec.whatwg.org/#dom-abortsignal-abort
function abortSignal(signal, reason) {
// 1. If signal is aborted, then return.
// if (signal[kReason]) signal[kAborted] == true;
if (signal[kAborted]) return;

// 2. Set signal's abort reason to reason if it is given;
// otherwise to a new "AbortError" DOMException.
signal[kAborted] = true;
signal[kReason] = reason;
// 3. Let dependentSignalsToAbort be a new list.
const dependentSignalsToAbort = [];
// 4. For each dependentSignal of signal's dependent signals:
if (signal[kDependantSignals]) {
SetPrototypeForEach(signal[kDependantSignals], (s) => {
const dependentSignal = s.deref();
// 1. If dependentSignal is not aborted, then:
if (dependentSignal && !dependentSignal[kAborted]) {
// 1. Set dependentSignal's abort reason to signal's abort reason.
dependentSignal[kReason] = reason;
dependentSignal[kAborted] = true;
// 2. Append dependentSignal to dependentSignalsToAbort.
ArrayPrototypePush(dependentSignalsToAbort, dependentSignal);
}
});
}
// 5. Run the abort steps for signal
runAbort(signal);
// 6. For each dependentSignal of dependentSignalsToAbort,
// run the abort steps for dependentSignal.
for (let i = 0; i < dependentSignalsToAbort.length; i++) {
const dependentSignal = dependentSignalsToAbort[i];
runAbort(dependentSignal);
};
}

// To run the abort steps for an AbortSignal signal
function runAbort(signal) {
// TODO: 1. For each algorithm of signal's abort algorithms: run algorithm.
// TODO: 2. Empty signal's abort algorithms.
// 3. Fire an event named abort at signal.
const event = new Event('abort', {
[kTrustEvent]: true,
});
signal.dispatchEvent(event);
signal[kDependantSignals]?.forEach((s) => {
const signalRef = s.deref();
if (signalRef) abortSignal(signalRef, reason);
});
}

class AbortController {
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-abortsignal-any.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,43 @@ describe('AbortSignal.any()', { concurrency: !process.env.TEST_PARALLEL }, () =>
controller.abort();
assert.strictEqual(result, 1);
});

it('marks dependent signals aborted before abort events fire', () => {
const controller = new AbortController();
const signal1 = AbortSignal.any([controller.signal]);
const signal2 = AbortSignal.any([signal1]);
let eventFired = false;

controller.signal.addEventListener('abort', () => {
const signal3 = AbortSignal.any([signal2]);
assert(controller.signal.aborted);
assert(signal1.aborted);
assert(signal2.aborted);
assert(signal3.aborted);
eventFired = true;
});

controller.abort();
assert(eventFired, 'event fired');
});

it('aborts dependent signals correctly for reentrant aborts', () => {
const controller1 = new AbortController();
const controller2 = new AbortController();
const signal = AbortSignal.any([controller1.signal, controller2.signal]);
let count = 0;

controller1.signal.addEventListener('abort', () => {
controller2.abort('reason 2');
});

signal.addEventListener('abort', () => {
count++;
});

controller1.abort('reason 1');
assert.strictEqual(count, 1);
assert(signal.aborted);
assert.strictEqual(signal.reason, 'reason 1');
});
});

0 comments on commit d1719ac

Please sign in to comment.