From d69d149d97e7c65929498bce9a764ff4b76810fa Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 9 Nov 2026 23:04:09 +0300 Subject: [PATCH] fix(operations): a housekeeping pass is not started while one runs The worker started housekeeping every 10 minutes on its control threads whether or not the last pass had ended. A pass stuck on a host (the cleanup of an idle upload on a host that does not answer) took one more thread each time, and after four passes no control thread was left for cancels and recovery. The next pass now starts only after the last one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- operations/tests/test_worker.py | 14 ++++++++++++++ operations/worker.py | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/operations/tests/test_worker.py b/operations/tests/test_worker.py index a5baaf78..dc35cf8a 100644 --- a/operations/tests/test_worker.py +++ b/operations/tests/test_worker.py @@ -60,6 +60,20 @@ class WorkerTestCase(TransactionTestCase): def job(self, host, n): return self.submit("test.host", {"host": host, "n": n}, f"key-h{host}-{n:04d}") + def test_a_stuck_housekeeping_pass_is_not_started_again(self): + stuck, passes = threading.Event(), [] + + def housekeeping(): + passes.append(1) + stuck.wait(10) + with patch("operations.worker.HOUSEKEEPING_EVERY", 0), patch("operations.worker.housekeeping", housekeeping): + self.start() + self.assertTrue(wait_for(lambda: passes)) + time.sleep(0.5) # ten ticks + self.assertEqual(len(passes), 1) + stuck.set() + self.assertTrue(wait_for(lambda: len(passes) > 1)) + def test_a_stuck_host_takes_half_the_slots_and_one_progress_read(self): fakes.STUCK[1] = threading.Event() stuck = [self.job(1, n) for n in range(4)] diff --git a/operations/worker.py b/operations/worker.py index 1838e2ac..074d4608 100644 --- a/operations/worker.py +++ b/operations/worker.py @@ -111,6 +111,7 @@ class Worker: self.progress_pool = ThreadPoolExecutor(PROGRESS_THREADS, thread_name_prefix="wvc-progress") self.recovery = Scheduler(self) self._last = {"heartbeat": 0.0, "progress": 0.0, "housekeeping": 0.0} + self.housekeeping = None # the future of the last housekeeping pass # -- process ------------------------------------------------------------ @@ -194,7 +195,9 @@ class Worker: self._claim() if now - self._last["housekeeping"] >= HOUSEKEEPING_EVERY: self._last["housekeeping"] = now - self.control.submit(_guarded, housekeeping) + # one pass at a time: a pass stuck on a host (an upload's cleanup) holds one control thread, not all + if self.housekeeping is None or self.housekeeping.done(): + self.housekeeping = self.control.submit(_guarded, housekeeping) def _mine(self, pks): """This worker's active jobs among pks; used only inside fenced."""