You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
What's the difference between .close() and .disconnect() in dgram Web Socket #41815
I am trying to resolve an issue on nodejs/help.
The user reported getting Error [ERR_SOCKET_DGRAM_IS_CONNECTED]: Already connected when reconnecting after deliberately closing the socket connection.
I was able to reproduce the issue using .close() and I also tried .disconnect(). In both cases, I encountered different errors when trying to reconnect after (deliberately) closing/disconnecting the socket.
Here's what I would like to understand:
I want to understand the difference between the two methods and their intended behaviour.
Should the socket reconnection work after closing the socket?
Based on the explanation I intend to create a PR to fix the issue (if any) and also add more context to descriptions of .close() and .disconnect() in dgram documentation
You're being nominated as collaborator so I'll give you hints instead of a straight up answer to encourage exploration in the code:
Disconnect:
You can find the code showing what they do in dgram.js
You will notice disconnect delegates to the handle's disconnect directly, it in turn calls uv_udp_connect(handle, nullptr) (you can check the libuv docs for what that does :))
Then returns and sets the state
Close:
Pushes a call to close to the queue if there is one (what does the queue do? who calls enqueue in that code?)
Calls close on the handle (where does close come from?)
Apologies for the delayed reply. I had a busy week.
Thanks for pointing me in the right direction, I will dig into the code and come up with the difference.
I will work on it this weekend.
Disconnect:
It unsets the remote port and address set using connect(). That doesn't mean we stop receiving messages on the socket. Before disconnecting, we receive messages only from the remote peer set in connect(), disconnecting removes the exclusivity.
In the code, we directly use disconnect() on the handle which calls uv_udp_connect with a NULL value in udp_wrap.cc that disconnects the handle.
Close:
Shuts down the socket. No more messages.
It completely resets the handle.
In the code, we use recvStop() on the handle which calls uv_udp_recv_stop in udp_wrap.cc that stops listening for incoming messages.
So the difference is that close() stops new messages and shuts down the socket while disconnect() simply undoes the effects of connect() and the socket keeps listening for messages.
Reacted by Benjamin Gruenbaum, Rich Trott, Iamgabrielsoft, Santiago Gimeno, Katrin Yordanova and newever
The 'close' event is emitted after a socket is closed with close(). Once triggered, no new 'message' events will be emitted on this socket
A synchronous function that disassociates a connected dgram.Socket from its remote address. Trying to call disconnect() on an unbound or already disconnected socket will result in an
Activity
benjamingr commentedon Feb 1, 2022
You're being nominated as collaborator so I'll give you hints instead of a straight up answer to encourage exploration in the code:
Disconnect:
dgram.jsuv_udp_connect(handle, nullptr)(you can check the libuv docs for what that does :))Close:
Trott commentedon Feb 4, 2022
.connect()and.disconnect()were added relatively recently (in 2019) by @santigimeno. You may want to look at that PR for some context in addition to the things @benjamingr points you to.marsonya commentedon Feb 5, 2022
Apologies for the delayed reply. I had a busy week.
Thanks for pointing me in the right direction, I will dig into the code and come up with the difference.
I will work on it this weekend.
marsonya commentedon Feb 8, 2022
Here's what I've gathered:
Disconnect:
It unsets the remote port and address set using
connect(). That doesn't mean we stop receiving messages on the socket. Before disconnecting, we receive messages only from the remote peer set inconnect(), disconnecting removes the exclusivity.In the code, we directly use
disconnect()on the handle which callsuv_udp_connectwith a NULL value inudp_wrap.ccthat disconnects the handle.Close:
Shuts down the socket. No more messages.
It completely resets the handle.
In the code, we use
recvStop()on the handle which callsuv_udp_recv_stopinudp_wrap.ccthat stops listening for incoming messages.So the difference is that
close()stops new messages and shuts down the socket whiledisconnect()simply undoes the effects ofconnect()and the socket keeps listening for messages.iamgabrielsoft commentedon Mar 25, 2022
The 'close' event is emitted after a socket is closed with close(). Once triggered, no new 'message' events will be emitted on this socket
A synchronous function that disassociates a connected dgram.Socket from its remote address. Trying to call disconnect() on an unbound or already disconnected socket will result in an
The writeup was gotten from https://nodejs.org/api/dgram.html#event-listening
santigimeno commentedon Nov 7, 2022
I think #41815 (comment) is the correct answer. I'm going to close this. Feel free to reopen it if you think there's still something to be solved.