Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

net: make Socket.connect() consistently asynchronous #8180

Closed
wants to merge 3 commits into from
Closed

net: make Socket.connect() consistently asynchronous #8180

wants to merge 3 commits into from

Conversation

cjihrig
Copy link

@cjihrig cjihrig commented Aug 15, 2014

Socket.prototype.connect() follows a synchronous code path in some cases and an asynchronous one in others. This commit makes it consistently asynchronous. This commit also removes hard coded IPv4 addresses, and throws on bad inputs immediately, instead of potentially after an asynchronous operation. Closes #8140

EDIT: I might have worded this slightly wrong. The connection is made asynchronously. I was referring to the fact that one current code path involved a DNS lookup, while the other two completed synchronously.

@trevnorris trevnorris added the net label Aug 18, 2014
@trevnorris trevnorris added this to the v0.12 milestone Aug 18, 2014
@@ -868,26 +847,33 @@ Socket.prototype.connect = function(options, cb) {
}

timers._unrefActive(this);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary style change.

@indutny
Copy link
Member

indutny commented Aug 27, 2014

I like the refactor, but don't get what you trying to get with this approach? The connect function itself is async and there is no bad thing about calling it as soon as possible, or am I missing something?

@cjihrig
Copy link
Author

cjihrig commented Aug 27, 2014

@indutny sorry that the description of this PR is a bit unclear.

This started as work for #8140 (answering your ping 😄). Along the way I realized that the argument checking was a little problematic, as it was possible to have an asynchronous dns.lookup(), followed by a check that throws.

I added the process.nextTick() to make both branches of the if statement behave more similarly. If you'd like, I can remove the nextTick() (along with the other nits).

@indutny
Copy link
Member

indutny commented Aug 27, 2014

Well, just remove nextTick for now, please ;)

@cjihrig
Copy link
Author

cjihrig commented Aug 27, 2014

@indutny updated!

@cjihrig
Copy link
Author

cjihrig commented Nov 3, 2014

@indutny ping

throw new TypeError('localPort should be a number: ' + localPort);

if (port <= 0 || port > 65535)
throw new RangeError('Port should be > 0 and < 65536');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well follow the pattern and include what the request port was

`Socket.prototype.connect()` establishes a connection
synchronously in some cases and asynchronously in others. This
commit makes it consistently asynchronous. This commit also
removes hard coded IPv4 addresses, and throws on bad inputs
immediately, instead of potentially after an asynchronous
operation.
@cjihrig
Copy link
Author

cjihrig commented Nov 3, 2014

@tjfontaine updated based on your comments

@trevnorris
Copy link

@tjfontaine Anything else holding this up?

@cjihrig ping back on Friday if no other response.

@cjihrig
Copy link
Author

cjihrig commented Jan 2, 2015

@trevnorris Friday ping

if (addressType === 6 || addressType === 4) {
port = port | 0;
if (port <= 0 || port > 65535)
throw new RangeError('Port should be > 0 and < 65536');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't notice this before. Why removal of the port range check?

@trevnorris
Copy link

@cjihrig Things LGTM. Go ahead and squash it, add the usual commit fields and land it. Thanks. :)

cjihrig added a commit that referenced this pull request Jan 4, 2015
Socket.prototype.connect() sometimes throws on bad inputs
after an asynchronous operation. This commit makes the input
validation synchronous. This commit also removes some hard
coded IP addresses.

PR-URL: #8180
Fixes: #8140
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
Reviewed-By: Timothy J Fontaine <tjfontaine@gmail.com>
@cjihrig cjihrig closed this Jan 4, 2015
@cjihrig cjihrig deleted the hard-coded-ips branch January 4, 2015 01:16
@cjihrig
Copy link
Author

cjihrig commented Jan 4, 2015

Landed in b636ba8

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants