dermesser / libsocket

The ultimate socket library for C and C++, supporting TCP, UDP and Unix sockets (DGRAM and STREAM) on Linux, FreeBSD, Solaris. See also my uvco library for a refreshed version!
https://borgac.net/~lbo/doc/libsocket/
Other
797 stars 195 forks source link

Address already in use on a clean close #69

Closed GlassBeaver closed 4 years ago

GlassBeaver commented 4 years ago

Hi, first thank you for the excellent libsocket library.

I have a problem similar to #61 in that TCP listen sockets on a server are kept in a TIME_WAIT state by linux (centos 7) for approximately 30 seconds. I've read #61 and tried the following but it didn't help:

sysctl net.ipv4.tcp_fin_timeout=5
sysctl net.ipv4.tcp_tw_recycle=1
sysctl net.ipv4.tcp_tw_reuse=1

I think I'm closing the listen stream & socket correctly:

listenStream->shutdown(LIBSOCKET_READ | LIBSOCKET_WRITE);
listenStream->destroy();
...
listenSocket.destroy();

Do you have any advice on how to combat this?

dermesser commented 4 years ago

I've observed this basically since I started working on it. I just tried reproducing it with the server.cpp/client.cpp examples. It resulted in this error about 2 out of 5 times I've ran it. Note that I didn't do a clean close here, but instead Ctrl-C'd the process.

With the transmission_client/_server examples, where the server actually closes the socket, I haven't been able to reproduce it. This is all on kernel 5.5.8.

I'm also not sure what is the reason for it. Given that libsocket is a relatively thin layer on top of standard syscalls, it must be something related to the TCP timing that you mentioned already. The parameters you posted definitely seem to improve the situation.

GlassBeaver commented 4 years ago

Thanks for taking a look - I'll close the ticket and update my servers to the latest kernels.