Skip to content

Commit

Permalink
lib: add diagnostics channel for process and worker
Browse files Browse the repository at this point in the history
PR-URL: #44045
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
theanarkh committed Aug 30, 2022
1 parent 8f5dee0 commit 0633e9a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
29 changes: 29 additions & 0 deletions doc/api/diagnostics_channel.md
Expand Up @@ -434,6 +434,8 @@ Emitted when server receives a request.

Emitted when server sends a response.

#### NET

`net.client.socket`

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

Emitted when a new TCP or pipe connection is received.

#### UDP

`udp.socket`

* `socket` {dgram.Socket}

Emitted when a new UDP socket is created.

#### Process

<!-- YAML
added: REPLACEME
-->

`child_process`

* `process` {ChildProcess}

Emitted when a new process is created.

#### Worker Thread

<!-- YAML
added: REPLACEME
-->

`worker_threads`

* `worker` [`Worker`][]

Emitted when a new thread is created.

[`'uncaughtException'`]: process.md#event-uncaughtexception
[`Worker`]: worker_threads.md#class-worker
[`channel.subscribe(onMessage)`]: #channelsubscribeonmessage
[`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname
[`diagnostics_channel.subscribe(name, onMessage)`]: #diagnostics_channelsubscribename-onmessage
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/child_process.js
Expand Up @@ -59,6 +59,8 @@ const { convertToValidSignal, deprecate } = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
const spawn_sync = internalBinding('spawn_sync');
const { kStateSymbol } = require('internal/dgram');
const dc = require('diagnostics_channel');
const childProcessChannel = dc.channel('child_process');

const {
UV_EACCES,
Expand Down Expand Up @@ -301,6 +303,11 @@ function ChildProcess() {

maybeClose(this);
};
if (childProcessChannel.hasSubscribers) {
childProcessChannel.publish({
process: this,
});
}
}
ObjectSetPrototypeOf(ChildProcess.prototype, EventEmitter.prototype);
ObjectSetPrototypeOf(ChildProcess, EventEmitter);
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/worker.js
Expand Up @@ -89,6 +89,9 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
debug = fn;
});

const dc = require('diagnostics_channel');
const workerThreadsChannel = dc.channel('worker_threads');

let cwdCounter;

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

process.nextTick(() => process.emit('worker', this));
if (workerThreadsChannel.hasSubscribers) {
workerThreadsChannel.publish({
worker: this,
});
}
}

[kOnExit](code, customErr, customErrReason) {
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-diagnostics-channel-process.js
@@ -0,0 +1,21 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const { ChildProcess } = require('child_process');
const dc = require('diagnostics_channel');

if (cluster.isPrimary) {
dc.subscribe('child_process', common.mustCall(({ process }) => {
assert.strictEqual(process instanceof ChildProcess, true);
}));
const worker = cluster.fork();
worker.on('online', common.mustCall(() => {
worker.send('disconnect');
}));
} else {
process.on('message', common.mustCall((msg) => {
assert.strictEqual(msg, 'disconnect');
process.disconnect();
}));
}
11 changes: 11 additions & 0 deletions test/parallel/test-diagnostics-channel-worker-threads.js
@@ -0,0 +1,11 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
const dc = require('diagnostics_channel');

dc.subscribe('worker_threads', common.mustCall(({ worker }) => {
assert.strictEqual(worker instanceof Worker, true);
}));

new Worker('const a = 1;', { eval: true });

0 comments on commit 0633e9a

Please sign in to comment.