Pelle Windestam Pelle.Windestam@tagmaster.com [nuttx]
2018-02-14 12:15:13 UTC
I have an application which is having some problems when it closes a TCP socket when the remote end has "disappeared" (i.e. network cable unplugged on the remote end). The socket has the SO_LINGER option enabled and set to 1 second. The issue is that the close() call does not return after 1 second, but after 120 seconds when the connection times out in tcp_timer().
tcp_close_disconnect() is called by close(), sets up a callback to tcp_close_eventhandler(), triggers the ethernet device to poll the tcp/ip stack(?) and waits for a semaphore. The tcp/ip stack calls the tcp_close_eventhandler() callback function which sets the TCP_CLOSE flag and checks if the SO_LINGER timout has elapsed, which it hasn't since this is done rigth after the call to close(). The problem seems to be that the tcp_close_eventhandler() callback is not called again until the timout in tcp_timer(), but it needs to be called when the SO_LINGER timout runs out, to release the semphore that tcp_close_disconnect() is waiting for.
I'm still trying to wrap my head around how all these bits fit together, but if anyone can shed some light on the issue and/or possible solutions I am happy to hear it.