From 68f4ce662cad28fed739900addbdee949ad3c1e8 Mon Sep 17 00:00:00 2001 From: Joshua Watt Date: Tue, 19 Jul 2022 13:36:53 -0500 Subject: 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. Signed-off-by: Joshua Watt Signed-off-by: Richard Purdie --- lib/bb/asyncrpc/serv.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/bb/asyncrpc/serv.py b/lib/bb/asyncrpc/serv.py index b4cffff21..585bc121d 100644 --- a/lib/bb/asyncrpc/serv.py +++ b/lib/bb/asyncrpc/serv.py @@ -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]) -- cgit 1.2.3-korg