From 2b811121cdda7b87d2ba3845a5dd1dcdc73d577d Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 1 Oct 2019 20:21:22 +0200 Subject: [PATCH 1/2] net: emit close on unconnected socket Socket should always emit 'close'. Regardless whether it has been connected or not. --- lib/net.js | 6 ++++-- test/parallel/test-net-connect-destroy.js | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-net-connect-destroy.js diff --git a/lib/net.js b/lib/net.js index 760bf084f4d324..baaf2180d1b463 100644 --- a/lib/net.js +++ b/lib/net.js @@ -660,10 +660,12 @@ Socket.prototype._destroy = function(exception, cb) { this._handle.onread = noop; this._handle = null; this._sockname = null; + cb(exception); + } else { + cb(exception); + process.nextTick(emitCloseNT, this); } - cb(exception); - if (this._server) { debug('has server'); this._server._connections--; diff --git a/test/parallel/test-net-connect-destroy.js b/test/parallel/test-net-connect-destroy.js new file mode 100644 index 00000000000000..3fde1c6378d822 --- /dev/null +++ b/test/parallel/test-net-connect-destroy.js @@ -0,0 +1,7 @@ +'use strict'; +const common = require('../common'); +const net = require('net'); + +const socket = new net.Socket() +socket.on('close', common.mustCall()); +socket.destroy(); From c1c2e312cfc801610547bcc67ad0352d2f084c49 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Thu, 3 Oct 2019 23:03:00 +0200 Subject: [PATCH 2/2] fixup: linting --- test/parallel/test-net-connect-destroy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-net-connect-destroy.js b/test/parallel/test-net-connect-destroy.js index 3fde1c6378d822..73fdb988f9ca83 100644 --- a/test/parallel/test-net-connect-destroy.js +++ b/test/parallel/test-net-connect-destroy.js @@ -2,6 +2,6 @@ const common = require('../common'); const net = require('net'); -const socket = new net.Socket() +const socket = new net.Socket(); socket.on('close', common.mustCall()); socket.destroy();