From 65c475269edc38e310e814947cf913989d268665 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 1 Oct 2019 20:21:22 +0200 Subject: [PATCH] net: emit close on unconnected socket Socket should always emit 'close'. Regardless whether it has been connected or not. PR-URL: https://github.com/nodejs/node/pull/29803 Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater Reviewed-By: Trivikram Kamat Reviewed-By: Minwoo Jung --- 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..73fdb988f9ca83 --- /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();