Socket Reset by Peer Every 2 Hours – Troubleshooting

socketssltcp

A client connects to a server using ssl (openssl) over a tcp socket.

If there is no ongoing communication, 2 hours later (plus a couple of minutes), the client complains "Socket error. Connection reset by peer".

I've tested this over several days by having the client open a new connection upon getting this warning, and the pattern repeats every two hours. I've looked at cron jobs on both the client and server, and can not see any which might be doing this.

Note that the socket server (reactphp) is unaware that the socket has ever been closed.

What might cause the connection to be reset every two hours? Is there kernel configuration settings which might be doing so?

Best Answer

TCP keepalive. Rips down connections if they're unused, after 2 hours. Can be easily changed. See http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html

In a nutshell, kernel tunable "tcp_keepalive_time" which is exposed via /proc/sys/net/ipv4/tcp_keepalive_time can be changed from the default 7200 as required.

Related Question