diff --git a/lib/diagnostics_channel.js b/lib/diagnostics_channel.js index bb0c7d66050..dd600d804fd 100644 --- a/lib/diagnostics_channel.js +++ b/lib/diagnostics_channel.js @@ -20,7 +20,6 @@ const { } = primordials; const { - aggregateTwoErrors, codes: { ERR_INVALID_ARG_TYPE, }, @@ -94,25 +93,12 @@ function maybeMarkInactive(channel) { } } -function disposeStoreScopes(stack, error, hasError = false) { - for (let i = stack.length - 1; i >= 0; i--) { - try { - stack[i][SymbolDispose](); - } catch (err) { - error = hasError ? aggregateTwoErrors(err, error) : err; - hasError = true; - } - } - - if (hasError) throw error; -} - class RunStoresScope { #stack; constructor(activeChannel, data) { - // TODO: use native DisposableStack once supported in core - const stack = []; + // eslint-disable-next-line no-restricted-globals + using stack = new DisposableStack(); // Enter stores using withScope if (activeChannel._stores) { @@ -132,11 +118,7 @@ class RunStoresScope { } } - try { - ArrayPrototypePush(stack, store.withScope(newContext)); - } catch (err) { - disposeStoreScopes(stack, err, true); - } + stack.use(store.withScope(newContext)); } } @@ -144,17 +126,11 @@ class RunStoresScope { activeChannel.publish(data); // Transfer ownership of the stack - // TODO: Restore stack.move() when DisposableStack is restored in core to avoid broken behavior. - this.#stack = stack; + this.#stack = stack.move(); } [SymbolDispose]() { - if (this.#stack !== undefined) { - const stack = this.#stack; - this.#stack = undefined; - - disposeStoreScopes(stack); - } + this.#stack[SymbolDispose](); } } @@ -230,12 +206,9 @@ class ActiveChannel { } runStores(data, fn, thisArg, ...args) { - const scope = this.withStoreScope(data); - try { - return ReflectApply(fn, thisArg, args); - } finally { - scope[SymbolDispose](); - } + // eslint-disable-next-line no-unused-vars + using scope = this.withStoreScope(data); + return ReflectApply(fn, thisArg, args); } } @@ -431,12 +404,9 @@ class BoundedChannel { run(context, fn, thisArg, ...args) { context ??= {}; - const scope = this.withScope(context); - try { - return ReflectApply(fn, thisArg, args); - } finally { - scope[SymbolDispose](); - } + // eslint-disable-next-line no-unused-vars + using scope = this.withScope(context); + return ReflectApply(fn, thisArg, args); } } @@ -562,8 +532,9 @@ class TracingChannel { } const { error } = this; - const scope = this.#callWindow.withScope(context); + // eslint-disable-next-line no-unused-vars + using scope = this.#callWindow.withScope(context); try { const result = ReflectApply(fn, thisArg, args); context.result = result; @@ -572,8 +543,6 @@ class TracingChannel { context.error = err; error.publish(context); throw err; - } finally { - scope[SymbolDispose](); } } @@ -597,9 +566,9 @@ class TracingChannel { context.error = err; error.publish(context); // Use continuation window for asyncStart/asyncEnd - const scope = continuationWindow.withScope(context); + // eslint-disable-next-line no-unused-vars + using scope = continuationWindow.withScope(context); // TODO: Is there a way to have asyncEnd _after_ the continuation? - scope[SymbolDispose](); } function onRejectWithRethrow(err) { @@ -610,14 +579,14 @@ class TracingChannel { function onResolve(result) { context.result = result; // Use continuation window for asyncStart/asyncEnd - const scope = continuationWindow.withScope(context); + // eslint-disable-next-line no-unused-vars + using scope = continuationWindow.withScope(context); // TODO: Is there a way to have asyncEnd _after_ the continuation? - scope[SymbolDispose](); return result; } - const scope = this.#callWindow.withScope(context); - + // eslint-disable-next-line no-unused-vars + using scope = this.#callWindow.withScope(context); try { const result = ReflectApply(fn, thisArg, args); // If the return value is not a thenable, return it directly with a warning. @@ -642,8 +611,6 @@ class TracingChannel { context.error = err; error.publish(context); throw err; - } finally { - scope[SymbolDispose](); } } @@ -668,28 +635,23 @@ class TracingChannel { } // Use continuation window for asyncStart/asyncEnd around callback - const scope = continuationWindow.withScope(context); - try { - return ReflectApply(callback, this, arguments); - } finally { - scope[SymbolDispose](); - } + // eslint-disable-next-line no-unused-vars + using scope = continuationWindow.withScope(context); + return ReflectApply(callback, this, arguments); } const callback = ArrayPrototypeAt(args, position); validateFunction(callback, 'callback'); ArrayPrototypeSplice(args, position, 1, wrappedCallback); - const scope = this.#callWindow.withScope(context); - + // eslint-disable-next-line no-unused-vars + using scope = this.#callWindow.withScope(context); try { return ReflectApply(fn, thisArg, args); } catch (err) { context.error = err; error.publish(context); throw err; - } finally { - scope[SymbolDispose](); } } }