Issue with socket SO_REUSEADDR when a client is connected.

jdzstz - gmail dot com jdzstz@gmail.com
Sun Jan 30 18:09:00 GMT 2011


I have detected an issue in windows xp with socket SO_REUSEADDR when a
tcp client keeps connected forever.

I don't know if it is a cygwin bug or a problem with microsoft winsock
implementation. I don't know if windows vista or windows 7 has the
same behaviour.


I have a server process that open a socket and binds to a port. After
a while, it closes the socket, re-open and re-bind to it.
In Unix (linux, solaris) the server process can always re-bind after
closing the previous socket, but in cygwin, there are some strange
issues. I have prepared a simplified tcp_server test program, in order
to be able to reproduce error in an easy way.

A)  If I bind to wildcard address 0.0.0.0   port 5555, it works
successfully, like Unix:

            1) tcp_server binds to 0.0.0.0:5555 and create a thread to
accept connections
            2) tcp_server sleep for 10 seconds
            3) a client process connect to 127.0.0.1:5555 and keeps
connected forever
            4) tcp_server awakes, closes all connections to port 5555
 (but client process keeps port connected, in ESTABLISHED state)
            5) tcp_server successfully reopens and rebind to 0.0.0.0:5555


B)  If I bind to address 127.0.0.1   port 5555, it fails:

            1) tcp_server binds to 127.0.0.1:5555 and create a thread
to accept connections
            2) tcp_server sleep for 10 seconds
            3) a client process connect to 127.0.0.1:5555 and keeps
connected forever
            4) tcp_server awakes, closes all connections to port 5555
 (but client process keeps port connected, in ESTABLISHED state)
            5) an error  "bind(): Address already in use"  occurs.


C) If there is no client connection, rebind is always successful.


TEST LOG:
=========

A) bind to 0.0.0.0 5555

$ ./tcp_server 0.0.0.0 5555 10
Start main
Opening socket on 0.0.0.0 5555
Main: Creating thread
Start sleep
Starting server thread, executing "accept"
accepted fd 4
End sleep
pthread_cancel
Waiting for server
Server returned "0xffffffff"
End main
Start main
Opening socket on 0.0.0.0 5555
Main: Creating thread
Start sleep
Starting server thread, executing "accept"
End sleep
pthread_cancel
Waiting for server
Server returned "0xffffffff"
End main
Start main
Opening socket on 0.0.0.0 5555


B) bind to 127.0.0.1 5555

$ ./tcp_server 127.0.0.1 5555 10
Start main
Opening socket on 127.0.0.1 5555
Main: Creating thread
Starting server thread, executing "accept"
Start sleep
accepted fd 4
End sleep
pthread_cancel
Waiting for server
Server returned "0xffffffff"
End main
Start main
bind(): Address already in use
Main: Creating thread
Start sleep
Starting server thread, executing "accept"
Accepted failed: Bad file descriptor
Ending thread
End sleep
pthread_cancel
Waiting for server
close = 9 Bad file descriptor
End main
Start main
bind(): Address already in use


I attach to email a tcp_server and tcp_client examples.  Execute
"./tcp_server 127.0.0.1 5555 10"  in one terminal and  "./tcp_client
127.0.0.1 5555" in other.
A telnet connection to tcp_server is also OK to see the problems.

This problem occurs sometimes in "varnishtest" tool  from varnish
cache software.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tcp_server.c
Type: text/x-csrc
Size: 3919 bytes
Desc: not available
URL: <http://cygwin.com/pipermail/cygwin/attachments/20110130/9b935277/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tcp_client.c
Type: text/x-csrc
Size: 2902 bytes
Desc: not available
URL: <http://cygwin.com/pipermail/cygwin/attachments/20110130/9b935277/attachment-0001.bin>
-------------- next part --------------
--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


More information about the Cygwin mailing list