Skip to contentSkip to conversation

asyncrpc: Add TCP Keep Alives

PATCH New Joshua Watt 1 participant 1 message Jul 19, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 asyncrpc: Add TCP Keep Alives

Adds TCP Keep Alive support to the async RPC server. This should help prevent file descriptor exhaustion on the server when client connections are interrupted and the socket never closes (e.g. no FIN is sent from the client).

A keep alive is sent after 30 seconds of inactivity, then every 15 seconds after that up to a maximum of 2 minutes.

---
 bitbake/lib/bb/asyncrpc/serv.py | 7 +++++++
 1 file changed, 7 insertions(+)
bitbake/lib/bb/asyncrpc/serv.py +7 -0
index b4cffff213..585bc121da 100644
@@ -151,6 +151,13 @@ class AsyncServer(object):
                 s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
                 s.setsockopt(socket.SOL_TCP, socket.TCP_QUICKACK, 1)
 
+                # Enable keep alives. This prevents broken client connections
+                # from persisting on the server for long periods of time.
+                s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
+                s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 30)
+                s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 15)
+                s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 4)
+
             name = self.server.sockets[0].getsockname()
             if self.server.sockets[0].family == socket.AF_INET6:
                 self.address = "[%s]:%d" % (name[0], name[1])
Signature · 1 line
2.33.0