From f21e59f99fb01ae86eeee76013e4a1a62f283174 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Thu, 12 Dec 2024 12:18:55 +0000 Subject: [PATCH] feature(root_daemon): robustness with timeout --- selfprivacy_api/root_daemon.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/selfprivacy_api/root_daemon.py b/selfprivacy_api/root_daemon.py index 829de94..9fef32b 100644 --- a/selfprivacy_api/root_daemon.py +++ b/selfprivacy_api/root_daemon.py @@ -117,20 +117,31 @@ def _process_request(request: str, allowed_commands: str) -> str: return "not permitted" +TIMEOUT = 6.0 + + def _root_loop(socket: socket_module.socket, allowed_commands): socket.listen(1) - # XXX - socket.settimeout(6.0) + socket.settimeout(TIMEOUT) while True: - conn, addr = socket.accept() + try: + conn, addr = socket.accept() + except TimeoutError: + continue + # conn is a new socket + # TODO: check that it can inherit timeout + conn.settimeout(TIMEOUT) pipe = conn.makefile("rw") # We accept a single line per connection for simplicity and safety line = pipe.readline() request = line.strip() answer = _process_request(request, allowed_commands) - conn.send(answer.encode("utf-8")) + try: + conn.send(answer.encode("utf-8")) + except TimeoutError: + pass pipe.close() conn.close()