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

socket disconnection flushes itself? #1308

Closed
dvv opened this issue May 22, 2016 · 3 comments
Closed

socket disconnection flushes itself? #1308

dvv opened this issue May 22, 2016 · 3 comments

Comments

@dvv
Copy link
Contributor

dvv commented May 22, 2016

Hello!

Am trying to write code which reliably connects to a remote TCP socket and keeps trying to reconnect on failures:

CONNECT = function(host, port)
  local fd = net.createConnection(net.TCP, 0)
  local connect = function()
    print("TRY", node.heap())
    fd:connect(port, host)
  end
  fd:on("receive", function(fd, s)
    print("IN", node.heap(), s)
  end)
  fd:on("sent", function(fd)
    print("OU", node.heap())
  end)
  fd:on("connection", function(fd)
    print("CON", node.heap())
  end)
  fd:on("reconnection", function(fd)
    print("REC", node.heap())
  end)
  -- disconnection restarts connection
  fd:on("disconnection", function(fd)
    print("DIS", node.heap())
    tmr.alarm(6, 1000, 0, connect)
  end)
  -- first attempt to connect
  tmr.alarm(6, 1000, 0, connect)
  return fd
end

CONNECT("host", 4444) -- NB: not open at host

I expected to see repeating TRY DIS but saw only TRY DIS TRY. So seems that the call to fd:connect chokes after fd:on("disconnection", fn) fired.

Wonder do we invalidate somehow fd in disconnection handler and is it intended?

TIA

@devsaurus
Copy link
Member

Wonder do we invalidate somehow fd in disconnection handler

Apparently, yes. The code for "socket disconnected" frees memory that got initialized during createConnection(). It's a one-way road from "disconnected -> close -> delete" - the life cycle of a socket doesn't allow to re-connect, IMO.

and is it intended?

Don't know. It's built this way, though it doesn't look symmetrical wrt API design. Maybe worth a request to support this use case with the upcoming net module reimplementation in #1080.

@dvv
Copy link
Contributor Author

dvv commented May 22, 2016

I see, socket is not supposed to be reusable.
Thank you!

@TerryE
Copy link
Collaborator

TerryE commented May 22, 2016

If you look at the code reconnection is really a bad description of what this is. Sockets either terminate with a normal close or an error close such as an abort or a timeout. The way that the ESPCONN layer handles this is that the disconnection callback is fired on normal close and the reconnection callback otherwise. Either way at the moment closing a socket results in cleanup of all its resources. I am holiday at the moment but I will finish sorting out the reimplementation when I get back home.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants