-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Comments
Sounds like a bug, not a doc problem, can you post a runnable gist to repro? |
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: Following the current docs I would expect this instead: |
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 |
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. |
`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]>
Documented in fbaef40. |
`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]>
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.
The text was updated successfully, but these errors were encountered: