镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@marsonya

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

Activity

  1. added
    questionIssues asking questions about Node.js.
    dgramIssues and PRs related to UDP and the dgram module.
    docIssues and PRs related to Node.js documentation.
    on Feb 1, 2022
  2. benjamingr commented on Feb 1, 2022

    @benjamingr
    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?)
  3. Trott commented on Feb 4, 2022

    @Trott
    Member

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

  4. marsonya commented on Feb 5, 2022

    @marsonya
    MemberAuthor

    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.

  5. marsonya commented on Feb 8, 2022

    @marsonya
    MemberAuthor

    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.

  6. iamgabrielsoft commented on Mar 25, 2022

    @iamgabrielsoft

    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

  7. santigimeno commented on Nov 7, 2022

    @santigimeno
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    dgramIssues and PRs related to UDP and the dgram module.docIssues and PRs related to Node.js documentation.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions