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

What's the difference between .close() and .disconnect() in dgram Web Socket #41815

Closed
marsonya opened this issue Feb 1, 2022 · 6 comments
Closed
Labels
dgram Issues and PRs related to the dgram subsystem / UDP. doc Issues and PRs related to the documentations. question Issues that look for answers.

Comments

@marsonya
Copy link
Member

marsonya commented Feb 1, 2022

Description of the problem

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

@marsonya marsonya added question Issues that look for answers. dgram Issues and PRs related to the dgram subsystem / UDP. doc Issues and PRs related to the documentations. labels Feb 1, 2022
@benjamingr
Copy link
Member

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?)

@Trott
Copy link
Member

Trott commented 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
Copy link
Member Author

marsonya commented 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
Copy link
Member Author

marsonya commented 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 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.

@iamgabrielsoft
Copy link

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dgram Issues and PRs related to the dgram subsystem / UDP. doc Issues and PRs related to the documentations. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

5 participants