From ba0dbaa1930db93a9e988ce1a525c8b2054a2970 Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Fri, 5 May 2017 06:10:43 -0500 Subject: [PATCH 1/2] Revert "net: remove unnecessary process.nextTick()" This reverts commit 571882c5a45872ac67e4e29513c4c8f23af9f781. Removing the process.nextTick() call can prevent the consumer from being able to catch error events. PR-URL: https://github.com/nodejs/node/pull/12854 Fixes: https://github.com/nodejs/node/issues/12841 Reviewed-By: Colin Ihrig Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis --- lib/net.js | 5 ++++- test/async-hooks/test-tcpwrap.js | 11 +++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/net.js b/lib/net.js index a3d778a44886ca..67e191fa213329 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1005,7 +1005,10 @@ function lookupAndConnect(self, options) { // If host is an IP, skip performing a lookup var addressType = cares.isIP(host); if (addressType) { - internalConnect(self, host, port, addressType, localAddress, localPort); + nextTick(self[async_id_symbol], function() { + if (self.connecting) + internalConnect(self, host, port, addressType, localAddress, localPort); + }); return; } diff --git a/test/async-hooks/test-tcpwrap.js b/test/async-hooks/test-tcpwrap.js index cbad3f3ddf3ca6..0827779229297b 100644 --- a/test/async-hooks/test-tcpwrap.js +++ b/test/async-hooks/test-tcpwrap.js @@ -48,13 +48,16 @@ const server = net { port: server.address().port, host: server.address().address }, common.mustCall(onconnected)); const tcps = hooks.activitiesOfTypes('TCPWRAP'); - const tcpconnects = hooks.activitiesOfTypes('TCPCONNECTWRAP'); assert.strictEqual( tcps.length, 2, '2 TCPWRAPs present when client is connecting'); - assert.strictEqual( - tcpconnects.length, 1, - '1 TCPCONNECTWRAP present when client is connecting'); + process.nextTick(() => { + const tcpconnects = hooks.activitiesOfTypes('TCPCONNECTWRAP'); + assert.strictEqual( + tcpconnects.length, 1, + '1 TCPCONNECTWRAP present when client is connecting'); + }); + tcp2 = tcps[1]; assert.strictEqual(tcps.length, 2, '2 TCPWRAP present when client is connecting'); From 2cf4882136b2c17c37aeb7b793a3040e64936b9f Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Thu, 11 May 2017 10:35:24 -0500 Subject: [PATCH 2/2] test: add regression test for immediate socket errors This test ensures that a http client request with the default agent that has a socket that is immediately destroyed can still be caught by adding an error event listener to the request object. PR-URL: https://github.com/nodejs/node/pull/12854 Fixes: https://github.com/nodejs/node/issues/12841 Reviewed-By: Colin Ihrig Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis --- test/parallel/test-http-client-immediate-error.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/parallel/test-http-client-immediate-error.js diff --git a/test/parallel/test-http-client-immediate-error.js b/test/parallel/test-http-client-immediate-error.js new file mode 100644 index 00000000000000..9fbe052efd4104 --- /dev/null +++ b/test/parallel/test-http-client-immediate-error.js @@ -0,0 +1,12 @@ +'use strict'; + +// Make sure http.request() can catch immediate errors in +// net.createConnection(). + +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); +const req = http.get({ host: '127.0.0.1', port: 1 }); +req.on('error', common.mustCall((err) => { + assert.strictEqual(err.code, 'ECONNREFUSED'); +}));