Skip to content

Commit 602c46f

Browse files
theanarkhRafaelGSS
authored andcommitted
lib: add diagnostics channel for process and worker
PR-URL: #44045 Reviewed-By: James M Snell <[email protected]>
1 parent af28901 commit 602c46f

5 files changed

+76
-0
lines changed

doc/api/diagnostics_channel.md

+29
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ Emitted when server receives a request.
428428

429429
Emitted when server sends a response.
430430

431+
#### NET
432+
431433
`net.client.socket`
432434

433435
* `socket` {net.Socket}
@@ -440,13 +442,40 @@ Emitted when a new TCP or pipe client socket is created.
440442

441443
Emitted when a new TCP or pipe connection is received.
442444

445+
#### UDP
446+
443447
`udp.socket`
444448

445449
* `socket` {dgram.Socket}
446450

447451
Emitted when a new UDP socket is created.
448452

453+
#### Process
454+
455+
<!-- YAML
456+
added: REPLACEME
457+
-->
458+
459+
`child_process`
460+
461+
* `process` {ChildProcess}
462+
463+
Emitted when a new process is created.
464+
465+
#### Worker Thread
466+
467+
<!-- YAML
468+
added: REPLACEME
469+
-->
470+
471+
`worker_threads`
472+
473+
* `worker` [`Worker`][]
474+
475+
Emitted when a new thread is created.
476+
449477
[`'uncaughtException'`]: process.md#event-uncaughtexception
478+
[`Worker`]: worker_threads.md#class-worker
450479
[`channel.subscribe(onMessage)`]: #channelsubscribeonmessage
451480
[`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname
452481
[`diagnostics_channel.subscribe(name, onMessage)`]: #diagnostics_channelsubscribename-onmessage

lib/internal/child_process.js

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const { convertToValidSignal, deprecate } = require('internal/util');
5959
const { isArrayBufferView } = require('internal/util/types');
6060
const spawn_sync = internalBinding('spawn_sync');
6161
const { kStateSymbol } = require('internal/dgram');
62+
const dc = require('diagnostics_channel');
63+
const childProcessChannel = dc.channel('child_process');
6264

6365
const {
6466
UV_EACCES,
@@ -301,6 +303,11 @@ function ChildProcess() {
301303

302304
maybeClose(this);
303305
};
306+
if (childProcessChannel.hasSubscribers) {
307+
childProcessChannel.publish({
308+
process: this,
309+
});
310+
}
304311
}
305312
ObjectSetPrototypeOf(ChildProcess.prototype, EventEmitter.prototype);
306313
ObjectSetPrototypeOf(ChildProcess, EventEmitter);

lib/internal/worker.js

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
8787
debug = fn;
8888
});
8989

90+
const dc = require('diagnostics_channel');
91+
const workerThreadsChannel = dc.channel('worker_threads');
92+
9093
let cwdCounter;
9194

9295
const environmentData = new SafeMap();
@@ -260,6 +263,11 @@ class Worker extends EventEmitter {
260263
this[kHandle].startThread();
261264

262265
process.nextTick(() => process.emit('worker', this));
266+
if (workerThreadsChannel.hasSubscribers) {
267+
workerThreadsChannel.publish({
268+
worker: this,
269+
});
270+
}
263271
}
264272

265273
[kOnExit](code, customErr, customErrReason) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
5+
const { ChildProcess } = require('child_process');
6+
const dc = require('diagnostics_channel');
7+
8+
if (cluster.isPrimary) {
9+
dc.subscribe('child_process', common.mustCall(({ process }) => {
10+
assert.strictEqual(process instanceof ChildProcess, true);
11+
}));
12+
const worker = cluster.fork();
13+
worker.on('online', common.mustCall(() => {
14+
worker.send('disconnect');
15+
}));
16+
} else {
17+
process.on('message', common.mustCall((msg) => {
18+
assert.strictEqual(msg, 'disconnect');
19+
process.disconnect();
20+
}));
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker } = require('worker_threads');
5+
const dc = require('diagnostics_channel');
6+
7+
dc.subscribe('worker_threads', common.mustCall(({ worker }) => {
8+
assert.strictEqual(worker instanceof Worker, true);
9+
}));
10+
11+
new Worker('const a = 1;', { eval: true });

0 commit comments

Comments
 (0)