Skip to content

Commit 9eb9c26

Browse files
authored
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 <[email protected]> PR-URL: #58349 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: theanarkh <[email protected]> Reviewed-By: Paolo Insogna <[email protected]>
1 parent fdfd0fd commit 9eb9c26

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

doc/api/diagnostics_channel.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,9 +1292,9 @@ Emitted when a `import()` throws an error. See [`error` event][].
12921292
12931293
`net.client.socket`
12941294

1295-
* `socket` {net.Socket}
1295+
* `socket` {net.Socket|tls.TLSSocket}
12961296

1297-
Emitted when a new TCP or pipe client socket is created.
1297+
Emitted when a new TCP or pipe client socket connection is created.
12981298

12991299
`net.server.socket`
13001300

lib/net.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,6 @@ function connect(...args) {
238238
debug('createConnection', normalized);
239239
const socket = new Socket(options);
240240

241-
if (netClientSocketChannel.hasSubscribers) {
242-
netClientSocketChannel.publish({
243-
socket,
244-
});
245-
}
246241
if (options.timeout) {
247242
socket.setTimeout(options.timeout);
248243
}
@@ -1238,6 +1233,12 @@ Socket.prototype.connect = function(...args) {
12381233
const options = normalized[0];
12391234
const cb = normalized[1];
12401235

1236+
if (netClientSocketChannel.hasSubscribers) {
1237+
netClientSocketChannel.publish({
1238+
socket: this,
1239+
});
1240+
}
1241+
12411242
if (cb !== null) {
12421243
this.once('connect', cb);
12431244
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
// This test ensures that the 'net.client.socket' diagnostics channel publishes
7+
// a message when tls.connect() is used to create a socket connection.
8+
9+
const assert = require('assert');
10+
const dc = require('diagnostics_channel');
11+
const fixtures = require('../common/fixtures');
12+
const tls = require('tls');
13+
14+
const options = {
15+
key: fixtures.readKey('agent1-key.pem'),
16+
cert: fixtures.readKey('agent1-cert.pem'),
17+
rejectUnauthorized: false,
18+
};
19+
20+
dc.subscribe('net.client.socket', common.mustCall(({ socket }) => {
21+
assert.strictEqual(socket instanceof tls.TLSSocket, true);
22+
}));
23+
24+
const server = tls.createServer(options, common.mustCall((socket) => {
25+
socket.destroy();
26+
server.close();
27+
}));
28+
29+
server.listen({ port: 0 }, common.mustCall(() => {
30+
const { port } = server.address();
31+
tls.connect(port, options);
32+
}));

test/parallel/test-diagnostics-channel-net.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const common = require('../common');
3+
const Countdown = require('../common/countdown');
34
const assert = require('assert');
45
const net = require('net');
56
const dc = require('diagnostics_channel');
@@ -18,19 +19,23 @@ function testDiagnosticChannel(subscribers, test, after) {
1819

1920
const testSuccessfulListen = common.mustCall(() => {
2021
let cb;
21-
const server = net.createServer(common.mustCall((socket) => {
22-
socket.destroy();
22+
const netClientSocketCount = 3;
23+
const countdown = new Countdown(netClientSocketCount, () => {
2324
server.close();
2425
cb();
25-
}));
26+
});
27+
const server = net.createServer(common.mustCall((socket) => {
28+
socket.destroy();
29+
countdown.dec();
30+
}, netClientSocketCount));
2631

2732
dc.subscribe('net.client.socket', common.mustCall(({ socket }) => {
2833
assert.strictEqual(isNetSocket(socket), true);
29-
}));
34+
}, netClientSocketCount));
3035

3136
dc.subscribe('net.server.socket', common.mustCall(({ socket }) => {
3237
assert.strictEqual(isNetSocket(socket), true);
33-
}));
38+
}, netClientSocketCount));
3439

3540
testDiagnosticChannel(
3641
{
@@ -48,8 +53,13 @@ const testSuccessfulListen = common.mustCall(() => {
4853
common.mustCall((callback) => {
4954
cb = callback;
5055
server.listen({ port: 0, customOption: true }, () => {
56+
// All supported ways of creating a net client socket connection.
5157
const { port } = server.address();
5258
net.connect(port);
59+
60+
net.createConnection(port);
61+
62+
new net.Socket().connect(port);
5363
});
5464
}),
5565
testFailingListen

0 commit comments

Comments
 (0)