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."""