Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs, test_runner, timers: Use kResistStopPropagation where applicable #48521

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,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 @@ -2439,7 +2440,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 @@ -33,6 +33,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 @@ -101,17 +103,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 @@ -162,7 +166,7 @@ class MockTimers {
return reject(err);
}

if (options.signal?.aborted) {
if (options.signal.aborted) {
return reject(abortIt(options.signal));
}
}
Expand All @@ -176,10 +180,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
7 changes: 6 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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 @@ -442,7 +444,10 @@ 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
10 changes: 8 additions & 2 deletions 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 @@ -261,12 +262,17 @@ class Test extends AsyncResource {
fn = noop;
}


this.#abortController = new AbortController();
this.#outerSignal = signal;
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