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 <[email protected]>
  • Loading branch information
theanarkh authored and juanarbol committed Oct 11, 2022
1 parent 8f3e8bb commit 6be761e
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
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ Emitted when server receives a request.

Emitted when server sends a response.

#### NET

`net.client.socket`

* `socket` {net.Socket}
Expand All @@ -437,13 +439,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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 @@ -260,6 +263,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 6be761e

Please sign in to comment.