Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

127.0.0.1:0 causes exception #1596

Closed
stevenvachon opened this issue May 21, 2015 · 6 comments
Closed

127.0.0.1:0 causes exception #1596

stevenvachon opened this issue May 21, 2015 · 6 comments

Comments

@stevenvachon
Copy link

require("request")("http://127.0.0.1:0");
events.js:141
      throw er; // Unhandled 'error' event
            ^
Error: connect EADDRNOTAVAIL 127.0.0.1 - Local (0.0.0.0:54620)
    at Object.exports._errnoException (util.js:843:11)
    at exports._exceptionWithHostPort (util.js:866:20)
    at connect (net.js:824:14)
    at lookupAndConnect (net.js:917:5)
    at Socket.connect (net.js:886: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)
@julien-f
Copy link

What does it mean to connect to the port 0?

0 can be used to bind to a random available port but it does not make sense to use it to connect…

@stevenvachon
Copy link
Author

Makes perfect sense in a test suite

@mmalecki
Copy link
Member

@stevenvachon would you mind explaining your use-case for connecting to port 0 in a test suite?

This one appears to be a difference in behavior between Node.js and Io.js - node v0.10.32 returns a ECONNREFUSED error to the callback while io.js v1.4.3 throws a RangeError. I believe that Io.js' behavior is more accurate as trying to connect to 0 does sound like a programmer's error.

@stevenvachon
Copy link
Author

Oops, I'd misunderstood the question. Serving on port 0 is perfect for test suites, however connecting (as was said) to port 0 is stupid.

I forget now why I'd ran that line of code, but it's at least evident at this point that the result is inconsistent than that of previous node.js versions. However, it's an io.js issue.

I'd ran that line of code to illustrate how this library is now inconsistent across node.js versions. Most errors are returned, while EADDRNOTAVAIL is thrown. Should this library do a try/catch to remain consistent and abstracted? Yes, it'd be a result of developer error, but catching it would avoid a server restart.

If anyone doesn't know, serving on port 0 is a standard way of connecting to any port that the OS has available, often a high number. Common ports such as 80 and 3000 are often not available and sniffing for such is faulty.

var server = require("http").createServer();
server.listen(0, host, function() {
    port = server.address().port;
});

@stevenvachon
Copy link
Author

Clarifying here as well: the issue is that this error is thrown, while all other errors are asynchronously passed to the callback function.

@stevenvachon stevenvachon changed the title Uncaught Error: connect EADDRNOTAVAIL 127.0.0.1:0 causes exception Jul 27, 2015
@simov
Copy link
Member

simov commented May 9, 2016

Fixed in v2.72 #2164

@simov simov closed this as completed May 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants