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();