Skip to content

The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.

JavaScript

Never let a dead worker keep the queue to itself (#72)

OsintMeThat56f0376f1c35
diff --git a/src/azimut/engine/workqueue.py b/src/azimut/engine/workqueue.py
index 072b9ae..fd1e163 100644
--- a/src/azimut/engine/workqueue.py
+++ b/src/azimut/engine/workqueue.py
@@ -112,6 +112,9 @@ def drain(case: "CaseType") -> int:
# -- the single background worker -----------------------------------------
+#: The one worker thread's name, which is also how a flag with nothing behind
+#: it is told from one a live thread is holding (`_reset_for_tests`).
+_WORKER_NAME = "azimut-worker"
_worker_lock = threading.Lock()
_pending: dict[str, "CaseType"] = {}
_worker_running = False
@@ -123,21 +126,39 @@ start_workers = True
def _run_loop() -> None:
global _worker_running
- while True:
- with _worker_lock:
- if not _pending:
+ # The normal end clears the flag under the same lock that finds the queue
+ # empty, so a `wake` in flight either sees a live worker or starts one. That
+ # atomicity is the point, and the `finally` below must not undo it.
+ handed_back = False
+ try:
+ while True:
+ with _worker_lock:
+ if not _pending:
+ _worker_running = False
+ handed_back = True
+ return
+ # Claimed before the drain, not after it. `wake` is a no-op while the
+ # case is already pending, so a job enqueued *during* the drain — past
+ # the point this pass read the queue — would have its mark removed by
+ # a pop on the way out and never be drained at all.
+ case_id = next(iter(_pending))
+ case = _pending.pop(case_id)
+ try:
+ drain(case)
+ except Exception: # a worker must never die on one case's failure
+ pass
+ finally:
+ # Any other way out of this thread — an interpreter shutdown, a
+ # `KeyboardInterrupt`, a `MemoryError`, anything `except Exception` does
+ # not catch — used to leave the flag set with nobody draining. `wake`
+ # then believed a worker was already on it and started none, so every
+ # thumbnail and every queued job after that point waited for a thread
+ # that no longer existed, until the process was restarted. Whatever the
+ # queue still holds is left where it is: the next `wake` starts a fresh
+ # worker, which picks it up.
+ if not handed_back:
+ with _worker_lock:
_worker_running = False
- return
- # Claimed before the drain, not after it. `wake` is a no-op while the
- # case is already pending, so a job enqueued *during* the drain — past
- # the point this pass read the queue — would have its mark removed by
- # a pop on the way out and never be drained at all.
- case_id = next(iter(_pending))
- case = _pending.pop(case_id)
- try:
- drain(case)
- except Exception: # a worker must never die on one case's failure
- pass
def wake(case: "CaseType") -> None:
@@ -151,7 +172,7 @@ def wake(case: "CaseType") -> None:
if _worker_running:
return
_worker_running = True
- threading.Thread(target=_run_loop, name="azimut-worker", daemon=True).start()
+ threading.Thread(target=_run_loop, name=_WORKER_NAME, daemon=True).start()
def recover_all() -> None:
@@ -174,6 +195,30 @@ def recover_all() -> None:
wake(case)
+def _reset_for_tests() -> None:
+ """Test seam: forget the queue, and the worker flag when nothing holds it.
+
+ Module state outlives a test, and two of them set the flag by hand. Under a
+ randomized order that leaked: a test that inherited `_worker_running = True`
+ with no thread behind it queued its work, was told a worker had it, and
+ waited out its whole budget for a drain that was never going to come.
+
+ The flag is only taken back when it is genuinely orphaned. A set flag with a
+ live thread behind it is not stale, it is true — clearing it there would let
+ a second worker start beside the first, and the pair would still be writing
+ when the temporary workspace is removed.
+ """
+ global _worker_running
+ with _worker_lock:
+ _pending.clear()
+ orphaned = not any(
+ thread.name == _WORKER_NAME and thread.is_alive()
+ for thread in threading.enumerate()
+ )
+ if orphaned:
+ _worker_running = False
+
+
def wait_until_idle(timeout: float = 5.0) -> bool:
"""Wait for the shared worker to finish its current queue, if any.
diff --git a/tests/conftest.py b/tests/conftest.py
index b05e7f9..defa1d0 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -48,6 +48,28 @@ def offline_reverse_geocode(monkeypatch):
geo._reset_pace()
[email protected](autouse=True)
+def a_worker_of_its_own():
+ """No test inherits the previous one's worker.
+
+ `workqueue` keeps its queue and its "a worker is running" flag on the module,
+ which outlives any one test, and two tests set that flag by hand. The suite
+ runs in a randomized order across xdist workers, so the orders that leak a
+ set flag with no live thread behind it come up now and then and not on
+ demand: `wake` believed a worker had the case, started none, and the test
+ waited its whole budget for a drain nobody was doing.
+
+ Set up before the fixtures that own a temporary workspace, so the state is
+ clean before a case exists at all. The worker is waited out first, because
+ clearing the flag under a live thread would let a second one start.
+ """
+ from azimut.engine import workqueue
+
+ workqueue.wait_until_idle(timeout=10)
+ workqueue._reset_for_tests()
+ yield
+
+
def _let_the_worker_finish() -> None:
"""Wait for the shared background worker before a throwaway workspace is
removed.
diff --git a/tests/test_workqueue.py b/tests/test_workqueue.py
index 7440284..dc4b891 100644
--- a/tests/test_workqueue.py
+++ b/tests/test_workqueue.py
@@ -135,3 +135,27 @@ def test_wait_until_idle_looks_once_more_after_the_deadline(case, monkeypatch):
monkeypatch.setattr(workqueue, "_worker_running", True)
assert workqueue.wait_until_idle(timeout=0) is False
+
+
+def test_a_worker_that_dies_hands_the_queue_back(case, monkeypatch):
+ """A thread lost to something no `except Exception` catches must not leave the
+ flag set.
+
+ `wake` starts nothing while it believes a worker is draining, so a flag left
+ behind by a dead thread stalled every queued job in the process — a thumbnail
+ that never appeared, with nothing to say why, until a restart.
+ """
+
+ def die(_case):
+ raise KeyboardInterrupt # not an Exception; the drain's own guard misses it
+
+ monkeypatch.setattr(workqueue, "drain", die)
+ monkeypatch.setitem(workqueue._pending, case.id, case)
+ monkeypatch.setattr(workqueue, "_worker_running", True)
+
+ with pytest.raises(KeyboardInterrupt):
+ workqueue._run_loop()
+
+ assert workqueue._worker_running is False
+ # and the case it never got to is still queued, for the worker after it
+ assert workqueue.wait_until_idle(timeout=0) is True