~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(benchmarks): read active_runs atomically in sync_pending_results (#5146)
Ehsan0482f5a9621d
diff --git a/changelog.d/4859.bugfix.md b/changelog.d/4859.bugfix.mdnew file mode 100644index 0000000000..38276092e3--- /dev/null+++ b/changelog.d/4859.bugfix.md@@ -0,0 +1 @@+Benchmark result polling (`GET /api/results/<id>`) no longer returns a spurious 500 when a run finishes at the same moment. `sync_pending_results` read `active_runs` with a non-atomic membership check followed by a separate subscript, so the worker thread's end-of-run `del self.active_runs[id]` could land between the two and raise an uncaught `KeyError` (the read happens before the method's `try`), which the results route reported as a 500. The value is now fetched once with `.get()`, so a run that finished during the poll is treated as having nothing left to sync and returns cleanly.diff --git a/src/local_deep_research/benchmarks/web_api/benchmark_service.py b/src/local_deep_research/benchmarks/web_api/benchmark_service.pyindex 045125cd6f..80b47fbd4c 100644--- a/src/local_deep_research/benchmarks/web_api/benchmark_service.py+++ b/src/local_deep_research/benchmarks/web_api/benchmark_service.py@@ -993,11 +993,16 @@ class BenchmarkService:self, benchmark_run_id: int, username: Optional[str] = None):"""Sync any pending results to database. Can be called from main thread."""- if benchmark_run_id not in self.active_runs:+ # Read outside _results_sync_lock, so the worker thread's+ # ``del self.active_runs[benchmark_run_id]`` (in _sync_results_to_database,+ # also unlocked) can land between the membership check and the subscript.+ # A plain ``active_runs[id]`` would then raise KeyError, which escapes+ # before the try below and surfaces as a spurious 500 on /api/results.+ # Fetch once so the check and the read see the same state. See #4859.+ run_data = self.active_runs.get(benchmark_run_id)+ if run_data is None:return 0- run_data = self.active_runs[benchmark_run_id]-if not username:username = run_data.get("data", {}).get("username")user_password = run_data.get("data", {}).get("user_password")diff --git a/tests/benchmarks/web_api/test_benchmark_service_coverage.py b/tests/benchmarks/web_api/test_benchmark_service_coverage.pyindex d014ec65c2..7d8d73cd42 100644--- a/tests/benchmarks/web_api/test_benchmark_service_coverage.py+++ b/tests/benchmarks/web_api/test_benchmark_service_coverage.py@@ -699,6 +699,25 @@ class TestSyncPendingResults:svc = _make_service()assert svc.sync_pending_results(999) == 0+ def test_returns_zero_when_entry_deleted_during_toctou_window(self):+ """The worker thread's ``del self.active_runs[id]`` can land between+ the membership check and the subscript in ``sync_pending_results``.+ The read must not raise KeyError (which surfaces as a spurious 500 on+ /api/results); a run that vanished has nothing pending. See #4859.+ """++ class _VanishingRuns(dict):+ # Present at the ``in`` check, gone at the read: models the+ # concurrent delete firing inside the TOCTOU window.+ def __contains__(self, key):+ return True++ svc = _make_service()+ # dict.get on an empty mapping returns None while __contains__ lies True.+ svc.active_runs = _VanishingRuns()++ assert svc.sync_pending_results(123) == 0+def test_saves_new_results(self):svc = _make_service()svc.active_runs[1] = {