Skip to content

Commit

Permalink
fs: use kResistStopPropagation
Browse files Browse the repository at this point in the history
PR-URL: #48521
Backport-PR-URL: #49618
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
  • Loading branch information
atlowChemi authored and targos committed Oct 28, 2023
1 parent f0536f8 commit 29dc4b6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ let ReadStream;
let WriteStream;
let rimraf;
let rimrafSync;
let kResistStopPropagation;

// These have to be separate because of how graceful-fs happens to do it's
// monkeypatching.
Expand Down Expand Up @@ -2435,7 +2436,8 @@ function watch(filename, options, listener) {
process.nextTick(() => watcher.close());
} else {
const listener = () => watcher.close();
options.signal.addEventListener('abort', listener);
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
options.signal.addEventListener('abort', listener, { __proto__: null, [kResistStopPropagation]: true });
watcher.once('close', () => {
options.signal.removeEventListener('abort', listener);
});
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function lazyLoadFsSync() {
internalSync ??= require('fs');
return internalSync;
}
let kResistStopPropagation;

async function traverse(dir, files = new SafeMap(), symbolicLinks = new SafeSet()) {
const { opendir } = lazyLoadFsPromises();
Expand Down Expand Up @@ -265,7 +266,8 @@ class FSWatcher extends EventEmitter {
} : (resolve, reject) => {
const onAbort = () => reject(new AbortError(undefined, { cause: signal.reason }));
if (signal.aborted) return onAbort();
signal.addEventListener('abort', onAbort, { __proto__: null, once: true });
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', onAbort, { __proto__: null, once: true, [kResistStopPropagation]: true });
this.once('change', (eventType, filename) => {
signal.removeEventListener('abort', onAbort);
resolve({ __proto__: null, value: { eventType, filename } });
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ ObjectDefineProperty(FSEvent.prototype, 'owner', {
set(v) { return this[owner_symbol] = v; },
});

let kResistStopPropagation;

async function* watch(filename, options = kEmptyObject) {
const path = toNamespacedPath(getValidatedPath(filename));
validateObject(options, 'options');
Expand Down Expand Up @@ -330,7 +332,10 @@ async function* watch(filename, options = kEmptyObject) {
};

try {
signal?.addEventListener('abort', oncancel, { once: true });
if (signal) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', oncancel, { __proto__: null, once: true, [kResistStopPropagation]: true });
}
handle.onchange = (status, eventType, filename) => {
if (status < 0) {
const error = uvException({
Expand Down
22 changes: 15 additions & 7 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const nodeTimers = require('timers');
const nodeTimersPromises = require('timers/promises');
const EventEmitter = require('events');

let kResistStopPropagation;

function compareTimersLists(a, b) {
return (a.runAt - b.runAt) || (a.id - b.id);
}
Expand Down Expand Up @@ -100,17 +102,19 @@ class MockTimers {
if (options?.signal) {
validateAbortSignal(options.signal, 'options.signal');

if (options.signal?.aborted) {
if (options.signal.aborted) {
throw abortIt(options.signal);
}

const onAbort = (reason) => {
emitter.emit('data', { __proto__: null, aborted: true, reason });
};

options.signal?.addEventListener('abort', onAbort, {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
options.signal.addEventListener('abort', onAbort, {
__proto__: null,
once: true,
[kResistStopPropagation]: true,
});
}

Expand Down Expand Up @@ -161,7 +165,7 @@ class MockTimers {
return reject(err);
}

if (options.signal?.aborted) {
if (options.signal.aborted) {
return reject(abortIt(options.signal));
}
}
Expand All @@ -175,10 +179,14 @@ class MockTimers {
return resolve(result || id);
}, ms);

options?.signal?.addEventListener('abort', onabort, {
__proto__: null,
once: true,
});
if (options?.signal) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
options.signal.addEventListener('abort', onabort, {
__proto__: null,
once: true,
[kResistStopPropagation]: true,
});
}
});
}

Expand Down
11 changes: 10 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const kDiagnosticsFilterArgs = ['tests', 'suites', 'pass', 'fail', 'cancelled',
const kCanceledTests = new SafeSet()
.add(kCancelledByParent).add(kAborted).add(kTestTimeoutFailure);

let kResistStopPropagation;

// TODO(cjihrig): Replace this with recursive readdir once it lands.
function processPath(path, testFiles, options) {
const stats = statSync(path);
Expand Down Expand Up @@ -441,7 +443,14 @@ function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) {
triggerUncaughtException(error, true /* fromPromise */);
}));
});
signal?.addEventListener('abort', () => root.postRun(), { __proto__: null, once: true });
if (signal) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener(
'abort',
() => root.postRun(),
{ __proto__: null, once: true, [kResistStopPropagation]: true },
);
}

return filesWatcher;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const kUnwrapErrors = new SafeSet()
.add(kTestCodeFailure).add(kHookFailure)
.add('uncaughtException').add('unhandledRejection');
const { testNamePatterns, testOnlyFlag } = parseCommandLine();
let kResistStopPropagation;

function stopTest(timeout, signal) {
if (timeout === kDefaultTimeout) {
Expand Down Expand Up @@ -266,7 +267,15 @@ class Test extends AsyncResource {
this.signal = this.#abortController.signal;

validateAbortSignal(signal, 'options.signal');
this.#outerSignal?.addEventListener('abort', this.#abortHandler);
if (signal) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
}

this.#outerSignal?.addEventListener(
'abort',
this.#abortHandler,
{ __proto__: null, [kResistStopPropagation]: true },
);

this.fn = fn;
this.harness = null; // Configured on the root test by the test harness.
Expand Down
10 changes: 7 additions & 3 deletions lib/timers/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const {
} = require('internal/util');

const kScheduler = Symbol('kScheduler');
let kResistStopPropagation;

function cancelListenerHandler(clear, reject, signal) {
if (!this._destroyed) {
Expand Down Expand Up @@ -81,7 +82,8 @@ function setTimeout(after, value, options = kEmptyObject) {
if (signal) {
oncancel = FunctionPrototypeBind(cancelListenerHandler,
timeout, clearTimeout, reject, signal);
signal.addEventListener('abort', oncancel);
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true });
}
});
return oncancel !== undefined ?
Expand Down Expand Up @@ -123,7 +125,8 @@ function setImmediate(value, options = kEmptyObject) {
oncancel = FunctionPrototypeBind(cancelListenerHandler,
immediate, clearImmediate, reject,
signal);
signal.addEventListener('abort', oncancel);
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true });
}
});
return oncancel !== undefined ?
Expand Down Expand Up @@ -164,7 +167,8 @@ async function* setInterval(after, value, options = kEmptyObject) {
callback = undefined;
}
};
signal.addEventListener('abort', onCancel, { once: true });
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', onCancel, { __proto__: null, once: true, [kResistStopPropagation]: true });
}

while (!signal?.aborted) {
Expand Down

0 comments on commit 29dc4b6

Please sign in to comment.