From f62a62c027642435eb5a2cef147aafefee45f279 Mon Sep 17 00:00:00 2001 From: catborise Date: Wed, 25 Nov 2026 16:42:18 +0300 Subject: [PATCH] fix(operations): SIGTERM no longer deadlocks the job worker The worker's SIGTERM/SIGINT handler called stopping.set(). A signal runs its handler in the main thread, which spends its time inside stopping.wait() holding the event's lock; set() then waited for that lock forever and the worker never stopped (systemd or docker stop had to kill it, leaving jobs to recovery). The handler now sets the event from a thread of its own. A standalone repro hung 3 times in 300 signals before and none after. --- operations/worker.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/operations/worker.py b/operations/worker.py index 5abf79d2..c89e90ae 100644 --- a/operations/worker.py +++ b/operations/worker.py @@ -130,8 +130,13 @@ class Worker: except BlockingIOError: logger.error("Another worker runs on this host; exiting") return 1 - signal.signal(signal.SIGTERM, lambda *_: self.stopping.set()) - signal.signal(signal.SIGINT, lambda *_: self.stopping.set()) + # a signal runs its handler in this thread, which may be inside stopping.wait() holding the + # event's lock: set from a thread of its own, the event waits for that lock instead of deadlocking + def stop(*_): + threading.Thread(target=self.stopping.set, daemon=True).start() + + signal.signal(signal.SIGTERM, stop) + signal.signal(signal.SIGINT, stop) status = 0 try: while not acquire(self.identity, self.host):