Skip to content

net: always publish to 'net.client.socket' diagnostics channel #58349

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/api/diagnostics_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
11 changes: 6 additions & 5 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-diagnostics-channel-net-client-socket-tls.js
Original file line number Diff line number Diff line change
@@ -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);
}));
20 changes: 15 additions & 5 deletions test/parallel/test-diagnostics-channel-net.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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(
{
Expand All @@ -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
Expand Down
Loading