From 9a358bd8d6a6c0e81c8b4d2bbab5c5cd13d111a7 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Fri, 16 May 2025 14:13:55 +0530 Subject: [PATCH] net: always publish to 'net.client.socket' diagnostics channel Previously, the 'net.client.socket' diagnostics channel was only published to when `net.connect()` was called. This change ensures the message is also published for the following calls: - net.createConnection() - net.Socket#connect() - tls.connect() Signed-off-by: Darshan Sen --- doc/api/diagnostics_channel.md | 4 +-- lib/net.js | 11 ++++--- ...agnostics-channel-net-client-socket-tls.js | 32 +++++++++++++++++++ test/parallel/test-diagnostics-channel-net.js | 20 +++++++++--- 4 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 test/parallel/test-diagnostics-channel-net-client-socket-tls.js diff --git a/doc/api/diagnostics_channel.md b/doc/api/diagnostics_channel.md index aef7f3e6ec88e4..0ca65d14aa2d71 100644 --- a/doc/api/diagnostics_channel.md +++ b/doc/api/diagnostics_channel.md @@ -1277,9 +1277,9 @@ Emitted when a `import()` throws an error. See [`error` event][]. `net.client.socket` -* `socket` {net.Socket} +* `socket` {net.Socket|tls.TLSSocket} -Emitted when a new TCP or pipe client socket is created. +Emitted when a new TCP or pipe client socket connection is created. `net.server.socket` diff --git a/lib/net.js b/lib/net.js index 7e0788556c8ef4..4d9cc4dd2c480e 100644 --- a/lib/net.js +++ b/lib/net.js @@ -238,11 +238,6 @@ function connect(...args) { debug('createConnection', normalized); const socket = new Socket(options); - if (netClientSocketChannel.hasSubscribers) { - netClientSocketChannel.publish({ - socket, - }); - } if (options.timeout) { socket.setTimeout(options.timeout); } @@ -1238,6 +1233,12 @@ Socket.prototype.connect = function(...args) { const options = normalized[0]; const cb = normalized[1]; + if (netClientSocketChannel.hasSubscribers) { + netClientSocketChannel.publish({ + socket: this, + }); + } + if (cb !== null) { this.once('connect', cb); } diff --git a/test/parallel/test-diagnostics-channel-net-client-socket-tls.js b/test/parallel/test-diagnostics-channel-net-client-socket-tls.js new file mode 100644 index 00000000000000..c887376fd288c9 --- /dev/null +++ b/test/parallel/test-diagnostics-channel-net-client-socket-tls.js @@ -0,0 +1,32 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +// This test ensures that the 'net.client.socket' diagnostics channel publishes +// a message when tls.connect() is used to create a socket connection. + +const assert = require('assert'); +const dc = require('diagnostics_channel'); +const fixtures = require('../common/fixtures'); +const tls = require('tls'); + +const options = { + key: fixtures.readKey('agent1-key.pem'), + cert: fixtures.readKey('agent1-cert.pem'), + rejectUnauthorized: false, +}; + +dc.subscribe('net.client.socket', common.mustCall(({ socket }) => { + assert.strictEqual(socket instanceof tls.TLSSocket, true); +})); + +const server = tls.createServer(options, common.mustCall((socket) => { + socket.destroy(); + server.close(); +})); + +server.listen({ port: 0 }, common.mustCall(() => { + const { port } = server.address(); + tls.connect(port, options); +})); diff --git a/test/parallel/test-diagnostics-channel-net.js b/test/parallel/test-diagnostics-channel-net.js index dc84a5b4e1db60..4a959ece277e84 100644 --- a/test/parallel/test-diagnostics-channel-net.js +++ b/test/parallel/test-diagnostics-channel-net.js @@ -1,5 +1,6 @@ 'use strict'; const common = require('../common'); +const Countdown = require('../common/countdown'); const assert = require('assert'); const net = require('net'); const dc = require('diagnostics_channel'); @@ -18,19 +19,23 @@ function testDiagnosticChannel(subscribers, test, after) { const testSuccessfulListen = common.mustCall(() => { let cb; - const server = net.createServer(common.mustCall((socket) => { - socket.destroy(); + const netClientSocketCount = 3; + const countdown = new Countdown(netClientSocketCount, () => { server.close(); cb(); - })); + }); + const server = net.createServer(common.mustCall((socket) => { + socket.destroy(); + countdown.dec(); + }, netClientSocketCount)); dc.subscribe('net.client.socket', common.mustCall(({ socket }) => { assert.strictEqual(isNetSocket(socket), true); - })); + }, netClientSocketCount)); dc.subscribe('net.server.socket', common.mustCall(({ socket }) => { assert.strictEqual(isNetSocket(socket), true); - })); + }, netClientSocketCount)); testDiagnosticChannel( { @@ -48,8 +53,13 @@ const testSuccessfulListen = common.mustCall(() => { common.mustCall((callback) => { cb = callback; server.listen({ port: 0, customOption: true }, () => { + // All supported ways of creating a net client socket connection. const { port } = server.address(); net.connect(port); + + net.createConnection(port); + + new net.Socket().connect(port); }); }), testFailingListen