Skip to content

Commit 0633e9a

Browse files
authored
lib: add diagnostics channel for process and worker
PR-URL: #44045 Reviewed-By: James M Snell <[email protected]>
1 parent 8f5dee0 commit 0633e9a

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

doc/api/diagnostics_channel.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ Emitted when server receives a request.
434434

435435
Emitted when server sends a response.
436436

437+
#### NET
438+
437439
`net.client.socket`
438440

439441
* `socket` {net.Socket}
@@ -446,13 +448,40 @@ Emitted when a new TCP or pipe client socket is created.
446448

447449
Emitted when a new TCP or pipe connection is received.
448450

451+
#### UDP
452+
449453
`udp.socket`
450454

451455
* `socket` {dgram.Socket}
452456

453457
Emitted when a new UDP socket is created.
454458

459+
#### Process
460+
461+
<!-- YAML
462+
added: REPLACEME
463+
-->
464+
465+
`child_process`
466+
467+
* `process` {ChildProcess}
468+
469+
Emitted when a new process is created.
470+
471+
#### Worker Thread
472+
473+
<!-- YAML
474+
added: REPLACEME
475+
-->
476+
477+
`worker_threads`
478+
479+
* `worker` [`Worker`][]
480+
481+
Emitted when a new thread is created.
482+
455483
[`'uncaughtException'`]: process.md#event-uncaughtexception
484+
[`Worker`]: worker_threads.md#class-worker
456485
[`channel.subscribe(onMessage)`]: #channelsubscribeonmessage
457486
[`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname
458487
[`diagnostics_channel.subscribe(name, onMessage)`]: #diagnostics_channelsubscribename-onmessage

lib/internal/child_process.js

Lines changed: 7 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
8989
debug = fn;
9090
});
9191

92+
const dc = require('diagnostics_channel');
93+
const workerThreadsChannel = dc.channel('worker_threads');
94+
9295
let cwdCounter;
9396

9497
const environmentData = new SafeMap();
@@ -262,6 +265,11 @@ class Worker extends EventEmitter {
262265
this[kHandle].startThread();
263266

264267
process.nextTick(() => process.emit('worker', this));
268+
if (workerThreadsChannel.hasSubscribers) {
269+
workerThreadsChannel.publish({
270+
worker: this,
271+
});
272+
}
265273
}
266274

267275
[kOnExit](code, customErr, customErrReason) {
Lines changed: 21 additions & 0 deletions
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+
}
Lines changed: 11 additions & 0 deletions
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)