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): stop the all-locks-held warning firing per new URL (#5663)

LearningCircuitacc33b28497b
diff --git a/changelog.d/5663.bugfix.md b/changelog.d/5663.bugfix.md
new file mode 100644
index 0000000000..af40c0dba7
--- /dev/null
+++ b/changelog.d/5663.bugfix.md
@@ -0,0 +1,7 @@
+The SearXNG rate limiter no longer repeats its capacity warning for every new instance URL. The old warning fired again on each new URL for as long as every tracked entry was held. It now fires once with exact counts -- how many of the tracked locks are evictable, out of how many, and the capacity it cannot get back under -- repeats at most every five minutes while the condition lasts, and is closed by a recovery line naming how many became evictable and how long the tracker was saturated. The two lines stay in order when threads race, so a recovery never appears without the warning it closes. Saturation is tracked as a state with hysteresis: leaving it takes several free slots rather than one, so a lock that is momentarily free between creation and use no longer counts as a recovery, and a later, genuinely separate exhaustion is still reported. Eviction also now prefers a lock that has already recorded a request over one that has never recorded one, so a request in flight is no longer the first entry dropped when the tracker is under pressure.
+
+This builds on #5688's `_UrlState` (one tracked entry per URL, carrying its lock and its timestamp together) rather than replacing it with the two separate `_url_locks`/`_url_last_request` maps this branch had used before merging main. The warning-dedup, hysteresis, and eviction-preference work above is unchanged; only the underlying tracking structure is #5688's.
+
+Timestamp writes remain on the shared `_UrlState` under its per-URL lock. A caller that obtained a state before eviction can still be followed by another caller holding that same object, so its timestamp must advance even after the map entry is removed. The attribute write preserves their configured delay without recreating the evicted entry; it needs no additional metadata-lock acquisition.
+
+Separately, and left unchanged by this PR: when every tracked lock is currently held, eviction returns without reclaiming anything, and the tracker still inserts the new entry, so `_url_state` can grow past `MAX_TRACKED_URLS` until a lock is released. This is pre-existing behavior, unrelated to the `_UrlState` merge, and is what the capacity warning above exists to surface rather than prevent.
diff --git a/docs/release_notes/1.10.6.md b/docs/release_notes/1.10.6.md
index 8d87cf5a38..b960bab7e5 100644
--- a/docs/release_notes/1.10.6.md
+++ b/docs/release_notes/1.10.6.md
@@ -101,7 +101,7 @@
that reports usage without a total is now counted as prompt + completion
rather than as zero tokens. ([#5623](https://github.com/LearningCircuit/local-deep-research/pull/5623))
- Clarified the help text and tooltips on the embedding settings page so they match which chunk-size, chunk-overlap, and text-separator settings each text splitter type actually uses. Previously, the descriptions implied every splitter honored every setting — for example, the chunk-size field was described as "characters per chunk" even though the `token` and `sentence` splitters interpret it as tokens, and the `semantic` splitter silently forces chunk overlap to 0. ([#5660](https://github.com/LearningCircuit/local-deep-research/pull/5660))
-- The SearXNG rate limiter now keeps each URL's lock and its last-request time in one tracked entry, so a URL evicted while one of its requests is still in flight can no longer leave its timestamp behind. Tracked state is now bounded by `MAX_TRACKED_URLS` as the module documented, rather than only the locks being bounded. ([#5669](https://github.com/LearningCircuit/local-deep-research/pull/5669))
+- The SearXNG rate limiter now keeps each URL's lock and its last-request time in one tracked entry, so a URL evicted while one of its requests is still in flight can no longer leave its timestamp behind. `MAX_TRACKED_URLS` remains an eviction target rather than a hard bound, so the tracker can still grow past it while every tracked lock is held. ([#5669](https://github.com/LearningCircuit/local-deep-research/pull/5669))
- Fixed three downstream effects of the chunk-anchor citation URLs introduced in #5381.
`fetch_content` can read library documents again: the RAG engines now cite `/library/document/<id>/chunks#chunk-<n>`, but the fetch tool's library resolver only matched an optional `/pdf` suffix and never stripped the fragment, so every such citation fell through to the egress gate and was rejected as `unsupported_scheme`. The resolver now accepts the `chunks` route and drops the fragment before matching.
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 64916600ed..1ae107bd38 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
@@ -14,9 +14,13 @@ The locking pattern mirrors other per-key locks in the codebase
per-URL lock, and that per-URL lock guards updates to the timestamp.
Note on memory:
-`_url_state` is bounded to a maximum capacity (`MAX_TRACKED_URLS`). When
-capacity is reached, stale/least-recently-used entries are evicted under
-`_meta_lock`.
+`MAX_TRACKED_URLS` is an eviction target, not a hard bound: the tracker can
+grow while all existing locks are held. At capacity, evictable entries are
+removed under `_meta_lock`. Each URL has exactly one tracked `_UrlState` carrying both
+its lock and its timestamp (#5688), so a timestamp write is an attribute
+write on a reference the caller already holds -- it cannot recreate an
+entry eviction has removed, the way writing into a *second*, separately
+keyed map could.
"""
import threading
@@ -40,29 +44,293 @@ class _UrlState:
_meta_lock = threading.Lock()
_url_state: dict[str, _UrlState] = {}
+# How long the "nothing to evict" warning stays quiet before repeating while
+# the condition persists.
+ALL_HELD_REWARN_SECONDS = 300.0
+
+# Hysteresis. Leaving the all-held state needs several free slots, not merely
+# one. A single evictable entry at capacity is usually just a lock sitting
+# between ``_get_url_state`` and its caller's ``with state.lock:`` -- treating
+# that as recovery would clear the throttle and let the warning fire again on
+# the very next URL, which is the behaviour this state exists to prevent.
+#
+# The divisor scales the recovery band with capacity. Below 20 it supplies
+# at most one slot; requiring two prevents a briefly free lock from clearing
+# the warning between lookup and acquisition. The threshold is capped at
+# capacity so recovery remains possible for a one-slot tracker.
+ALL_HELD_RECOVERY_HEADROOM_DIVISOR = 10
+ALL_HELD_RECOVERY_MIN_SLOTS = 2
+
+# When that warning was last emitted, or None when the condition is not
+# active. A timestamp rather than a flag so a condition that never clears
+# keeps reporting instead of going silent after one line. Guarded by
+# ``_meta_lock``.
+_all_held_warned_at: float | None = None
+# Onset of the current saturated episode. Distinct from
+# ``_all_held_warned_at``, which is rewritten on every re-warn so it can keep
+# governing ``ALL_HELD_REWARN_SECONDS``. Deriving the duration from that
+# would report the time since the last re-warn rather than the time at
+# capacity -- a twenty-minute episode with three re-warns would claim ~300s.
+_all_held_since: float | None = None
+
+# Orders the log lines the state transitions produce. The decision is made
+# under ``_meta_lock`` but emitted after releasing it, so two threads can
+# reach ``_emit_capacity_log`` in the opposite order to the decisions they
+# carry. ``_emit_lock`` serializes the emissions and ``_emitted_generation``
+# drops any record a newer transition has already overtaken. Never acquired
+# while holding ``_meta_lock``. ``reset_for_tests`` is the one path that takes
+# ``_meta_lock`` while holding it; the hierarchy stays acyclic because no
+# thread ever waits on ``_emit_lock`` while holding ``_meta_lock``.
+_emit_lock = threading.Lock()
+_state_generation = 0
+_emitted_generation = 0
+
def _normalize_url(url: str) -> str:
"""Normalize instance URL for keying rate limits."""
return url.rstrip("/")
+def _recovery_headroom_slots() -> int:
+ """Free slots required before the all-held state is considered over.
+
+ Read from ``MAX_TRACKED_URLS`` on each call so tests that shrink the
+ capacity get a proportional threshold instead of a stale constant.
+
+ Capped at the capacity itself: while anything is tracked, headroom can
+ never exceed ``MAX_TRACKED_URLS``, so a larger threshold would make
+ recovery unreachable and pin the state as warned for good.
+ """
+ return min(
+ max(MAX_TRACKED_URLS, 0),
+ max(
+ ALL_HELD_RECOVERY_MIN_SLOTS,
+ MAX_TRACKED_URLS // ALL_HELD_RECOVERY_HEADROOM_DIVISOR,
+ ),
+ )
+
+
+def _count_evictable_unlocked(limit: int) -> int:
+ """Count tracked locks nobody holds, counting no further than ``limit``.
+
+ The result is exact when it comes back below ``limit`` and a lower bound
+ when it reaches it. Callers pass the smallest limit that still separates
+ the branches they have to choose between, which caps the *free* entries
+ counted -- not the entries walked. A held lock is stepped over without
+ advancing the count, so the early exit only pays off while free entries
+ are plentiful; a tracker whose locks are mostly held is scanned in full.
+ That is the saturated state this reporting exists for, and while it lasts
+ the scan runs on every ``_get_url_state`` under ``_meta_lock``. It stays
+ affordable because the tracker is keyed by SearXNG instance URL, so it
+ holds a handful of entries in practice rather than ``MAX_TRACKED_URLS``
+ of them.
+
+ Must be called while holding ``_meta_lock``.
+ """
+ if limit <= 0:
+ return 0
+ count = 0
+ for state in _url_state.values():
+ if not state.lock.locked():
+ count += 1
+ if count >= limit:
+ break
+ return count
+
+
+def _capacity_pressure_unlocked() -> (
+ tuple[bool, str, tuple[object, ...], int] | None
+):
+ """Update the all-held state and return the line it wants logged.
+
+ Returns ``None`` when nothing needs saying, otherwise
+ ``(is_warning, message, args, generation)`` for the caller to emit
+ *after* releasing ``_meta_lock``. The database sink queues the record
+ from the worker threads this code runs on rather than writing it inline
+ (``utilities/log_utils.py``), but the formatting, the queueing and every
+ other sink still run on the calling thread, and ``_meta_lock`` serializes
+ every lock lookup in the process. The emission is moved out to keep that
+ critical section short and free of I/O. ``generation`` increases with
+ every transition so a thread descheduled between deciding and emitting
+ cannot publish a line a newer transition has already superseded (see
+ ``_emit_capacity_log``).
+
+ The decision runs on every ``_get_url_state`` call rather than only when
+ an entry is created, so an exhaustion that clears while traffic continues
+ on already-tracked URLs is observed -- otherwise a later, genuinely
+ distinct episode would be silently swallowed by the re-warn interval.
+
+ Saturation is measured as *headroom*: the number of entries the tracker
+ can still admit without dropping a lock that is in use, i.e. free
+ capacity plus evictable entries. Warning at ``headroom <= 0`` and
+ recovering only at ``headroom >= _recovery_headroom_slots()`` gives the
+ state hysteresis, so one transient entry cannot flip it.
+
+ Must be called while holding ``_meta_lock``.
+ """
+ global _all_held_warned_at, _all_held_since, _state_generation
+
+ tracked = len(_url_state)
+ if _all_held_warned_at is None and tracked < MAX_TRACKED_URLS:
+ # Free capacity and no outstanding warning: nothing can change, and
+ # the common path avoids the scan below.
+ return None
+
+ slots = _recovery_headroom_slots()
+ # Once headroom is provably this large no further free lock can change
+ # which branch runs, so the count stops there. Unwarned, only the
+ # ``headroom <= 0`` boundary matters and one free lock settles it.
+ needed = slots if _all_held_warned_at is not None else 1
+ evictable = _count_evictable_unlocked(tracked - MAX_TRACKED_URLS + needed)
+ headroom = MAX_TRACKED_URLS - tracked + evictable
+
+ if evictable < tracked and headroom <= 0:
+ # ``evictable < tracked`` keeps the claim honest: at least one lock
+ # really is in use. Without it a capacity of zero would warn about a
+ # lone free, unheld lock on the very first lookup.
+ now = time.monotonic()
+ if (
+ _all_held_warned_at is None
+ or now - _all_held_warned_at >= ALL_HELD_REWARN_SECONDS
+ ):
+ if _all_held_warned_at is None:
+ # Entry to the episode. Re-warns rewrite the timer below but
+ # must not move the onset, or the recovery line would report
+ # the re-warn interval instead of the real duration.
+ _all_held_since = now
+ _all_held_warned_at = now
+ _state_generation += 1
+ # ``headroom <= 0`` is exactly ``tracked - evictable >=
+ # MAX_TRACKED_URLS``: evicting every free entry still would not
+ # get back under the limit. Some of them may well be evictable,
+ # so the line reports both counts instead of claiming none are.
+ return (
+ True,
+ "SearXNG rate limiter: only {} of {} tracked URL locks are "
+ "evictable, not enough to bring the tracker back under its "
+ "capacity of {}",
+ (evictable, tracked, MAX_TRACKED_URLS),
+ _state_generation,
+ )
+ return None
+
+ if _all_held_warned_at is None:
+ return None
+ if tracked and headroom < slots:
+ # Still saturated, just not to the last slot. Stay warned so the
+ # re-warn interval keeps governing.
+ return None
+
+ # ``_all_held_since`` is written alongside ``_all_held_warned_at`` on entry,
+ # so it is set here; the fallback only guards a torn state. Compare with
+ # ``is not None`` rather than truthiness -- a 0.0 onset is a real reading.
+ onset = (
+ _all_held_since if _all_held_since is not None else _all_held_warned_at
+ )
+ held_for = time.monotonic() - onset
+ _all_held_warned_at = None
+ _all_held_since = None
+ _state_generation += 1
+ # The bounded count above is a lower bound once it hits its limit, and
+ # this line quotes real numbers, so finish counting. Once per transition,
+ # not once per call.
+ evictable = _count_evictable_unlocked(tracked)
+ return (
+ False,
+ "SearXNG rate limiter: {} of {} tracked URL locks are evictable "
+ "again after {:.1f}s at capacity",
+ (evictable, tracked, held_for),
+ _state_generation,
+ )
+
+
+def _emit_capacity_log(
+ pending: tuple[bool, str, tuple[object, ...], int] | None,
+) -> None:
+ """Emit the record ``_capacity_pressure_unlocked`` produced, if any.
+
+ Call with ``_meta_lock`` released: the emission is out here to keep the
+ meta-lock's critical section short and free of I/O, not because the sink
+ blocks (see ``_capacity_pressure_unlocked``).
+
+ ``_emit_lock`` puts the lines back in the order their transitions were
+ decided, and a record a newer transition has already overtaken is dropped
+ rather than logged late. Emitting it late would leave an operator reading
+ "evictable again" as the last word on a tracker that is in fact saturated
+ and warned -- with the next warning then suppressed for
+ ``ALL_HELD_REWARN_SECONDS``, or indefinitely if headroom settles inside
+ the hysteresis band.
+
+ A recovery is held to the same standard in the other direction: it is
+ emitted only when the transition it closes has already been emitted. A
+ recovery that overtakes its own still-unemitted warning would otherwise
+ print "evictable again" with nothing preceding it to recover from, and
+ would stamp that warning away as stale on the way past. Dropping the
+ recovery instead leaves the warning to be emitted on its own, which reads
+ as an episode still open rather than one that never happened.
+ """
+ global _emitted_generation
+
+ if pending is None:
+ return
+ is_warning, message, args, generation = pending
+ with _emit_lock:
+ if generation <= _emitted_generation:
+ return
+ if not is_warning and generation > _emitted_generation + 1:
+ return
+ _emitted_generation = generation
+ if is_warning:
+ logger.warning(message, *args)
+ else:
+ logger.info(message, *args)
+
+
+def _eviction_sort_key(state: "_UrlState") -> float:
+ """Order entries for eviction: oldest genuine timestamp first.
+
+ ``last_request == 0.0`` marks a URL whose ``_UrlState`` has been created
+ but has not recorded a request yet. That is reachable and unlocked: an
+ entry a *different* thread just created shows up this way for the
+ window between ``_get_url_state`` returning it and that thread's
+ ``respect_rate_limit`` reaching ``with state.lock:`` -- the same window
+ ``_evict_stale_locks_unlocked`` documents as leaving the entry evictable.
+ (It is not reachable for a request whose delay is still being applied:
+ that path only sleeps when ``last_request > 0``, which never hits this
+ branch, and it holds ``state.lock`` for the whole sleep, which the
+ caller's lock-held check excludes regardless of this key.)
+
+ It sorts last, not first, so a just-created entry with no timestamp yet
+ is never the preferred eviction victim; defaulting it to the front would
+ make the newest entries the ones eviction reaches for first, ahead of
+ entries that actually went stale.
+ """
+ return state.last_request if state.last_request > 0.0 else float("inf")
+
+
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
+ it lets the next caller for that URL build a fresh state 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.
+ ``MAX_TRACKED_URLS`` until one is released. Reporting that condition is
+ ``_capacity_pressure_unlocked``'s job, not this function's.
``locked()`` is not a complete answer, and this function does not make it
- one. ``respect_rate_limit`` fetches its state from ``_get_url_state`` under
- ``_meta_lock`` and acquires the lock 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.
+ one. ``respect_rate_limit`` fetches its state from ``_get_url_state``
+ under ``_meta_lock`` and acquires the lock 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)``.
+
+ Closing it too is possible without serializing the sleeps -- pin the entry
+ with a refcount incremented under ``_meta_lock`` in ``_get_url_state`` and
+ decremented after the caller releases, and skip pinned entries here -- but
+ that is more machinery than the residual window costs: one skipped delay
+ for one URL, self-healing on that URL's next request.
Must be called while holding ``_meta_lock``.
"""
@@ -72,13 +340,11 @@ def _evict_stale_locks_unlocked() -> None:
url for url, state in _url_state.items() if not state.lock.locked()
]
if not evictable:
- logger.warning(
- f"SearXNG rate limiter: all {len(_url_state)} tracked URL locks "
- "are in use, so none can be evicted at capacity"
- )
return
- # Remove oldest half of entries based on their last_request timestamp
- sorted_urls = sorted(evictable, key=lambda u: _url_state[u].last_request)
+ # Remove oldest half of entries based on _eviction_sort_key.
+ sorted_urls = sorted(
+ evictable, key=lambda u: _eviction_sort_key(_url_state[u])
+ )
to_remove = sorted_urls[: max(1, len(sorted_urls) // 2)]
for url in to_remove:
_url_state.pop(url, None)
@@ -95,7 +361,9 @@ def _get_url_state(normalized_url: str) -> _UrlState:
_evict_stale_locks_unlocked()
state = _UrlState()
_url_state[normalized_url] = state
- return state
+ pending = _capacity_pressure_unlocked()
+ _emit_capacity_log(pending)
+ return state
def respect_rate_limit(instance_url: str, delay_seconds: float) -> None:
@@ -122,11 +390,31 @@ def respect_rate_limit(instance_url: str, delay_seconds: float) -> None:
)
time.sleep(wait_time)
now = time.monotonic()
- # Attribute write on a held reference: an evicted entry stays evicted.
+ # Other callers may still hold this state after eviction. Keep their
+ # timing current under the shared lock; an attribute write cannot
+ # recreate the removed dictionary entry.
state.last_request = now
def reset_for_tests() -> None:
- """Clear all tracked state. Intended for unit tests only."""
- with _meta_lock:
- _url_state.clear()
+ """Clear all tracked state. Intended for unit tests only.
+
+ Both generation counters are rewound inside one ``_emit_lock``
+ section, so a transition decided *after* the reset cannot land between
+ two separate rewinds and be stamped away. It does not order a record
+ decided *before* the reset: such a record still carries a generation
+ above the rewound counter and will stamp ``_emitted_generation`` ahead
+ of ``_state_generation``, dropping transitions until it catches up.
+ Closing that would mean holding ``_emit_lock`` across the decision,
+ which is the serialisation moving emission out of ``_meta_lock``
+ exists to avoid. Callers must not reset while traffic is in flight.
+ """
+ global _all_held_warned_at, _all_held_since
+ global _state_generation, _emitted_generation
+ with _emit_lock:
+ with _meta_lock:
+ _url_state.clear()
+ _all_held_warned_at = None
+ _all_held_since = None
+ _state_generation = 0
+ _emitted_generation = 0
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 3c3d5e8f0a..dba81cd289 100644
--- a/tests/web_search_engines/engines/test_search_engine_searxng.py
+++ b/tests/web_search_engines/engines/test_search_engine_searxng.py
@@ -378,6 +378,45 @@ class TestIsValidSearchResult:
assert engine._is_valid_search_result(None) is False
+# Substrings identifying the two capacity-state lines the SearXNG rate
+# limiter emits, kept here so a wording change touches one place.
+SATURATION_WARNING = "not enough to bring the tracker back under"
+RECOVERY_LOG = "evictable again"
+
+
+def _track_urls(limiter, count=4, port_base=8080):
+ """Put ``count`` URLs into the tracker through the public entry point.
+
+ Returns them in creation order, so ``urls[0]`` is the oldest entry.
+ """
+ urls = [f"http://localhost:{port_base + i}" for i in range(count)]
+ for url in urls:
+ limiter.respect_rate_limit(url, 0.1)
+ return urls
+
+
+def _hold_every_lock(limiter, urls):
+ """Acquire the tracked lock of every URL in ``urls``, in order.
+
+ Returns the locks so the caller can release them. A test that releases
+ some of them mid-body is expected to guard its own cleanup.
+ """
+ held = [limiter._url_state[url].lock for url in urls]
+ for lock in held:
+ assert lock.acquire(timeout=10)
+ return held
+
+
+def _saturate(limiter, count=4, port_base=8080):
+ """Track ``count`` URLs and hold every one of their locks.
+
+ The starting point of most of the capacity-state tests: the tracker sits
+ at the capacity the test monkeypatched, with nothing evictable.
+ """
+ urls = _track_urls(limiter, count, port_base)
+ return urls, _hold_every_lock(limiter, urls)
+
+
class TestRateLimiting:
"""Tests for rate limiting functionality."""
@@ -399,9 +438,13 @@ class TestRateLimiting:
)
_searxng_rate_limiter.respect_rate_limit("http://localhost:8080", 0.5)
- state = _searxng_rate_limiter._url_state.get("http://localhost:8080")
- assert state is not None
- assert state.last_request > 0
+ assert "http://localhost:8080" in _searxng_rate_limiter._url_state
+ assert (
+ _searxng_rate_limiter._url_state[
+ "http://localhost:8080"
+ ].last_request
+ > 0
+ )
_searxng_rate_limiter.reset_for_tests()
assert len(_searxng_rate_limiter._url_state) == 0
@@ -577,8 +620,7 @@ class TestRateLimiting:
)
held_url = "http://localhost:8080"
- held_state = _searxng_rate_limiter._url_state[held_url]
- held_lock = held_state.lock
+ held_lock = _searxng_rate_limiter._url_state[held_url].lock
acquired = threading.Event()
release = threading.Event()
@@ -594,7 +636,8 @@ class TestRateLimiting:
_searxng_rate_limiter.respect_rate_limit(
"http://localhost:8084", 0.1
)
- assert _searxng_rate_limiter._url_state.get(held_url) is held_state
+ held_state = _searxng_rate_limiter._url_state.get(held_url)
+ assert held_state is not None and held_state.lock is held_lock
assert (
"http://localhost:8081" not in _searxng_rate_limiter._url_state
)
@@ -614,16 +657,17 @@ class TestRateLimiting:
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)
+ urls = _track_urls(_searxng_rate_limiter)
all_acquired = threading.Barrier(len(urls) + 1)
release = threading.Event()
def holder(url):
with _searxng_rate_limiter._url_state[url].lock:
- all_acquired.wait(10)
+ try:
+ all_acquired.wait(10)
+ except threading.BrokenBarrierError:
+ return
release.wait(10)
threads = [threading.Thread(target=holder, args=(url,)) for url in urls]
@@ -642,56 +686,948 @@ class TestRateLimiting:
for thread in threads:
thread.join(10)
- def test_eviction_mid_call_leaves_no_tracked_entry_behind(
- self, monkeypatch
+ def test_all_locks_held_warns_once_then_notes_recovery(
+ self, monkeypatch, loguru_caplog
):
- """A URL evicted while its call is in flight stays evicted."""
- import threading
+ """The all-held warning is a state transition, not a per-call log."""
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ try:
+ with loguru_caplog.at_level("INFO"):
+ for _ in range(3):
+ _searxng_rate_limiter._get_url_state(
+ "http://localhost:8084"
+ )
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+ assert RECOVERY_LOG not in loguru_caplog.text
+ finally:
+ for lock in held:
+ lock.release()
+
+ with loguru_caplog.at_level("INFO"):
+ _searxng_rate_limiter._get_url_state(urls[0])
+ assert RECOVERY_LOG in loguru_caplog.text
+ assert _searxng_rate_limiter._all_held_warned_at is None
+
+ def test_all_locks_held_warns_again_once_the_interval_elapses(
+ self, monkeypatch, loguru_caplog
+ ):
+ """A condition that never clears must not go silent after one line."""
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+ monkeypatch.setattr(
+ _searxng_rate_limiter, "ALL_HELD_REWARN_SECONDS", 0.0
+ )
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ try:
+ with loguru_caplog.at_level("WARNING"):
+ for _ in range(2):
+ _searxng_rate_limiter._get_url_state(
+ "http://localhost:8084"
+ )
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 2
+ finally:
+ for lock in held:
+ lock.release()
+
+ def test_transient_evictable_entry_is_not_a_recovery(
+ self, monkeypatch, loguru_caplog
+ ):
+ """A lock between publication and acquisition must not clear the state.
+
+ ``_get_url_state`` publishes a lock before its caller acquires it, so at
+ capacity a single entry routinely reports ``locked() == False`` for the
+ length of that window while every other lock is genuinely in use.
+ Reading that one free slot as a recovery would clear the throttle and
+ let the very next call warn again.
+
+ The whole cycle runs on lookups of URLs that are already tracked, so it
+ also pins the decision to every ``_get_url_state`` call rather than only
+ to the calls that create a lock.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 20)
+ assert _searxng_rate_limiter._recovery_headroom_slots() == 2
+
+ urls, held = _saturate(_searxng_rate_limiter, 20, 9000)
+ try:
+ with loguru_caplog.at_level("INFO"):
+ # At capacity with every lock in use. No new URL is involved:
+ # a lookup of an already-tracked URL has to see this.
+ _searxng_rate_limiter._get_url_state(urls[0])
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+
+ # That URL's holder finishes, and the next caller for it picks
+ # the lock up again from _get_url_state. For the length of that
+ # window the entry is evictable: one free slot out of twenty,
+ # with nineteen locks still held. Not a recovery.
+ held[0].release()
+ reacquired = _searxng_rate_limiter._get_url_state(urls[0]).lock
+ assert reacquired is held[0]
+ assert RECOVERY_LOG not in loguru_caplog.text
+ assert _searxng_rate_limiter._all_held_warned_at is not None
+
+ # The window closes and the tracker is saturated again, which
+ # must not read as a fresh episode either.
+ assert reacquired.acquire(timeout=10)
+ _searxng_rate_limiter._get_url_state(urls[1])
+ assert RECOVERY_LOG not in loguru_caplog.text
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+ assert _searxng_rate_limiter._all_held_warned_at is not None
+ finally:
+ # held[0] is released and reacquired mid-test, so an assertion
+ # failing inside that window would otherwise be masked by
+ # ``RuntimeError: release unlocked lock`` from this cleanup.
+ for lock in held:
+ if lock.locked():
+ lock.release()
+
+ def test_partial_recovery_below_the_threshold_stays_warned(
+ self, monkeypatch, loguru_caplog
+ ):
+ """Two evictable entries at one over capacity give only one free slot.
+
+ Recovery needs ``_recovery_headroom_slots()`` of headroom, so a
+ margin that thin keeps the state warned -- and therefore keeps the
+ re-warn interval, rather than the next new URL, in charge.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 20)
+ assert _searxng_rate_limiter._recovery_headroom_slots() == 2
+
+ urls, held = _saturate(_searxng_rate_limiter, 20, 9000)
+ released = []
+ try:
+ with loguru_caplog.at_level("INFO"):
+ _searxng_rate_limiter._get_url_state("http://localhost:9900")
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+
+ # One lock comes back; with 9900's still-unacquired lock that
+ # is one slot of headroom out of twenty.
+ held[0].release()
+ released.append(held[0])
+ _searxng_rate_limiter._get_url_state(urls[1])
+ assert RECOVERY_LOG not in loguru_caplog.text
+ assert _searxng_rate_limiter._all_held_warned_at is not None
+
+ # Three more make the margin real.
+ for lock in held[1:4]:
+ lock.release()
+ released.append(lock)
+ _searxng_rate_limiter._get_url_state(urls[4])
+ assert RECOVERY_LOG in loguru_caplog.text
+ assert _searxng_rate_limiter._all_held_warned_at is None
+ finally:
+ for lock in held:
+ if lock not in released:
+ lock.release()
+
+ def test_second_exhaustion_episode_is_logged_within_the_interval(
+ self, monkeypatch, loguru_caplog
+ ):
+ """A genuinely distinct episode is not swallowed by the re-warn timer.
+
+ The interval only suppresses repeats of an episode that never cleared.
+ Once the condition is observed to have cleared, the next exhaustion is
+ a new event and must be reported immediately.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls = _track_urls(_searxng_rate_limiter)
+
+ with loguru_caplog.at_level("INFO"):
+ held = _hold_every_lock(_searxng_rate_limiter, urls)
+ try:
+ _searxng_rate_limiter._get_url_state("http://localhost:8084")
+ finally:
+ for lock in held:
+ lock.release()
+
+ # The condition clears while traffic continues on tracked URLs.
+ _searxng_rate_limiter._get_url_state(urls[0])
+ assert _searxng_rate_limiter._all_held_warned_at is None
+
+ # A second, unrelated exhaustion, well inside ALL_HELD_REWARN_SECONDS.
+ episode_two = [
+ _searxng_rate_limiter._url_state[url].lock
+ for url in list(_searxng_rate_limiter._url_state)
+ ]
+ for lock in episode_two:
+ assert lock.acquire(timeout=10)
+ try:
+ _searxng_rate_limiter._get_url_state("http://localhost:8085")
+ finally:
+ for lock in episode_two:
+ lock.release()
+
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 2
+
+ def test_recovery_log_reports_counts_and_duration(
+ self, monkeypatch, loguru_caplog
+ ):
+ """The recovery line carries the same kind of numbers as the warning."""
+ import re
+
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ try:
+ _searxng_rate_limiter._get_url_state("http://localhost:8084")
+ finally:
+ for lock in held:
+ lock.release()
+
+ with loguru_caplog.at_level("INFO"):
+ _searxng_rate_limiter._get_url_state(urls[0])
+
+ assert re.search(
+ r"5 of 5 tracked URL locks are evictable again after \d+\.\d+s",
+ loguru_caplog.text,
+ )
+
+ def test_recovery_duration_spans_the_episode_not_the_last_rewarn(
+ self, monkeypatch, loguru_caplog
+ ):
+ """held_for measures time at capacity, not time since the re-warn.
+
+ ``_all_held_warned_at`` is rewritten on every re-warn so it can keep
+ governing ``ALL_HELD_REWARN_SECONDS``. Deriving the duration from it
+ would make a long episode punctuated by re-warns report roughly the
+ re-warn interval instead of its real length.
+ """
+ import re
+ import time
+ from types import SimpleNamespace
+
from local_deep_research.web_search_engines.engines import (
_searxng_rate_limiter,
)
monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+ monkeypatch.setattr(
+ _searxng_rate_limiter, "ALL_HELD_REWARN_SECONDS", 0.0
+ )
+
+ clock = [1000.0]
+ # Rebind the module's ``time`` name rather than setting ``monotonic``
+ # on ``_searxng_rate_limiter.time`` -- that attribute *is* the stdlib
+ # ``time`` module, so patching through it swaps the clock out from
+ # under every other thread in the process for the test's duration.
+ monkeypatch.setattr(
+ _searxng_rate_limiter,
+ "time",
+ SimpleNamespace(monotonic=lambda: clock[0], sleep=time.sleep),
+ )
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ try:
+ # Onset, then two re-warns 100s apart. The last re-warn lands at
+ # t=1200, so a timer-derived duration would report ~0s.
+ _searxng_rate_limiter._get_url_state("http://localhost:8084")
+ clock[0] = 1100.0
+ _searxng_rate_limiter._get_url_state("http://localhost:8085")
+ clock[0] = 1200.0
+ _searxng_rate_limiter._get_url_state("http://localhost:8086")
+ finally:
+ for lock in held:
+ lock.release()
+
+ clock[0] = 1300.0
+ with loguru_caplog.at_level("INFO"):
+ _searxng_rate_limiter._get_url_state(urls[0])
+
+ match = re.search(
+ r"evictable again after (\d+\.\d+)s", loguru_caplog.text
+ )
+ assert match, loguru_caplog.text
+ assert float(match.group(1)) == 300.0
+
+ def test_capacity_logs_are_emitted_outside_the_meta_lock(self, monkeypatch):
+ """The capacity lines are written with ``_meta_lock`` released."""
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls = _track_urls(_searxng_rate_limiter)
+
+ meta_lock_held_during_emit = []
+
+ class _RecordingLogger:
+ def _record(self, *args, **kwargs):
+ meta_lock_held_during_emit.append(
+ _searxng_rate_limiter._meta_lock.locked()
+ )
+
+ warning = _record
+ info = _record
+
+ monkeypatch.setattr(_searxng_rate_limiter, "logger", _RecordingLogger())
+
+ held = _hold_every_lock(_searxng_rate_limiter, urls)
+ try:
+ _searxng_rate_limiter._get_url_state("http://localhost:8084")
+ finally:
+ for lock in held:
+ lock.release()
+ _searxng_rate_limiter._get_url_state(urls[0])
+
+ assert meta_lock_held_during_emit == [False, False]
+
+ def test_zero_capacity_warns_only_about_locks_actually_in_use(
+ self, monkeypatch, loguru_caplog
+ ):
+ """The warning never describes a table with nothing in use.
+
+ ``headroom <= 0`` alone is not enough: at a capacity of zero the very
+ first lookup satisfies it while the one lock it just created is free
+ and unheld. The warning speaks about locks that are in use, so it
+ fires only when at least one of them is.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 0)
+
+ # An empty tracker has nothing to report, whatever the capacity says.
+ with _searxng_rate_limiter._meta_lock:
+ assert _searxng_rate_limiter._capacity_pressure_unlocked() is None
+
+ with loguru_caplog.at_level("WARNING"):
+ lock = _searxng_rate_limiter._get_url_state(
+ "http://localhost:8080"
+ ).lock
+
+ assert "tracked URL locks" not in loguru_caplog.text
+
+ # A lock that really is held is a different matter, and still warns.
+ assert lock.acquire(timeout=10)
+ try:
+ with loguru_caplog.at_level("WARNING"):
+ _searxng_rate_limiter._get_url_state("http://localhost:8080")
+ assert (
+ "only 0 of 1 tracked URL locks are evictable"
+ in loguru_caplog.text
+ )
+ finally:
+ lock.release()
+
+ def test_eviction_window_leaves_no_orphaned_timestamp(self, monkeypatch):
+ """A URL evicted before its timestamp is written must not leak a key.
+
+ Each URL carries exactly one ``_UrlState`` (#5688), so eviction pops
+ one entry from ``_url_state`` and a timestamp write for an
+ already-evicted URL is an attribute write on the orphaned state the
+ caller still holds -- it cannot recreate the dict entry eviction just
+ removed.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
- victim = "http://localhost:8080"
real_get_url_state = _searxng_rate_limiter._get_url_state
- in_window = threading.Event()
- resume = threading.Event()
- def park_inside_the_window(normalized_url):
+ def evicting_get_url_state(normalized_url):
state = real_get_url_state(normalized_url)
- if normalized_url == victim:
- in_window.set()
- resume.wait(10)
+ # Stand in for another thread evicting the entry in the window
+ # between publication of the state and the caller acquiring its
+ # lock.
+ with _searxng_rate_limiter._meta_lock:
+ _searxng_rate_limiter._url_state.pop(normalized_url, None)
return state
monkeypatch.setattr(
- _searxng_rate_limiter, "_get_url_state", park_inside_the_window
+ _searxng_rate_limiter, "_get_url_state", evicting_get_url_state
+ )
+
+ for _ in range(5):
+ _searxng_rate_limiter.respect_rate_limit(
+ "http://localhost:8080", 0.1
+ )
+
+ assert _searxng_rate_limiter._url_state == {}
+
+ def test_evicted_state_still_throttles_callers_that_already_hold_it(
+ self, monkeypatch
+ ):
+ """Eviction drops the map entry, but existing callers share its clock.
+
+ An identity guard on the final timestamp write would leave both
+ callers seeing the initial zero timestamp and skipping their delay.
+ Updating the retained state preserves their timing without restoring
+ an evicted dictionary entry.
+ """
+ from types import SimpleNamespace
+
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter as limiter,
)
- caller = threading.Thread(
- target=_searxng_rate_limiter.respect_rate_limit,
- args=(victim, 0.1),
+
+ url = "http://localhost:8080"
+ first = limiter._get_url_state(url)
+ second = limiter._get_url_state(url)
+ assert first is second
+ with limiter._meta_lock:
+ limiter._url_state.pop(url)
+
+ retained = [first, second]
+ monkeypatch.setattr(
+ limiter, "_get_url_state", lambda _: retained.pop(0)
)
- caller.start()
+ now = [100.0]
+ sleeps = []
+
+ def advance(seconds):
+ sleeps.append(seconds)
+ now[0] += seconds
+
+ monkeypatch.setattr(
+ limiter,
+ "time",
+ SimpleNamespace(monotonic=lambda: now[0], sleep=advance),
+ )
+ limiter.respect_rate_limit(url, 0.5)
+ limiter.respect_rate_limit(url, 0.5)
+
+ assert sleeps == [0.5]
+ assert first.last_request == 100.5
+ assert limiter._url_state == {}
+
+ def test_a_superseded_capacity_line_is_dropped_not_logged_late(
+ self, monkeypatch, loguru_caplog
+ ):
+ """The last line an operator sees must agree with the live state.
+
+ The decision runs under ``_meta_lock`` and the emission deliberately
+ does not, so a thread descheduled in between can publish a recovery
+ *after* a later thread has already published the warning that
+ superseded it -- leaving "evictable again" as the operator's last word
+ on a saturated tracker, with the next warning then suppressed for
+ ``ALL_HELD_REWARN_SECONDS``.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ with loguru_caplog.at_level("INFO"):
+ _searxng_rate_limiter._get_url_state("http://localhost:8084")
+ for lock in held:
+ lock.release()
+
+ # Thread A decides recovery and is descheduled before emitting.
+ with _searxng_rate_limiter._meta_lock:
+ recovery = _searxng_rate_limiter._capacity_pressure_unlocked()
+ assert recovery is not None and recovery[0] is False
+
+ # Thread B decides the next warning and emits it first.
+ everything = [
+ state.lock
+ for state in _searxng_rate_limiter._url_state.values()
+ ]
+ for lock in everything:
+ assert lock.acquire(timeout=10)
+ try:
+ with _searxng_rate_limiter._meta_lock:
+ warning = (
+ _searxng_rate_limiter._capacity_pressure_unlocked()
+ )
+ assert warning is not None and warning[0] is True
+
+ _searxng_rate_limiter._emit_capacity_log(warning)
+ # Thread A wakes up. Its record is stale and must not run.
+ _searxng_rate_limiter._emit_capacity_log(recovery)
+ finally:
+ for lock in everything:
+ lock.release()
+
+ assert RECOVERY_LOG not in loguru_caplog.text
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 2
+ assert _searxng_rate_limiter._all_held_warned_at is not None
+
+ def test_capacity_lines_still_emit_in_order_when_not_superseded(
+ self, monkeypatch, loguru_caplog
+ ):
+ """Dropping stale records must not drop the ordinary ones."""
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ with loguru_caplog.at_level("INFO"):
+ _searxng_rate_limiter._get_url_state("http://localhost:8084")
+ for lock in held:
+ lock.release()
+ _searxng_rate_limiter._get_url_state(urls[0])
+
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+ assert loguru_caplog.text.count(RECOVERY_LOG) == 1
+ assert loguru_caplog.text.index(SATURATION_WARNING) < (
+ loguru_caplog.text.index(RECOVERY_LOG)
+ )
+
+ def test_small_capacity_still_has_a_hysteresis_band(
+ self, monkeypatch, loguru_caplog
+ ):
+ """The band must not collapse onto the warn threshold.
+
+ Below twenty entries the divisor alone supplies at most one slot.
+ The two-slot floor keeps a briefly free lock from clearing the
+ warning: one free slot out of four stays inside the recovery band.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+ assert _searxng_rate_limiter._recovery_headroom_slots() == 2
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ released = []
try:
- assert in_window.wait(10)
- # The victim holds no lock yet and has no timestamp, so eviction
- # both admits it and sorts it oldest.
- for i in range(1, 6):
- _searxng_rate_limiter.respect_rate_limit(
- f"http://localhost:808{i}", 0.1
- )
- assert victim not in _searxng_rate_limiter._url_state
+ with loguru_caplog.at_level("INFO"):
+ _searxng_rate_limiter._get_url_state(urls[0])
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+
+ # One slot back out of four: inside the band, still warned.
+ held[0].release()
+ released.append(held[0])
+ _searxng_rate_limiter._get_url_state(urls[1])
+ assert RECOVERY_LOG not in loguru_caplog.text
+ assert _searxng_rate_limiter._all_held_warned_at is not None
+
+ # Two is the threshold, so this one is a real recovery.
+ held[1].release()
+ released.append(held[1])
+ _searxng_rate_limiter._get_url_state(urls[2])
+ assert RECOVERY_LOG in loguru_caplog.text
+ assert _searxng_rate_limiter._all_held_warned_at is None
+ finally:
+ for lock in held:
+ if lock not in released:
+ lock.release()
+
+ def test_small_capacity_state_does_not_flap_on_a_reused_lock(
+ self, monkeypatch, loguru_caplog
+ ):
+ """With the real re-warn interval, a lock cycling in and out of use
+ must not produce a warning per cycle.
+
+ ``ALL_HELD_REWARN_SECONDS`` is deliberately left at its production
+ value here: the only way a second warning can appear within it is a
+ recovery having reset the state in between, which is exactly the flap
+ the hysteresis band exists to stop.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ try:
+ with loguru_caplog.at_level("INFO"):
+ for index, lock in enumerate(held):
+ # Saturated, then one holder finishes and the next caller
+ # picks the same lock up again.
+ _searxng_rate_limiter._get_url_state(urls[index])
+ lock.release()
+ _searxng_rate_limiter._get_url_state(urls[index - 1])
+ assert lock.acquire(timeout=10)
finally:
- resume.set()
- caller.join(10)
+ for lock in held:
+ lock.release()
+
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+ assert RECOVERY_LOG not in loguru_caplog.text
+
+ def test_warning_reports_how_many_locks_are_evictable(
+ self, monkeypatch, loguru_caplog
+ ):
+ """Zero headroom does not mean zero evictable entries.
+
+ ``headroom <= 0`` is ``tracked - evictable >= MAX_TRACKED_URLS``, which
+ admits a non-zero ``evictable`` whenever the tracker has grown past
+ capacity. Claiming that all of them are in use would be false, so the
+ line reports both counts and the limit it cannot get back under.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+ monkeypatch.setattr(
+ _searxng_rate_limiter, "ALL_HELD_REWARN_SECONDS", 0.0
+ )
+
+ # Six tracked entries, every one of them held: the tracker is past
+ # capacity because nothing could be reclaimed on the way there.
+ urls = [f"http://localhost:808{i}" for i in range(6)]
+ held = []
+ for url in urls:
+ lock = _searxng_rate_limiter._get_url_state(url).lock
+ assert lock.acquire(timeout=10)
+ held.append(lock)
+ try:
+ # Two callers finish. Two of six are now evictable -- evicting both
+ # still leaves four tracked, which is not under a capacity of four.
+ held[0].release()
+ held[1].release()
+ with loguru_caplog.at_level("WARNING"):
+ _searxng_rate_limiter._get_url_state(urls[2])
+ finally:
+ for lock in held[2:]:
+ lock.release()
- assert victim not in _searxng_rate_limiter._url_state
assert (
- len(_searxng_rate_limiter._url_state)
- <= _searxng_rate_limiter.MAX_TRACKED_URLS
+ "only 2 of 6 tracked URL locks are evictable, not enough to "
+ "bring the tracker back under its capacity of 4"
+ ) in loguru_caplog.text
+ assert "all 6 tracked URL locks are in use" not in loguru_caplog.text
+
+ def test_eviction_prefers_a_timestamped_lock_over_a_brand_new_one(
+ self, monkeypatch
+ ):
+ """A lock with no timestamp yet is brand new, not the oldest.
+
+ ``_get_url_state`` returns a freshly created ``_UrlState`` (lock
+ unlocked, ``last_request == 0.0``) *before* its caller reaches
+ ``respect_rate_limit``'s ``with state.lock:`` -- ``respect_rate_limit``
+ writes a real timestamp once that caller passes its delay. This test
+ reproduces exactly that window by calling ``_get_url_state`` directly
+ and never acquiring the lock, the same window
+ ``_evict_stale_locks_unlocked`` documents as leaving an entry
+ evictable. ``_eviction_sort_key`` maps that ``0.0`` to ``+inf`` so it
+ sorts last; naively sorting on the raw timestamp would put brand-new
+ entries at the front of the victim list, evicting the newest entries
+ before ones that actually went stale.
+
+ (This is distinct from a request whose delay is still being applied:
+ that path holds ``state.lock`` for the whole sleep, so it is already
+ excluded from ``evictable`` by the lock-held guard regardless of this
+ sort key.)
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
)
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ settled = [f"http://localhost:808{i}" for i in range(2)]
+ for url in settled:
+ _searxng_rate_limiter.respect_rate_limit(url, 0.1)
+
+ # Brand new: the locks exist, their timestamps do not yet. The
+ # tracker is now exactly at capacity, so the next new URL triggers
+ # eviction.
+ brand_new = [f"http://localhost:809{i}" for i in range(2)]
+ for url in brand_new:
+ _searxng_rate_limiter._get_url_state(url)
+ assert _searxng_rate_limiter._url_state[url].last_request == 0.0
+
+ assert len(_searxng_rate_limiter._url_state) == 4
+
+ _searxng_rate_limiter._get_url_state("http://localhost:8099")
+
+ for url in brand_new:
+ assert url in _searxng_rate_limiter._url_state
+ for url in settled:
+ assert url not in _searxng_rate_limiter._url_state
+
+ def test_capacity_scan_stops_once_the_answer_cannot_change(
+ self, monkeypatch
+ ):
+ """The evictable count runs under ``_meta_lock``, so it is bounded.
+
+ Below the warn threshold the only question is whether *any* lock is
+ free, so the scan stops at the first one instead of walking every
+ tracked URL on a hot path that already serializes every other lookup.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ class _CountingLock:
+ def __init__(self):
+ self.checks = 0
+
+ def locked(self):
+ self.checks += 1
+ return False
+
+ locks = {f"http://localhost:808{i}": _CountingLock() for i in range(4)}
+ with _searxng_rate_limiter._meta_lock:
+ _searxng_rate_limiter._url_state.update(
+ {
+ url: _searxng_rate_limiter._UrlState(lock=lock)
+ for url, lock in locks.items()
+ }
+ )
+ pending = _searxng_rate_limiter._capacity_pressure_unlocked()
+
+ assert pending is None
+ assert sum(lock.checks for lock in locks.values()) == 1
+
+ def test_the_recovery_band_scales_with_the_shipped_capacity(
+ self, monkeypatch
+ ):
+ """At the real ``MAX_TRACKED_URLS`` the band comes from the divisor.
+
+ The two-slot floor is what every other test in this file measures,
+ because they all shrink the capacity to 4 or 20. Below twenty entries
+ the divisor contributes at most one slot, so a build that dropped it
+ or weakened it would look identical there while the shipped tracker
+ went from a hundred slots of hysteresis to two.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 1000)
+
+ assert _searxng_rate_limiter._recovery_headroom_slots() == 100
+
+ def test_recovery_stays_reachable_at_a_one_entry_capacity(
+ self, monkeypatch, loguru_caplog
+ ):
+ """The band is capped at the capacity, so it can always be met.
+
+ Headroom can never exceed ``MAX_TRACKED_URLS`` while anything is
+ tracked, so an uncapped two-slot floor at a capacity of one would make
+ recovery arithmetically impossible and pin the tracker as warned for
+ the life of the process.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 1)
+ assert _searxng_rate_limiter._recovery_headroom_slots() == 1
+
+ urls, held = _saturate(_searxng_rate_limiter, 1)
+ try:
+ with loguru_caplog.at_level("INFO"):
+ _searxng_rate_limiter._get_url_state(urls[0])
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+
+ held[0].release()
+ _searxng_rate_limiter._get_url_state(urls[0])
+ assert RECOVERY_LOG in loguru_caplog.text
+ assert _searxng_rate_limiter._all_held_warned_at is None
+ finally:
+ for lock in held:
+ if lock.locked():
+ lock.release()
+
+ def test_the_rewarn_interval_is_five_minutes(self):
+ """Pin the shipped value.
+
+ Every test that exercises re-warning patches this to ``0.0`` to keep
+ itself fast, so nothing else in the suite would notice the constant
+ being changed.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ assert _searxng_rate_limiter.ALL_HELD_REWARN_SECONDS == 300.0
+
+ def test_capacity_lines_are_emitted_under_the_emit_lock(self, monkeypatch):
+ """Two threads emitting at once must not interleave their lines.
+
+ The decision is made under ``_meta_lock`` and the emission happens
+ after releasing it, so two threads can be inside
+ ``_emit_capacity_log`` at the same time with records from different
+ transitions. ``_emit_lock`` is what keeps one line from being written
+ into the middle of another and what makes the generation check a
+ read-modify-write rather than a race.
+
+ Racing threads cannot be made to interleave on demand without sleeps,
+ so the deterministic half of this test is the lock itself: a recording
+ stand-in reports whether it was held at the moment each line was
+ written, and the events it records must stay properly nested.
+ """
+ import threading
+
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ events = []
+ events_lock = threading.Lock()
+
+ class _RecordingLock:
+ """A real lock that notes when it is taken and released."""
+
+ def __init__(self):
+ self._lock = threading.Lock()
+ self.held = False
+
+ def acquire(self, *args, **kwargs):
+ acquired = self._lock.acquire(*args, **kwargs)
+ if acquired:
+ self.held = True
+ with events_lock:
+ events.append("acquire")
+ return acquired
+
+ def release(self):
+ with events_lock:
+ events.append("release")
+ self.held = False
+ self._lock.release()
+
+ def locked(self):
+ return self._lock.locked()
+
+ def __enter__(self):
+ self.acquire()
+ return self
+
+ def __exit__(self, *exc_info):
+ self.release()
+
+ emit_lock = _RecordingLock()
+ monkeypatch.setattr(_searxng_rate_limiter, "_emit_lock", emit_lock)
+
+ class _RecordingLogger:
+ def _record(self, message, *args):
+ with events_lock:
+ events.append(
+ ("emit", emit_lock.held, message.format(*args))
+ )
+
+ warning = _record
+ info = _record
+
+ monkeypatch.setattr(_searxng_rate_limiter, "logger", _RecordingLogger())
+
+ # Two warnings from consecutive transitions. Whichever thread wins,
+ # the newer record must be the one that lands.
+ records = [
+ (True, "first {}", (1,), 1),
+ (True, "second {}", (2,), 2),
+ ]
+ start = threading.Barrier(len(records))
+
+ def emit(record):
+ start.wait(10)
+ _searxng_rate_limiter._emit_capacity_log(record)
+
+ threads = [
+ threading.Thread(target=emit, args=(record,)) for record in records
+ ]
+ for thread in threads:
+ thread.start()
+ for thread in threads:
+ thread.join(10)
+ for thread in threads:
+ assert not thread.is_alive()
+
+ emissions = [event for event in events if isinstance(event, tuple)]
+ assert emissions, events
+ # Held at the moment of the write. Dropping the ``with _emit_lock:``
+ # leaves this False.
+ assert all(held for _, held, _ in emissions), events
+ # No line written twice, and none started while another was running.
+ assert len(emissions) == len({text for _, _, text in emissions}), events
+ depth = 0
+ for event in events:
+ if event == "acquire":
+ depth += 1
+ elif event == "release":
+ depth -= 1
+ else:
+ assert depth == 1, events
+ assert 0 <= depth <= 1, events
+ assert depth == 0, events
+ assert _searxng_rate_limiter._emitted_generation == 2
+
+ def test_a_recovery_that_overtakes_its_own_warning_is_withheld(
+ self, monkeypatch, loguru_caplog
+ ):
+ """The mirror image: a recovery must not be the first word either.
+
+ Thread A decides the warning and is descheduled before emitting it,
+ the holders finish, and thread B decides the recovery and emits
+ first. Logging that recovery would print "evictable again" with
+ nothing before it to recover from, and would stamp A's warning away
+ as stale on the way past -- so an episode that really happened would
+ leave no warning in the log at all. Withholding the recovery leaves
+ the warning to stand on its own, which reads as an episode still
+ open rather than one that never occurred.
+ """
+ from local_deep_research.web_search_engines.engines import (
+ _searxng_rate_limiter,
+ )
+
+ monkeypatch.setattr(_searxng_rate_limiter, "MAX_TRACKED_URLS", 4)
+
+ urls, held = _saturate(_searxng_rate_limiter)
+ try:
+ with loguru_caplog.at_level("INFO"):
+ # Thread A decides the warning and does not get to emit it.
+ with _searxng_rate_limiter._meta_lock:
+ warning = (
+ _searxng_rate_limiter._capacity_pressure_unlocked()
+ )
+ assert warning is not None and warning[0] is True
+
+ # Every holder finishes; thread B decides the recovery.
+ for lock in held:
+ lock.release()
+ with _searxng_rate_limiter._meta_lock:
+ recovery = (
+ _searxng_rate_limiter._capacity_pressure_unlocked()
+ )
+ assert recovery is not None and recovery[0] is False
+ assert recovery[3] == warning[3] + 1
+
+ # B emits first. Nothing may be logged yet.
+ _searxng_rate_limiter._emit_capacity_log(recovery)
+ assert RECOVERY_LOG not in loguru_caplog.text
+ assert SATURATION_WARNING not in loguru_caplog.text
+
+ # A wakes up. Its warning is not stale and still goes out.
+ _searxng_rate_limiter._emit_capacity_log(warning)
+ assert loguru_caplog.text.count(SATURATION_WARNING) == 1
+ assert RECOVERY_LOG not in loguru_caplog.text
+ finally:
+ for lock in held:
+ if lock.locked():
+ lock.release()
+
def test_rate_limit_multithreaded(self):
"""Multiple threads invoking respect_rate_limit concurrently are properly serialized."""
import concurrent.futures
diff --git a/tests/web_search_engines/test_adapter_failure_contracts.py b/tests/web_search_engines/test_adapter_failure_contracts.py
index c1e2c4afdd..8692336d46 100644
--- a/tests/web_search_engines/test_adapter_failure_contracts.py
+++ b/tests/web_search_engines/test_adapter_failure_contracts.py
@@ -1068,12 +1068,16 @@ DELAY = 5.0
class TestSearxngUrlThrottleEviction:
"""Issue #5748: the throttle is keyed by instance URL in a bounded
- map, and a brand-new entry carries ``last_request == 0.0``.
-
- The existing suite already shows that such an entry is evicted. What
- is pinned here is the *consequence* -- the configured delay silently
- stops applying to that instance -- and the exact ordering rule that
- causes it.
+ map. A brand-new entry carries ``last_request == 0.0``, meaning "no
+ request recorded yet" -- see ``_eviction_sort_key`` -- which sorts
+ *last*, not first, in the eviction order: an in-flight request whose
+ timestamp has not been recorded yet is not the preferred
+ eviction victim just for lacking a timestamp.
+
+ What is pinned here is the harm eviction still causes despite that
+ ordering rule -- the configured delay can silently stop applying to a
+ genuinely idle-but-recently-used instance -- and the exact ordering
+ rule that decides which entries go.
"""
def test_repeat_request_to_one_instance_is_throttled(self, throttle_clock):
@@ -1092,12 +1096,14 @@ class TestSearxngUrlThrottleEviction:
assert searxng_throttle._url_state == {}
assert throttle_clock.slept == []
- def test_a_new_entry_sorts_oldest_and_is_evicted_ahead_of_live_ones(
+ def test_a_new_entry_sorts_last_and_is_not_evicted_ahead_of_live_ones(
self, throttle_clock
):
- """The ordering key is ``last_request``, and a freshly created
- entry holds ``0.0`` -- numerically older than any real monotonic
- timestamp, so it is always at the front of the eviction queue."""
+ """The ordering key is ``_eviction_sort_key``, and an in-flight
+ entry (state created, request not yet recorded) reads
+ ``last_request == 0.0`` -- which the key maps to ``+inf``, so it
+ sorts after every real monotonic timestamp, not before, and is
+ never the preferred eviction victim."""
for index in range(3):
searxng_throttle.respect_rate_limit(
f"http://live{index}.invalid", DELAY
@@ -1116,12 +1122,15 @@ class TestSearxngUrlThrottleEviction:
searxng_throttle._url_state[f"http://live{i}.invalid"].last_request
for i in range(3)
]
- assert all(ts > state.last_request for ts in live_timestamps)
+ assert all(ts > 0.0 for ts in live_timestamps)
- # Reaching capacity evicts the oldest half -> the in-flight entry
- # goes first, and the genuinely oldest *live* entry with it.
+ # Reaching capacity evicts the oldest half by timestamp -> the
+ # in-flight entry (no timestamp, sorts last) survives, and the
+ # two genuinely oldest *live* entries go instead.
searxng_throttle.respect_rate_limit("http://newcomer.invalid", DELAY)
- assert in_flight not in searxng_throttle._url_state
+ assert in_flight in searxng_throttle._url_state
+ assert "http://live0.invalid" not in searxng_throttle._url_state
+ assert "http://live1.invalid" not in searxng_throttle._url_state
assert "http://live2.invalid" in searxng_throttle._url_state
def test_an_evicted_instance_silently_loses_its_configured_throttle(
@@ -1183,7 +1192,12 @@ class TestSearxngUrlThrottleEviction:
from being dropped -- the guard the in-flight window misses."""
held = "http://busy.invalid"
state = searxng_throttle._get_url_state(held)
- state.last_request = 0.0 # oldest possible, so eviction wants it
+ # A genuine old timestamp, not 0.0: 0.0 now means "in flight" and
+ # sorts *last* for eviction (see _eviction_sort_key), so it would
+ # no longer be the entry eviction most wants to reclaim. An old
+ # real timestamp is, which is what this test needs to show the
+ # held-lock guard -- not the ordering -- is what saves it.
+ state.last_request = 1.0
state.lock.acquire()
try:
for index in range(5):