Coverage for app/backend/src/couchers/supervisor.py: 97%

42 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-11-30 13:34 +0000

1""" 

2Parent-process supervision for the child processes (API workers, background workers, scheduler, compactor). 

3 

4We don't respawn individual children: if any child exits on its own the parent tears the rest down and 

5returns the dead child, so the caller can exit non-zero and let the container be restarted from scratch. 

6On SIGTERM/SIGINT the parent instead shuts down gracefully, forwarding SIGTERM so children drain. 

7 

8Every child and parent-local gRPC server is told to stop before any of them is waited on, so they drain 

9concurrently. Children still alive after SHUTDOWN_DEADLINE are killed. 

10""" 

11 

12import logging 

13import signal 

14import threading 

15import time 

16from collections.abc import Sequence 

17from multiprocessing import Process 

18 

19import grpc 

20 

21from couchers.constants import GRACEFUL_SHUTDOWN_TIMEOUT, SHUTDOWN_DEADLINE 

22from couchers.metrics import supervised_children_alive_gauge 

23 

24logger = logging.getLogger(__name__) 

25 

26_POLL_INTERVAL = 1.0 

27 

28 

29def catch_shutdown_signals() -> threading.Event: 

30 """ 

31 Returns an event that is set on SIGTERM or SIGINT. 

32 

33 Poll it with is_set() rather than calling wait() on the main thread: the handler runs on the main thread, and 

34 Event.set() deadlocks if the signal arrives while wait() holds the event's lock. 

35 """ 

36 event = threading.Event() 

37 signal.signal(signal.SIGTERM, lambda *_: event.set()) 

38 signal.signal(signal.SIGINT, lambda *_: event.set()) 

39 return event 

40 

41 

42def supervise(children: list[Process], parent_servers: Sequence[grpc.Server] = ()) -> Process | None: 

43 """Block until a shutdown signal or until a child dies, then drain everything. Returns the dead child.""" 

44 shutting_down = catch_shutdown_signals() 

45 

46 crashed: Process | None = None 

47 while not shutting_down.is_set(): 

48 supervised_children_alive_gauge.set(sum(child.is_alive() for child in children)) 

49 crashed = next((child for child in children if not child.is_alive()), None) 

50 if crashed is not None: 50 ↛ 53line 50 didn't jump to line 53 because the condition on line 50 was always true

51 logger.critical(f"Child {crashed.name} (pid {crashed.pid}) exited with code {crashed.exitcode}") 

52 break 

53 time.sleep(_POLL_INTERVAL) 

54 else: 

55 logger.info("Received shutdown signal, shutting down") 

56 

57 for child in children: 

58 if child.is_alive(): 

59 child.terminate() 

60 server_stopped = [server.stop(GRACEFUL_SHUTDOWN_TIMEOUT) for server in parent_servers] 

61 deadline = time.monotonic() + SHUTDOWN_DEADLINE 

62 for child in children: 

63 child.join(timeout=max(0.0, deadline - time.monotonic())) 

64 for stopped in server_stopped: 

65 stopped.wait(timeout=max(0.0, deadline - time.monotonic())) 

66 

67 # multiprocessing joins every remaining child when the parent exits, so a stuck one would hang us until docker 

68 # sends SIGKILL 

69 for child in children: 

70 if child.is_alive(): 

71 logger.error(f"Child {child.name} (pid {child.pid}) did not exit within {SHUTDOWN_DEADLINE}s, killing it") 

72 child.kill() 

73 child.join() 

74 

75 return crashed