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

net: wrong docs for socket 'connect' event registration #901

Closed
Legiew opened this issue Feb 20, 2015 · 5 comments
Closed

net: wrong docs for socket 'connect' event registration #901

Legiew opened this issue Feb 20, 2015 · 5 comments
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. net Issues and PRs related to the net subsystem.

Comments

@Legiew
Copy link

Legiew commented Feb 20, 2015

I'am playing around with a few issues in the net module.

As said in the docs the parameter 'connectionListener' (callback) in the functions net.createConnection, net.connect and socket.connect will be added as an listener for the 'connect' event.

If you have a look in the socket.connect function, which is called from every of these functions, the 'connect' event is fired only once. (net.js line 864: self.once('connect', cb);) It is not saved or added as a permanent listener.

I think this should be corrected in the docs. If you create a socket with the net.createConnection function and reuse this socket and connect again with the socket.connect function you have to register again for the 'connect' event. If you rely on the docs you only will get one event fired.

I can make a PR for this, but first wanted to get some comments about this cause I am not 100% sure if I got this right.

@micnic micnic added doc Issues and PRs related to the documentations. net Issues and PRs related to the net subsystem. labels Feb 20, 2015
@sam-github
Copy link
Contributor

Sounds like a bug, not a doc problem, can you post a runnable gist to repro?

@Legiew
Copy link
Author

Legiew commented Mar 9, 2015

Hey,

sry that it took me so long, but I had no time to work on this.

Here is a gist to reproduce: https://gist.github.com/Legiew/46a54f686e7fd2421d39

You get the following:
server bound
Connect Event 1
client connected
client disconnected
Closed
reconnect
Connect Event 2
client connected
client disconnected
Closed

Following the current docs I would expect this instead:
server bound
Connect Event 1
client connected
client disconnected
Closed
reconnect
Connect Event 1
Connect Event 2
client connected
client disconnected
Closed

@silverwind
Copy link
Contributor

I also think this is more of a code bug, but if we change that behaviour there could possibly be a lot of broken modules relying on the current behavior. It looks like the .once listener also is applied to socket, and possible other places too (tls and the various createServer). I'd keep the change doc only for the sake of compatibilty.

@bnoordhuis
Copy link
Member

There's a good reason for using .once(). If .on() was used instead and the event listener holds a reference to (for example) a 100 MB buffer, that buffer will stay around for the lifetime of the socket.

Due to the way V8 allocates closure contexts for inner functions, your listener may be holding onto objects it doesn't even reference directly. That's why, as a rule of thumb, listeners should stay attached for the shortest period possible.

I move we document the current behavior.

PS: 'inner functions' includes top-level functions in modules and scripts. Those are wrapped in an outer function by the module loader.

@brendanashworth brendanashworth added the good first issue Issues that are suitable for first-time contributors. label Apr 21, 2015
bnoordhuis pushed a commit that referenced this issue May 17, 2015
`connectListener` is registered as a listener to the 'connect' event
once. Update the documentation to reflect that behavior.

Fixes: #901
PR-URL: #1717
Reviewed-By: Ben Noordhuis <[email protected]>
@bnoordhuis
Copy link
Member

Documented in fbaef40.

andrewdeandrade pushed a commit to andrewdeandrade/node that referenced this issue Jun 3, 2015
`connectListener` is registered as a listener to the 'connect' event
once. Update the documentation to reflect that behavior.

Fixes: nodejs/node#901
PR-URL: nodejs/node#1717
Reviewed-By: Ben Noordhuis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. net Issues and PRs related to the net subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants