GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — diagnostics_channel: replace using with try-finally · nodejs/node@50b7999

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • diagnostics_channel.js
  • test/parallel
    • test-diagnostics-channel-no-erm.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/diagnostics_channel.js‎

Copy file name to clipboardExpand all lines: lib/diagnostics_channel.js
+46-24Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class RunStoresScope {
8888
#stack;
8989

9090
constructor(activeChannel, data) {
91-
// eslint-disable-next-line no-restricted-globals
92-
using stack = new DisposableStack();
91+
// TODO: use native DisposableStack once supported in core
92+
const stack = [];
9393

9494
// Enter stores using withScope
9595
if (activeChannel._stores) {
@@ -109,19 +109,27 @@ class RunStoresScope {
109109
}
110110
}
111111

112-
stack.use(store.withScope(newContext));
112+
ArrayPrototypePush(stack, store.withScope(newContext));
113113
}
114114
}
115115

116116
// Publish data
117117
activeChannel.publish(data);
118118

119119
// Transfer ownership of the stack
120-
this.#stack = stack.move();
120+
// TODO: Restore stack.move() when DisposableStack is restored in core to avoid broken behavior.
121+
this.#stack = stack;
121122
}
122123

123124
[SymbolDispose]() {
124-
this.#stack[SymbolDispose]();
125+
if (this.#stack !== undefined) {
126+
const stack = this.#stack;
127+
this.#stack = undefined;
128+
129+
for (let i = stack.length - 1; i >= 0; i--) {
130+
stack[i][SymbolDispose]();
131+
}
132+
}
125133
}
126134
}
127135

@@ -197,9 +205,12 @@ class ActiveChannel {
197205
}
198206

199207
runStores(data, fn, thisArg, ...args) {
200-
// eslint-disable-next-line no-unused-vars
201-
using scope = this.withStoreScope(data);
202-
return ReflectApply(fn, thisArg, args);
208+
const scope = this.withStoreScope(data);
209+
try {
210+
return ReflectApply(fn, thisArg, args);
211+
} finally {
212+
scope[SymbolDispose]();
213+
}
203214
}
204215
}
205216

@@ -396,9 +407,12 @@ class BoundedChannel {
396407

397408
run(context, fn, thisArg, ...args) {
398409
context ??= {};
399-
// eslint-disable-next-line no-unused-vars
400-
using scope = this.withScope(context);
401-
return ReflectApply(fn, thisArg, args);
410+
const scope = this.withScope(context);
411+
try {
412+
return ReflectApply(fn, thisArg, args);
413+
} finally {
414+
scope[SymbolDispose]();
415+
}
402416
}
403417
}
404418

@@ -520,9 +534,8 @@ class TracingChannel {
520534
}
521535

522536
const { error } = this;
537+
const scope = this.#callWindow.withScope(context);
523538

524-
// eslint-disable-next-line no-unused-vars
525-
using scope = this.#callWindow.withScope(context);
526539
try {
527540
const result = ReflectApply(fn, thisArg, args);
528541
context.result = result;
@@ -531,6 +544,8 @@ class TracingChannel {
531544
context.error = err;
532545
error.publish(context);
533546
throw err;
547+
} finally {
548+
scope[SymbolDispose]();
534549
}
535550
}
536551

@@ -550,9 +565,9 @@ class TracingChannel {
550565
context.error = err;
551566
error.publish(context);
552567
// Use continuation window for asyncStart/asyncEnd
553-
// eslint-disable-next-line no-unused-vars
554-
using scope = continuationWindow.withScope(context);
568+
const scope = continuationWindow.withScope(context);
555569
// TODO: Is there a way to have asyncEnd _after_ the continuation?
570+
scope[SymbolDispose]();
556571
}
557572

558573
function onRejectWithRethrow(err) {
@@ -563,14 +578,14 @@ class TracingChannel {
563578
function onResolve(result) {
564579
context.result = result;
565580
// Use continuation window for asyncStart/asyncEnd
566-
// eslint-disable-next-line no-unused-vars
567-
using scope = continuationWindow.withScope(context);
581+
const scope = continuationWindow.withScope(context);
568582
// TODO: Is there a way to have asyncEnd _after_ the continuation?
583+
scope[SymbolDispose]();
569584
return result;
570585
}
571586

572-
// eslint-disable-next-line no-unused-vars
573-
using scope = this.#callWindow.withScope(context);
587+
const scope = this.#callWindow.withScope(context);
588+
574589
try {
575590
const result = ReflectApply(fn, thisArg, args);
576591
// If the return value is not a thenable, return it directly with a warning.
@@ -595,6 +610,8 @@ class TracingChannel {
595610
context.error = err;
596611
error.publish(context);
597612
throw err;
613+
} finally {
614+
scope[SymbolDispose]();
598615
}
599616
}
600617

@@ -615,23 +632,28 @@ class TracingChannel {
615632
}
616633

617634
// Use continuation window for asyncStart/asyncEnd around callback
618-
// eslint-disable-next-line no-unused-vars
619-
using scope = continuationWindow.withScope(context);
620-
return ReflectApply(callback, this, arguments);
635+
const scope = continuationWindow.withScope(context);
636+
try {
637+
return ReflectApply(callback, this, arguments);
638+
} finally {
639+
scope[SymbolDispose]();
640+
}
621641
}
622642

623643
const callback = ArrayPrototypeAt(args, position);
624644
validateFunction(callback, 'callback');
625645
ArrayPrototypeSplice(args, position, 1, wrappedCallback);
626646

627-
// eslint-disable-next-line no-unused-vars
628-
using scope = this.#callWindow.withScope(context);
647+
const scope = this.#callWindow.withScope(context);
648+
629649
try {
630650
return ReflectApply(fn, thisArg, args);
631651
} catch (err) {
632652
context.error = err;
633653
error.publish(context);
634654
throw err;
655+
} finally {
656+
scope[SymbolDispose]();
635657
}
636658
}
637659
}
Collapse file

‎test/parallel/test-diagnostics-channel-no-erm.js‎

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Flags: --no-js-explicit-resource-management
2+
'use strict';
3+
4+
require('../common');
5+
const { tracingChannel } = require('diagnostics_channel');
6+
7+
// This test ensures that using diagnostics_channel does not cause a segfault
8+
// when explicit resource management is disabled in V8.
9+
// Refs: https://github.com/nodejs/node/issues/64230
10+
11+
tracingChannel('foo').traceSync(() => {});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.