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(searxng): skip in-use locks when the rate-limit tracker evicts at capacity (#5607)

Ehsancc4b30be82d9
diff --git a/changelog.d/5517.bugfix.md b/changelog.d/5517.bugfix.md
new file mode 100644
index 0000000000..a249873d82
--- /dev/null
+++ b/changelog.d/5517.bugfix.md
@@ -0,0 +1 @@
+Skip SearXNG rate-limit locks that are in use when the URL tracker evicts at capacity, so a held lock is not replaced by a fresh one that ignores the configured delay.
diff --git a/src/local_deep_research/web_search_engines/engines/_searxng_rate_limiter.py b/src/local_deep_research/web_search_engines/engines/_searxng_rate_limiter.py
index 677c0e47ef..23b25c18a4 100644
--- a/src/local_deep_research/web_search_engines/engines/_searxng_rate_limiter.py
+++ b/src/local_deep_research/web_search_engines/engines/_searxng_rate_limiter.py
@@ -40,14 +40,33 @@ def _normalize_url(url: str) -> str:
def _evict_stale_locks_unlocked() -> None:
"""Evict oldest entries when tracked URLs reach capacity.
+ A lock another thread is currently holding is never a candidate: dropping
+ it lets the next caller for that URL build a fresh lock and read no
+ timestamp, so the holder's delay stops applying. When every tracked lock
+ is held there is nothing to reclaim and the tracker grows past
+ ``MAX_TRACKED_URLS`` until one is released.
+
+ ``locked()`` is not a complete answer, and this function does not make it
+ one. ``respect_rate_limit`` fetches its lock from ``_get_url_lock`` under
+ ``_meta_lock`` and acquires it afterwards, so between those two steps the
+ object reports ``locked() == False`` and stays evictable. A URL evicted in
+ that window loses its timestamp and skips one delay. The window is narrower
+ than the one this check closes, which spans ``time.sleep(wait_time)``, and
+ closing it as well would mean holding ``_meta_lock`` across the sleep.
+
Must be called while holding ``_meta_lock``.
"""
if len(_url_locks) < MAX_TRACKED_URLS:
return
+ evictable = [url for url, lock in _url_locks.items() if not lock.locked()]
+ if not evictable:
+ logger.warning(
+ f"SearXNG rate limiter: all {len(_url_locks)} tracked URL locks "
+ "are in use, so none can be evicted at capacity"
+ )
+ return
# Remove oldest half of entries based on _url_last_request timestamp
- sorted_urls = sorted(
- _url_locks.keys(), key=lambda u: _url_last_request.get(u, 0.0)
- )
+ sorted_urls = sorted(evictable, key=lambda u: _url_last_request.get(u, 0.0))
to_remove = sorted_urls[: max(1, len(sorted_urls) // 2)]
for url in to_remove:
_url_locks.pop(url, None)
diff --git a/tests/web_search_engines/engines/test_search_engine_searxng.py b/tests/web_search_engines/engines/test_search_engine_searxng.py
index c0580178f9..60065a9fe0 100644
--- a/tests/web_search_engines/engines/test_search_engine_searxng.py
+++ b/tests/web_search_engines/engines/test_search_engine_searxng.py
@@ -565,6 +565,85 @@ class TestRateLimiting:
assert len(_searxng_rate_limiter._url_locks) <= 3
assert "http://localhost:8084" in _searxng_rate_limiter._url_locks
+ def test_eviction_skips_a_lock_another_thread_holds(self, monkeypatch):
+ """The oldest entry is held, so eviction must move on to the next one."""
+ import threading
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ for i in range(4):
+ _searxng_rate_limiter.respect_rate_limit(
+ f"http://localhost:808{i}", 0.1
+ )
+
+ held_url = "http://localhost:8080"
+ held_lock = _searxng_rate_limiter._url_locks[held_url]
+ acquired = threading.Event()
+ release = threading.Event()
+
+ def holder():
+ with held_lock:
+ acquired.set()
+ release.wait(10)
+
+ thread = threading.Thread(target=holder)
+ thread.start()
+ try:
+ assert acquired.wait(10)
+ _searxng_rate_limiter.respect_rate_limit(
+ "http://localhost:8084", 0.1
+ )
+ assert _searxng_rate_limiter._url_locks.get(held_url) is held_lock
+ assert (
+ "http://localhost:8081" not in _searxng_rate_limiter._url_locks
+ )
+ finally:
+ release.set()
+ thread.join(10)
+
+ def test_eviction_keeps_everything_when_all_locks_are_held(
+ self, monkeypatch
+ ):
+ """With no free entry the tracker grows past capacity rather than
+ dropping a lock that is in use."""
+ import threading
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls = [f"http://localhost:808{i}" for i in range(4)]
+ for url in urls:
+ _searxng_rate_limiter.respect_rate_limit(url, 0.1)
+
+ all_acquired = threading.Barrier(len(urls) + 1)
+ release = threading.Event()
+
+ def holder(url):
+ with _searxng_rate_limiter._url_locks[url]:
+ all_acquired.wait(10)
+ release.wait(10)
+
+ threads = [threading.Thread(target=holder, args=(url,)) for url in urls]
+ for thread in threads:
+ thread.start()
+ try:
+ all_acquired.wait(10)
+ _searxng_rate_limiter.respect_rate_limit(
+ "http://localhost:8084", 0.1
+ )
+ assert len(_searxng_rate_limiter._url_locks) == 5
+ for url in urls:
+ assert url in _searxng_rate_limiter._url_locks
+ finally:
+ release.set()
+ for thread in threads:
+ thread.join(10)
+
def test_rate_limit_multithreaded(self):
"""Multiple threads invoking respect_rate_limit concurrently are properly serialized."""
import concurrent.futures