diff --git a/lib/net.js b/lib/net.js index b5ae15e4692393..44087e76c91801 100644 --- a/lib/net.js +++ b/lib/net.js @@ -927,7 +927,9 @@ function lookupAndConnect(self, options) { var addressType = exports.isIP(host); if (addressType) { process.nextTick(function() { - connect(self, host, port, addressType, localAddress, localPort); + // Verify that the socket wasn't destroyed already + if (self._connecting) + connect(self, host, port, addressType, localAddress, localPort); }); return; } diff --git a/test/parallel/test-net-socket-destroy-immediately.js b/test/parallel/test-net-socket-destroy-immediately.js new file mode 100644 index 00000000000000..b606edbfe01519 --- /dev/null +++ b/test/parallel/test-net-socket-destroy-immediately.js @@ -0,0 +1,12 @@ +'use strict'; +var common = require('../common'); +var assert = require('assert'); +var net = require('net'); + +var socket = net.createConnection(common.PORT, common.localhostIPv4); + +socket.on('error', function(err) { + assert(false); +}); + +socket.destroy();