Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(queue): make QueueProcessorV2.stop() interrupt the loop wait (#4533)

LearningCircuitcea3e1da94e9
diff --git a/src/local_deep_research/web/queue/processor_v2.py b/src/local_deep_research/web/queue/processor_v2.py
index a089ca3cfb..5323bd5709 100644
--- a/src/local_deep_research/web/queue/processor_v2.py
+++ b/src/local_deep_research/web/queue/processor_v2.py
@@ -57,6 +57,12 @@ class QueueProcessorV2:
self.running = False
self.thread = None
self._loop_iteration = 0
+ # Wakes the loop out of its inter-iteration wait so stop() returns
+ # in milliseconds instead of blocking for up to check_interval.
+ # The test suite stops the processor after every app test
+ # (tests/conftest.py reset_singletons), so a blocking stop() adds
+ # ~check_interval seconds of teardown to each of those tests.
+ self._stop_event = threading.Event()
# Per-user settings will be retrieved from each user's database
# when processing their queue using SettingsManager
@@ -210,6 +216,10 @@ class QueueProcessorV2:
return
self.running = True
+ # Re-arm the wait for restart: stop() leaves the event set, and
+ # create_app() restarts this singleton after the test-suite
+ # teardown stops it.
+ self._stop_event.clear()
self.thread = threading.Thread(
target=self._process_queue_loop, daemon=True
)
@@ -219,6 +229,7 @@ class QueueProcessorV2:
def stop(self):
"""Stop the queue processor thread."""
self.running = False
+ self._stop_event.set()
if self.thread:
self.thread.join(timeout=10)
logger.info("Queue processor v2 stopped")
@@ -672,7 +683,10 @@ class QueueProcessorV2:
"periodic dead-thread sweep", exc_info=True
)
- time.sleep(self.check_interval)
+ # Event.wait, not time.sleep: stop() must be able to interrupt
+ # this pause, otherwise shutdown blocks for up to
+ # check_interval seconds.
+ self._stop_event.wait(self.check_interval)
def _process_user_queue(self, username: str, session_id: str) -> bool:
"""
diff --git a/tests/web/queue/test_processor_v2.py b/tests/web/queue/test_processor_v2.py
index 7a9da60051..53f80962b6 100644
--- a/tests/web/queue/test_processor_v2.py
+++ b/tests/web/queue/test_processor_v2.py
@@ -129,6 +129,74 @@ class TestQueueProcessorV2StartStop:
assert processor.running is False
processor.thread.join.assert_called_once_with(timeout=10)
+ def test_stop_interrupts_check_interval_wait(self):
+ """stop returns promptly while the loop sleeps between iterations.
+
+ The autouse reset_singletons fixture stops the processor after
+ every app test, so a stop() that blocks for check_interval adds
+ that many seconds of teardown to each of those tests (observed
+ as uniform 10.00s teardowns that pushed CI past its job timeout).
+ """
+ import time
+
+ from local_deep_research.web.queue.processor_v2 import (
+ QueueProcessorV2,
+ )
+
+ processor = QueueProcessorV2(check_interval=30)
+
+ # Wrap the real wait so the test knows when the loop has entered
+ # it — deterministic, unlike sleeping and hoping the first
+ # iteration has finished.
+ loop_entered_wait = threading.Event()
+ real_wait = processor._stop_event.wait
+
+ def tracking_wait(timeout=None):
+ loop_entered_wait.set()
+ return real_wait(timeout)
+
+ processor._stop_event.wait = tracking_wait
+ processor.start()
+ try:
+ assert loop_entered_wait.wait(timeout=10), (
+ "loop never reached its inter-iteration wait"
+ )
+
+ started = time.monotonic()
+ processor.stop()
+ elapsed = time.monotonic() - started
+
+ assert elapsed < 5, (
+ f"stop() blocked for {elapsed:.1f}s; the loop's "
+ "inter-iteration wait was not interrupted"
+ )
+ assert not processor.thread.is_alive()
+ finally:
+ processor.stop()
+
+ def test_start_after_stop_rearms_loop_wait(self):
+ """A restarted processor loops again instead of exiting instantly.
+
+ stop() leaves _stop_event set; start() must clear it or the
+ restarted loop's wait returns immediately every iteration
+ (busy-spin) — and create_app() does restart this singleton after
+ the test-suite teardown stops it.
+ """
+ from local_deep_research.web.queue.processor_v2 import (
+ QueueProcessorV2,
+ )
+
+ processor = QueueProcessorV2()
+ processor.start()
+ processor.stop()
+
+ with patch.object(processor, "_process_queue_loop"):
+ processor.start()
+ try:
+ assert not processor._stop_event.is_set()
+ finally:
+ processor.stop()
+
class TestQueueProcessorV2NotifyUserActivity:
"""Tests for notify_user_activity method."""
diff --git a/tests/web/queue/test_processor_v2_coverage.py b/tests/web/queue/test_processor_v2_coverage.py
index d8022b7009..c3ce4feef5 100644
--- a/tests/web/queue/test_processor_v2_coverage.py
+++ b/tests/web/queue/test_processor_v2_coverage.py
@@ -328,7 +328,7 @@ class TestProcessQueueLoopCleanup:
call_counts = {"iterations": 0}
- def fake_sleep(t):
+ def fake_wait(t):
call_counts["iterations"] += 1
if call_counts["iterations"] >= 7:
proc.running = False
@@ -337,7 +337,7 @@ class TestProcessQueueLoopCleanup:
mock_cleanup_dead = MagicMock()
with (
- patch(f"{MODULE}.time.sleep", side_effect=fake_sleep),
+ patch.object(proc._stop_event, "wait", side_effect=fake_wait),
patch(f"{MODULE}.logger"),
patch.object(proc, "_users_to_check", set()),
patch(
@@ -368,13 +368,13 @@ class TestProcessQueueLoopCleanup:
call_counts = {"n": 0}
- def fake_sleep(t):
+ def fake_wait(t):
call_counts["n"] += 1
if call_counts["n"] >= 2:
proc.running = False
with (
- patch(f"{MODULE}.time.sleep", side_effect=fake_sleep),
+ patch.object(proc._stop_event, "wait", side_effect=fake_wait),
patch(f"{MODULE}.logger"),
patch.object(proc, "_users_to_check", set()),
patch.dict(