0

I have a server on a well known TCP port to which bunch of clients are connected. Clients use the non blocking option to connect to the server.

When I kill the server process, the client sockets go to CLOSE_WAIT state. Now if I restart the server process and the clients try to connect again, the connect() call seems to block even though its supposed to be non-blocking..

The actual fix might actually be to close the socket when the server dies. But I am trying to understand the current behavior..

  • when an existing connection is in CLOSE_WAIT what is preventing a new connection being established ?
  • Why is the connect blocking even though is non-blocking option is set ?

This is seen with Linux 2.6.3x kernel..

Manohar
  • 3,865
  • 11
  • 41
  • 56
  • 2.6.3x kernel does not mean much. There is a considerable difference between 2.6.30 & 2.6.38. And upgrading the kernel to a 3.0.0 or 3.1.0 might make a difference. – Basile Starynkevitch Jan 12 '12 at 01:42
  • Are you using `SO_REUSEADDR`? See http://stackoverflow.com/questions/775638/using-so-reuseaddr-what-happens-to-previously-open-socket – Basile Starynkevitch Jan 12 '12 at 01:46
  • @BasileStarynkevitch 2.6.3x is sufficient info for this question. This seems to be a basic TCP/IP behavior which is unlikely to change often. Actual version is 2.6.32. And no, I am not going to try 3.0.0 on the assumption that the behavior might be different in 3.0.0 – Manohar Jan 12 '12 at 01:47
  • And what about the `SO_REUSEADDR` option? – Basile Starynkevitch Jan 12 '12 at 01:48
  • Isn't the SO_REUSEADDR option useful at the server side, where the server wants to listen on the same socket right after a restart ? In my case server is able to bind to the same well-known port. But the issue is at the client side where they are not able to connect.. – Manohar Jan 12 '12 at 01:52

2 Answers2

1

It sounds like a bug in the client. If you set the socket non-blocking and then call connect, there is no reason the connect call should block. Can you paste the client code that creates the socket, sets it non-blocking, and calls connect? Also, are you positive it is blocked in the connect call itself?

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Its possible that I had a bug in the client.. client has an inotify fd which it monitors using libevent to track death of the server. Looks like read on the inotify fd was blocking. my bad.. and Thanks. – Manohar Jan 12 '12 at 09:38
0

I believe your question is quite exactly answered here and related to SO_REUSEADDR. The other answer to a question about Using SO_REUSEADDR - What happens to previously open socket? is also relevant.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547