Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add diagnostics channel to process and worker #44045

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions doc/api/diagnostics_channel.md
Expand Up @@ -428,6 +428,8 @@ Emitted when server receives a request.

Emitted when server sends a response.

#### NET

`net.client.socket`

* `socket` {net.Socket}
Expand All @@ -440,13 +442,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
theanarkh marked this conversation as resolved.
Show resolved Hide resolved

<!-- YAML
added: REPLACEME
-->

`child_process`

* `process` {ChildProcess}

Emitted when a new process is created.

#### Worker Thread
theanarkh marked this conversation as resolved.
Show resolved Hide resolved

<!-- 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 });