mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
diagnostics_channel: replace using with try-finally
Signed-off-by: Ayush Chaudhary <ayush23chaudhary@gmail.com> PR-URL: https://github.com/nodejs/node/pull/64251 Fixes: https://github.com/nodejs/node/issues/64230 Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
dd5dfb5250
commit
b1251217f2
1 file changed
+62
-24
+62
-24
@@ -20,6 +20,7 @@ const {
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
aggregateTwoErrors,
|
||||
codes: {
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
},
|
||||
@@ -93,12 +94,25 @@ 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) {
|
||||
// eslint-disable-next-line no-restricted-globals
|
||||
using stack = new DisposableStack();
|
||||
// TODO: use native DisposableStack once supported in core
|
||||
const stack = [];
|
||||
|
||||
// Enter stores using withScope
|
||||
if (activeChannel._stores) {
|
||||
@@ -118,7 +132,11 @@ class RunStoresScope {
|
||||
}
|
||||
}
|
||||
|
||||
stack.use(store.withScope(newContext));
|
||||
try {
|
||||
ArrayPrototypePush(stack, store.withScope(newContext));
|
||||
} catch (err) {
|
||||
disposeStoreScopes(stack, err, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,11 +144,17 @@ class RunStoresScope {
|
||||
activeChannel.publish(data);
|
||||
|
||||
// Transfer ownership of the stack
|
||||
this.#stack = stack.move();
|
||||
// TODO: Restore stack.move() when DisposableStack is restored in core to avoid broken behavior.
|
||||
this.#stack = stack;
|
||||
}
|
||||
|
||||
[SymbolDispose]() {
|
||||
this.#stack[SymbolDispose]();
|
||||
if (this.#stack !== undefined) {
|
||||
const stack = this.#stack;
|
||||
this.#stack = undefined;
|
||||
|
||||
disposeStoreScopes(stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,9 +230,12 @@ class ActiveChannel {
|
||||
}
|
||||
|
||||
runStores(data, fn, thisArg, ...args) {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
using scope = this.withStoreScope(data);
|
||||
return ReflectApply(fn, thisArg, args);
|
||||
const scope = this.withStoreScope(data);
|
||||
try {
|
||||
return ReflectApply(fn, thisArg, args);
|
||||
} finally {
|
||||
scope[SymbolDispose]();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -404,9 +431,12 @@ class BoundedChannel {
|
||||
|
||||
run(context, fn, thisArg, ...args) {
|
||||
context ??= {};
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
using scope = this.withScope(context);
|
||||
return ReflectApply(fn, thisArg, args);
|
||||
const scope = this.withScope(context);
|
||||
try {
|
||||
return ReflectApply(fn, thisArg, args);
|
||||
} finally {
|
||||
scope[SymbolDispose]();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,9 +562,8 @@ 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;
|
||||
@@ -543,6 +572,8 @@ class TracingChannel {
|
||||
context.error = err;
|
||||
error.publish(context);
|
||||
throw err;
|
||||
} finally {
|
||||
scope[SymbolDispose]();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -566,9 +597,9 @@ class TracingChannel {
|
||||
context.error = err;
|
||||
error.publish(context);
|
||||
// Use continuation window for asyncStart/asyncEnd
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
using scope = continuationWindow.withScope(context);
|
||||
const scope = continuationWindow.withScope(context);
|
||||
// TODO: Is there a way to have asyncEnd _after_ the continuation?
|
||||
scope[SymbolDispose]();
|
||||
}
|
||||
|
||||
function onRejectWithRethrow(err) {
|
||||
@@ -579,14 +610,14 @@ class TracingChannel {
|
||||
function onResolve(result) {
|
||||
context.result = result;
|
||||
// Use continuation window for asyncStart/asyncEnd
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
using scope = continuationWindow.withScope(context);
|
||||
const scope = continuationWindow.withScope(context);
|
||||
// TODO: Is there a way to have asyncEnd _after_ the continuation?
|
||||
scope[SymbolDispose]();
|
||||
return result;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
using scope = this.#callWindow.withScope(context);
|
||||
const 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.
|
||||
@@ -611,6 +642,8 @@ class TracingChannel {
|
||||
context.error = err;
|
||||
error.publish(context);
|
||||
throw err;
|
||||
} finally {
|
||||
scope[SymbolDispose]();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -631,23 +664,28 @@ class TracingChannel {
|
||||
}
|
||||
|
||||
// Use continuation window for asyncStart/asyncEnd around callback
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
using scope = continuationWindow.withScope(context);
|
||||
return ReflectApply(callback, this, arguments);
|
||||
const scope = continuationWindow.withScope(context);
|
||||
try {
|
||||
return ReflectApply(callback, this, arguments);
|
||||
} finally {
|
||||
scope[SymbolDispose]();
|
||||
}
|
||||
}
|
||||
|
||||
const callback = ArrayPrototypeAt(args, position);
|
||||
validateFunction(callback, 'callback');
|
||||
ArrayPrototypeSplice(args, position, 1, wrappedCallback);
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
using scope = this.#callWindow.withScope(context);
|
||||
const scope = this.#callWindow.withScope(context);
|
||||
|
||||
try {
|
||||
return ReflectApply(fn, thisArg, args);
|
||||
} catch (err) {
|
||||
context.error = err;
|
||||
error.publish(context);
|
||||
throw err;
|
||||
} finally {
|
||||
scope[SymbolDispose]();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user