net: don't throw on immediately destroyed socket
authorEvan Lucas <evanlucas@me.com>
Mon, 27 Jul 2015 00:24:28 +0000 (19:24 -0500)
committerEvan Lucas <evanlucas@me.com>
Mon, 27 Jul 2015 21:23:21 +0000 (16:23 -0500)
Fixes regression introduced in af249fa8a15bad8996187e73b480b30dcd881bad.

With connect being deferred to the next tick, Socket.destroy could be
called before connect. Socket.destroy sets _connecting to false which
would cause an assertion error.

Fixes: https://github.com/nodejs/io.js/issues/2250
PR-URL: https://github.com/nodejs/io.js/pull/2251
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
lib/net.js
test/parallel/test-net-connect-immediate-destroy.js [new file with mode: 0644]

index b5ae15e..c144caa 100644 (file)
@@ -927,7 +927,8 @@ function lookupAndConnect(self, options) {
   var addressType = exports.isIP(host);
   if (addressType) {
     process.nextTick(function() {
-      connect(self, host, port, addressType, localAddress, localPort);
+      if (self._connecting)
+        connect(self, host, port, addressType, localAddress, localPort);
     });
     return;
   }
diff --git a/test/parallel/test-net-connect-immediate-destroy.js b/test/parallel/test-net-connect-immediate-destroy.js
new file mode 100644 (file)
index 0000000..37dc4b2
--- /dev/null
@@ -0,0 +1,8 @@
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const net = require('net');
+
+const socket = net.connect(common.PORT, common.localhostIPv4, assert.fail);
+socket.on('error', assert.fail);
+socket.destroy();