I'm currently fighting with some crappy piece of (custom) server software which doesn't accept its connections properly (written in Java by a PHP programmer who never before touched sockets let alone threads). My guess is that a thread is dying before the socket is properly accepted in the client thread. I can't be sure and it actually doesn't matter much since the software is currently reimplemented; the old version has to be kept running until the new version goes online, as reliable as possible but without any time and money spent on debugging the old codebase.

The bug manifests itself in the following netstat output; some connections are never transferred from the kernel to use space (that's how I interpret this, better interpretations are welcome):

Proto Recv-Q Send-Q Local Address         Foreign Address         State       PID/Program name
tcp6     228      0       ESTABLISHED -               
tcp6       0      0      ESTABLISHED 14741/java      
tcp6       0      0        ESTABLISHED 14741/java      
tcp6    2677      0     ESTABLISHED -               
tcp6    3375      0      ESTABLISHED -               
tcp6   14742      0       ESTABLISHED -               
tcp6     774      0       ESTABLISHED -               
tcp6      92      0       ESTABLISHED -               
tcp6       0      0       ESTABLISHED 14741/java      
tcp6       0      0    ESTABLISHED 14741/java      
tcp6    3051      0   ESTABLISHED -               
tcp6     246      0       ESTABLISHED -               
tcp6     618      0       ESTABLISHED -               
tcp6    1041      0       ESTABLISHED -               
tcp6       0      0       ESTABLISHED 14741/java      

When this happens and the clients reconnect, they tend to work. But they won't reconnect by itself until they run into a rather long timeout. Since the custom full-duplex protocol in its current incarnation doesn't ack any data sent by the client and the latter doesn't expect any regularly incoming requests from the server, this can be days since the client sends its data happily until the kernel's receive queue runs full. On the server (kernel) side it should be possible to detect stale sockets since the clients send data regularly.

So, assuming my interpretation of this problem is correct, what I wondered was if there is a kernel parameter I can tune which makes the kernel drop/close TCP connections with a RST if they aren't read from by the user space in a timely manner.

Better explanations of what happens here are welcome as well.


You can try tuning TCP keepalive to much shorter values. By default a connection can be idle for two hours before keepalive kicks in.

Exactly what values you should use is really dependent on what your application does and what your users expect or how they interact with it.

  • Unfortunately, that doesn't work. The client is a proprietary GSM module (Cinterion BGS2T) which doesn't support sending TCP Keepalives. The server part does theoretically use Keepalives but it looks like the thread dies before that socket option is set (the sockets hang around for hours or days if the server isn't restarted). – mss Sep 4 '13 at 21:35
  • Did you actually try it? – Michael Hampton Sep 4 '13 at 21:38
  • Yes, I did. I set the Keepalive timeout down to 15 minutes for testing. – mss Sep 4 '13 at 21:47
  • You might not have any other option than to soundly berate the developers, then. – Michael Hampton Sep 4 '13 at 21:48
  • One idea I had was to set the size of the kernel's receive queue to a really small value to make it kill these connections. But since the options are global and not per-process I'm afraid to break a lot of other things by doing so. – mss Sep 4 '13 at 22:01

I guess the answer is no.

The problem was solved by replacing the software in question but ideas are still welcome.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.