lib: add diagnostics channel for process and worker

PR-URL: https://github.com/nodejs/node/pull/44045
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
theanarkh authored and GitHub committed 2022-08-30 17:08:33 +00:00
1 parent 8f5dee0f6e
commit 0633e9a0b5
5 files changed
+76

No files matched your search

+29
View File
@@ -434,6 +434,8 @@ Emitted when server receives a request.
Emitted when server sends a response.
#### NET
`net.client.socket`
* `socket` {net.Socket}
@@ -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
+7
View File
@@ -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,
@@ -301,6 +303,11 @@ function ChildProcess() {
maybeClose(this);
};
if (childProcessChannel.hasSubscribers) {
childProcessChannel.publish({
process: this,
});
}
}
ObjectSetPrototypeOf(ChildProcess.prototype, EventEmitter.prototype);
ObjectSetPrototypeOf(ChildProcess, EventEmitter);
+8
View File
@@ -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();
@@ -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) {
@@ -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();
}));
}
@@ -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 });