Skip to content

lib: reuse default DOMException in AbortController #47908

New issue

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

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

Already on GitHub? # to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions benchmark/events/abortcontroller-abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common.js');

const bench = common.createBenchmark(main, {
n: [1e6],
});

function main({ n }) {
bench.start();
for (let i = 0; i < n; i++) {
const ac = new AbortController();
ac.signal.addEventListener('abort', () => {});
ac.abort();
}
bench.end(n);
}
28 changes: 25 additions & 3 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// in https://github.com/mysticatea/abort-controller (MIT license)

const {
ErrorCaptureStackTrace,
ObjectAssign,
ObjectDefineProperties,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -63,6 +64,7 @@ const {

let _MessageChannel;
let makeTransferable;
let defaultDOMException;

// Loading the MessageChannel and makeTransferable have to be done lazily
// because otherwise we'll end up with a require cycle that ends up with
Expand All @@ -79,6 +81,19 @@ function lazyMakeTransferable(obj) {
return makeTransferable(obj);
}

function lazyDOMException() {
if (defaultDOMException) {
return defaultDOMException;
}

defaultDOMException = new DOMException('This operation was aborted', 'AbortError');

// Avoid V8 leak
// eslint-disable-next-line no-unused-vars
const dummy = defaultDOMException.stack;
return defaultDOMException;
}

const clearTimeoutRegistry = new SafeFinalizationRegistry(clearTimeout);
const timeOutSignals = new SafeSet();

Expand Down Expand Up @@ -166,8 +181,11 @@ class AbortSignal extends EventTarget {
* @param {any} [reason]
* @returns {AbortSignal}
*/
static abort(
reason = new DOMException('This operation was aborted', 'AbortError')) {
static abort(reason = undefined) {
if (reason === undefined) {
reason = lazyDOMException();
ErrorCaptureStackTrace(reason);
}
return createAbortSignal({ aborted: true, reason });
}

Expand Down Expand Up @@ -328,7 +346,11 @@ class AbortController {
/**
* @param {any} [reason]
*/
abort(reason = new DOMException('This operation was aborted', 'AbortError')) {
abort(reason) {
if (reason === undefined) {
reason = lazyDOMException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are not creating a new error everytime abort is called, 2 different usages in the code throwing this error, might have a different stack trace. Is this compliant with the spec?

Copy link
Member Author

@debadree25 debadree25 May 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah good point, not sure about that, one thing we could do maybe is move this lazy loading inside the functions themselves. that should ensure correct trace?

ErrorCaptureStackTrace(reason);
}
abortSignal(this.#signal ??= createAbortSignal(), reason);
}

Expand Down