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 <raisinten@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/58349
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: theanarkh <theratliter@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
This commit is contained in:
Darshan Sen 2025-05-18 14:35:17 +05:30 committed by GitHub
parent fdfd0fd7b5
commit 9eb9c26b26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 12 deletions

View File

@ -1292,9 +1292,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`

View File

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

View File

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

View File

@ -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