GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — http/net: fix catching immediate connect errors by m0ppers · Pull Request #1823 · nodejs/node

Conversation

@m0ppers

@m0ppers m0ppers commented May 28, 2015

Copy link
Copy Markdown

When creating a standard http client request the agent is calling
net.createConnection. The agent will then defer installing error
handling to nextTick(). If there is any immediate error in
createConnection iojs will bail with an uncaught error. There is already
one error situation which is handled in a special way (i.e. throwing
error is deferred so the standard error handler may catch it). However
there are more situations where this may happen: Namely in the connect()
function.

Due to a locally changed ip address I was getting the following error and the iojs bailed out (which should not happen as error management was in place):

events.js:141
      throw er; // Unhandled 'error' event
            ^
Error: connect EHOSTDOWN 192.168.1.11:80 - Local (192.168.1.10:54125)
    at Object.exports._errnoException (util.js:846:11)
    at exports._exceptionWithHostPort (util.js:869:20)
    at connect (net.js:840:14)
    at lookupAndConnect (net.js:933:5)
    at Socket.connect (net.js:902:5)
    at Agent.exports.connect.exports.createConnection (net.js:61:35)
    at Agent.createSocket (_http_agent.js:177:16)
    at Agent.addRequest (_http_agent.js:147:23)
    at new ClientRequest (_http_client.js:132:16)
    at Object.exports.request (http.js:30:10)

I am still unsure why I am the only one facing this problem and maybe the pull request is entirely bogus. The error situation seems pretty common to me (and that's why I am surprised): My dhcp assigned my dev machine a new address and connecting to the old, hardcoded ip address is now failing with an ETIMEOUT (as expected). However another, slightly (few seconds) deferred request (while the first one is still working) will trigger an immediate EHOSTDOWN/EHOSTUNREACH error for me which cannot be caught using request.on("error", fn).

I can 100% reproduce this with iojs 2.1 and master using the following gist:

https://gist.github.com/m0ppers/3709f7d87b73be9aca7b

I am on osx yosemite.

The wrapped nextTick, nextTick thing is of course totally evil. However I cannot seem to fix it otherwise. The root cause IMHO is the deferred error management in the http client. I don't know the reason for it and left it untouched but IMHO this should be the real fix.

It has been introduced with this commit (which makes me even more unsure as this is years ago):

nodejs/node-v0.x-archive@7c87e09

Can somebody reproduce the problem? Am missing something? Does somebody have a better fix (nextTick(nextTick()))? :S

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@m0ppers
http/net: fix catching immediate connect errors
6fbd074
When creating a standard http client request the agent is calling
net.createConnection. The agent will then defer installing error
handling to nextTick(). If there is any immediate error in
createConnection iojs will bail with an uncaught error. There is already
one error situation which is handled in a special way (i.e. throwing
error is deferred so the standard error handler may catch it). However
there are more situations where this may happen: Namely in the connect()
function.
@Fishrock123 Fishrock123 added http Issues and PRs related to the http subsystem. net Issues and PRs related to the net subsystem. labels May 28, 2015
@Fishrock123

Fishrock123 commented May 28, 2015

Copy link
Copy Markdown
Contributor

@m0ppers does this happen on node 0.12 too? (I'm assuming it does given the commit in question)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@m0ppers

m0ppers commented May 28, 2015

Copy link
Copy Markdown
Author

Interesting :O works as expected with 0.12:

hans-guenther:io.js mop$ node -v; node broken.js 
v0.12.4
Server running at http://<ip-address>:8001/
GOT REQUEST
GOT REQUEST
ERR { [Error: connect EHOSTUNREACH]
  code: 'EHOSTUNREACH',
  errno: 'EHOSTUNREACH',
  syscall: 'connect' }
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@m0ppers

m0ppers commented May 28, 2015

Copy link
Copy Markdown
Author

Just reverified with io.js 2.1.0:

hans-guenther:io.js mop$ node -v; node broken.js 
v2.1.0
Server running at http://<ip-address>:8001/
GOT REQUEST
GOT REQUEST
events.js:141
      throw er; // Unhandled 'error' event
            ^
Error: connect EHOSTDOWN 192.168.1.11:80 - Local (192.168.1.10:55714)
    at Object.exports._errnoException (util.js:846:11)
    at exports._exceptionWithHostPort (util.js:869:20)
    at connect (net.js:840:14)
    at lookupAndConnect (net.js:933:5)
    at Socket.connect (net.js:902:5)
    at Agent.exports.connect.exports.createConnection (net.js:61:35)
    at Agent.createSocket (_http_agent.js:177:16)
    at Agent.addRequest (_http_agent.js:147:23)
    at new ClientRequest (_http_client.js:132:16)
    at Object.exports.request (http.js:30:10)

So definately some regression. Interesting :O Didn't expect that. However that explains why so far nobody got that error

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@evanlucas

evanlucas commented May 28, 2015

Copy link
Copy Markdown
Contributor

I'm thinking this may a regression introduced in #1505. If the host is an IP address, it skips calling the lookup function which could be causing the error to be on the same tick. Looking into it.

Ref: https://github.com/nodejs/io.js/blob/master/lib/net.js#L927

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@evanlucas

evanlucas commented May 28, 2015

Copy link
Copy Markdown
Contributor

Can you try to reproduce after apply this patch?

commit f31295c0925464621073d2d635d9060284d4f62d
Author: Evan Lucas <[email protected]>
Date:   Thu May 28 18:08:06 2015 -0500

    defer

diff --git a/lib/net.js b/lib/net.js
index 5309521..d74f001 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -924,7 +924,9 @@ function lookupAndConnect(self, options) {
   // TODO(evanlucas) should we hot path this for localhost?
   var addressType = exports.isIP(host);
   if (addressType) {
-    connect(self, host, port, addressType, localAddress, localPort);
+    process.nextTick(function() {
+      connect(self, host, port, addressType, localAddress, localPort);
+    })
     return;
   }

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@m0ppers

m0ppers commented May 29, 2015

Copy link
Copy Markdown
Author

Doesn't help :S it DOES however help when wrapped in another (evil) nextTick(). Would you agree that the rootcause for all the nextTick hacks in connect() is the http client stuff? Maybe it is time to fix the rootcause? The sole reason the error management has to be wrapped into nextTick(s) is that the http client stuff installs error management only on nextTick() isn't it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bnoordhuis
bnoordhuis force-pushed the master branch from 311c3e4 to b926718 Compare June 2, 2015 17:25
@evanlucas

evanlucas commented Jun 2, 2015

Copy link
Copy Markdown
Contributor

I am not able to reproduce with the above patch. I am thinking that removing the hot path of skipping lookup should be added back though. Mind trying this patch?

commit 9daac3964dd0db81564759375b0f18a201d88306
Author: Evan Lucas <[email protected]>
Date:   Tue Jun 2 14:55:39 2015 -0500

    Don't skip lookup

    If lookup is skipped, an error can be emitted on the current tick
    (before an error listener has been added)

diff --git a/lib/net.js b/lib/net.js
index 5309521..06f38fa 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -920,14 +920,6 @@ function lookupAndConnect(self, options) {
   }
   port |= 0;

-  // If host is an IP, skip performing a lookup
-  // TODO(evanlucas) should we hot path this for localhost?
-  var addressType = exports.isIP(host);
-  if (addressType) {
-    connect(self, host, port, addressType, localAddress, localPort);
-    return;
-  }
-
   if (options.lookup && typeof options.lookup !== 'function')
     throw new TypeError('options.lookup should be a function.');
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@m0ppers

m0ppers commented Jun 8, 2015

Copy link
Copy Markdown
Author

that works...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@m0ppers

m0ppers commented Jun 8, 2015

Copy link
Copy Markdown
Author

i just retried the nextTick around connect patch and it now (?) works for me too. Maybe i made a mistake while applying the patch. You can reproduce my original problem, can't you? If so i would assume that i made an error testing that patch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@m0ppers

m0ppers commented Jun 24, 2015

Copy link
Copy Markdown
Author

any update?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@evanlucas evanlucas mentioned this pull request Jun 25, 2015
net: wrap connect in nextTick #2054
Merged
@rvagg
rvagg force-pushed the master branch from 9d21135 to 628a3ab Compare June 25, 2015 09:18
evanlucas added a commit to evanlucas/node that referenced this pull request Jul 1, 2015
@evanlucas
net: wrap connect in nextTick
af249fa
Fixes an edge case regression introduced in
1bef717.

With the lookup being skipped, an error could be emitted before an
error listener has been added.

An example of this was presented by changing the server’s IP address
and then immediately making a request to the old address.

Related: nodejs#1823
PR-URL: nodejs#2054
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-by: Trevor Norris <[email protected]>
@evanlucas

evanlucas commented Jul 1, 2015

Copy link
Copy Markdown
Contributor

Fixed in af249fa.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@evanlucas evanlucas closed this Jul 1, 2015
mscdex pushed a commit to mscdex/io.js that referenced this pull request Jul 9, 2015
@evanlucas @mscdex
net: wrap connect in nextTick
8c43ff2
Fixes an edge case regression introduced in
1bef717.

With the lookup being skipped, an error could be emitted before an
error listener has been added.

An example of this was presented by changing the server’s IP address
and then immediately making a request to the old address.

Related: nodejs#1823
PR-URL: nodejs#2054
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-by: Trevor Norris <[email protected]>
This was referenced Jun 22, 2023
[Snyk] Fix for 1 vulnerabilities aliscco/alisco-node#506
Open
[Snyk] Fix for 1 vulnerabilities aliscco/alisco-node#508
Open
[Snyk] Fix for 1 vulnerabilities aliscco/alisco-node#543
Open
[Snyk] Fix for 1 vulnerabilities aliscco/alisco-node#574
Open
This was referenced Jul 7, 2023
[Snyk] Fix for 4 vulnerabilities leonardoadame/node#63
Open
[Snyk] Fix for 9 vulnerabilities leonardoadame/node#128
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

http Issues and PRs related to the http subsystem. net Issues and PRs related to the net subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@m0ppers @Fishrock123 @evanlucas
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.