~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
"""``web/app.py::main()``'s HTTPS branch, and every guard on the lifespan's
shutdown path.
Recovered from main's deleted ``tests/web/test_app_main_coverage.py`` and
``tests/web/test_app_coverage.py``. Those files tested a Flask ``create_app``
plus a chain of ``atexit`` handlers; the ``atexit`` MECHANISM is gone (see
the comment at ``fastapi_app.py`` explaining why the lifespan owns shutdown
instead), but the PROPERTIES those handlers were tested for moved into
``lifespan()``'s post-``yield`` block and survive there unchanged:
* ``shutdown_scheduler`` -> ``if news_scheduler: try: news_scheduler.stop()``
* ``shutdown_databases`` -> ``try: db_manager.close_all_databases()``
* ``flush_logs_on_exit`` -> ``try: flush_log_queue(); stop_log_queue_processor()``
Each is a bare ``try/except`` whose whole reason to exist is that a failure
in one teardown step must not strand the steps after it. Nothing on this
branch ever makes one of them raise:
``tests/web/test_startup_ordering_contracts.py`` proves the guards EXIST by
parsing the AST, and ``tests/web/test_lifespan_startup_shutdown.py`` proves
the clean path runs in the right order — neither observes what happens when
a step actually fails, which is the only situation the guards are for.
WHY A CHILD PROCESS. ``socketio_asgi.init_lock()`` only assigns when
``_lock is None``, so a process may complete at most ONE real lifespan
cycle. The probes below therefore run in a FRESH interpreter, one lifespan
each, and report back as JSON — the vehicle established by
``tests/web/test_lifespan_startup_shutdown.py``. These tests carry no
``lifespan`` marker: they enter no lifespan in the pytest process.
Every instrumented step calls THROUGH to the real implementation before
raising, so the resource really is released and the child cannot hang on a
stranded non-daemon thread — the raise exercises the guard without
sabotaging the teardown it guards.
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
from pathlib import Path
from types import ModuleType
from unittest.mock import MagicMock, patch
import pytest
from local_deep_research.web import fastapi_app
SRC_ROOT = Path(fastapi_app.__file__).resolve().parents[2]
# Leave time to kill/reap the child and report the failure before pytest's
# 180-second deadline terminates the whole worker.
_BOOT_PROBE_TIMEOUT_SECONDS = 120
# ---------------------------------------------------------------------------
# main()'s HTTPS branch
# ---------------------------------------------------------------------------
_CONFIG_TEMPLATE = {
"host": "127.0.0.1",
"port": 5000,
"debug": False,
"use_https": False,
}
def _run_main(use_https: bool):
"""Run ``web.app.main()`` with everything below it stubbed.
Returns ``(run_with_uvicorn_mock, logger_mock)``.
"""
from local_deep_research.web import app as web_app
config = dict(_CONFIG_TEMPLATE, use_https=use_https)
with (
patch.object(web_app, "load_server_config", return_value=config),
patch.object(web_app, "config_logger"),
patch.object(web_app, "_run_with_uvicorn") as run,
patch.object(web_app, "logger") as logger,
patch(
"local_deep_research.vector_stores.legacy_cleanup"
".migrate_legacy_docstores"
),
):
web_app.main()
return run, logger
def _warning_texts(logger_mock):
return [str(call) for call in logger_mock.warning.call_args_list]
class TestMainHttpsWarning:
"""``web.use_https`` never served TLS on either framework.
``load_server_config()``'s default for ``use_https`` is ``True``
(``tests/web/test_server_config.py``), so this is the COMMON path, and
an operator who sets it and gets silence reasonably concludes TLS is on.
"""
def test_https_true_still_launches_the_server(self):
"""The branch must warn, not abort: the server still comes up on
plain HTTP with the resolved host/port/debug."""
run, _logger = _run_main(use_https=True)
run.assert_called_once_with("127.0.0.1", 5000, False)
def test_https_true_warns_that_tls_is_not_served(self):
_run, logger = _run_main(use_https=True)
warnings = _warning_texts(logger)
assert warnings, (
"use_https=True produced no warning at all — the operator gets "
"silence and assumes TLS is on"
)
assert any("use_https" in text for text in warnings), warnings
assert any("reverse proxy" in text for text in warnings), (
"the warning must tell the operator what to do instead "
f"(terminate TLS at a reverse proxy): {warnings}"
)
def test_https_false_emits_no_https_warning(self):
"""Negative control: the warning is gated on the setting, not
emitted unconditionally."""
_run, logger = _run_main(use_https=False)
https_warnings = [
text
for text in _warning_texts(logger)
if "use_https" in text or "reverse proxy" in text
]
assert https_warnings == [], https_warnings
def test_legacy_docstore_cleanup_failure_does_not_block_uvicorn_startup():
"""The one-time migration is best-effort at the real web entrypoint.
A filesystem error must be visible to the operator, but it cannot turn an
otherwise healthy FastAPI deployment into a dead process. This drives
the exception branch rather than merely checking that its ``try`` exists.
"""
from local_deep_research.web import app as web_app
config = dict(_CONFIG_TEMPLATE)
module_name = "local_deep_research.vector_stores.legacy_cleanup"
migration_module = ModuleType(module_name)
migrate = MagicMock(side_effect=OSError("read-only legacy store"))
migration_module.migrate_legacy_docstores = migrate
with (
patch.object(web_app, "_install_thread_excepthook"),
patch.object(web_app, "load_server_config", return_value=config),
patch.object(web_app, "config_logger"),
patch.object(web_app, "_run_with_uvicorn") as run,
patch.object(web_app, "logger") as logger,
patch.dict(sys.modules, {module_name: migration_module}),
):
web_app.main()
migrate.assert_called_once_with()
logger.exception.assert_called_once_with(
"Legacy RAG docstore migration failed at startup"
)
run.assert_called_once_with("127.0.0.1", 5000, False)
# ---------------------------------------------------------------------------
# Lifespan shutdown guards — boot probe
# ---------------------------------------------------------------------------
_PROBE_SOURCE = r'''
"""Boot the real app once and report what the shutdown path did.
argv: <mode> <output json path>
mode "shutdown-broken" -- every guarded shutdown step raises AFTER doing
its real work, so each guard is exercised
without stranding the resource it releases.
mode "no-news-scheduler" -- news.scheduler.enabled=false, so the
`if news_scheduler:` shutdown branch must
no-op rather than blow up on None.
"""
import json
import sys
from pathlib import Path
MODE = sys.argv[1]
OUT = Path(sys.argv[2])
trace = []
def record(event):
trace.append(event)
from fastapi.testclient import TestClient
from local_deep_research.database.encrypted_db import db_manager
from local_deep_research.scheduler import background as scheduler_background
from local_deep_research.utilities import log_utils
from local_deep_research.web import fastapi_app
from local_deep_research.web.auth import connection_cleanup
from local_deep_research.web.queue.processor_v2 import queue_processor
_real_get_scheduler = scheduler_background.get_background_job_scheduler
_real_start_cleanup = connection_cleanup.start_connection_cleanup_scheduler
_real_qp_stop = queue_processor.stop
_real_flush = log_utils.flush_log_queue
_real_stop_log_queue = log_utils.stop_log_queue_processor
_real_close_all = db_manager.close_all_databases
BREAK = MODE == "shutdown-broken"
def _get_scheduler(*args, **kwargs):
scheduler = _real_get_scheduler(*args, **kwargs)
real_stop = scheduler.stop
def _stop(*a, **k):
record("news_scheduler.stop")
real_stop(*a, **k)
if BREAK:
raise RuntimeError("simulated news scheduler stop failure")
scheduler.stop = _stop
return scheduler
def _start_cleanup(*args, **kwargs):
scheduler = _real_start_cleanup(*args, **kwargs)
real_shutdown = scheduler.shutdown
def _shutdown(wait=True, *a, **k):
record("cleanup_scheduler.shutdown")
real_shutdown(wait, *a, **k)
if BREAK:
raise RuntimeError("simulated cleanup scheduler shutdown failure")
scheduler.shutdown = _shutdown
return scheduler
def _qp_stop(*args, **kwargs):
record("queue_processor.stop")
_real_qp_stop(*args, **kwargs)
if BREAK:
raise RuntimeError("simulated queue processor stop failure")
def _flush(*args, **kwargs):
record("flush_log_queue")
_real_flush(*args, **kwargs)
# Deliberately stop the drain daemon here too: the lifespan's guard
# SKIPS stop_log_queue_processor() when flush raises, and a stranded
# non-daemon drain thread would hang this probe on exit.
_real_stop_log_queue()
if BREAK:
raise RuntimeError("simulated log flush failure")
def _stop_log_queue(*args, **kwargs):
record("stop_log_queue_processor")
return _real_stop_log_queue(*args, **kwargs)
def _close_all(*args, **kwargs):
record("close_all_databases")
_real_close_all(*args, **kwargs)
if BREAK:
raise RuntimeError("simulated database close failure")
scheduler_background.get_background_job_scheduler = _get_scheduler
connection_cleanup.start_connection_cleanup_scheduler = _start_cleanup
queue_processor.stop = _qp_stop
log_utils.flush_log_queue = _flush
log_utils.stop_log_queue_processor = _stop_log_queue
db_manager.close_all_databases = _close_all
result = {"mode": MODE}
with TestClient(fastapi_app.app) as client:
result["health_status"] = client.get("/api/v1/health").status_code
# Reaching here at all means __exit__ returned: no exception escaped the
# lifespan's shutdown block.
result["shutdown_completed"] = True
result["trace"] = trace
OUT.write_text(json.dumps(result, indent=1), encoding="utf-8")
'''
def _run_boot_probe(mode: str, workdir: Path, extra_env=None) -> dict:
script = workdir / "shutdown_probe.py"
script.write_text(_PROBE_SOURCE, encoding="utf-8")
out = workdir / "probe.json"
data_dir = workdir / "data"
data_dir.mkdir()
env = dict(os.environ)
env["LDR_DATA_DIR"] = str(data_dir)
env.setdefault("LDR_BOOTSTRAP_ALLOW_UNENCRYPTED", "true")
env["PYTHONPATH"] = os.pathsep.join(
[str(SRC_ROOT), env.get("PYTHONPATH", "")]
).rstrip(os.pathsep)
# Take the PRODUCTION path for the research queue processor so its
# shutdown guard is covered too (lifespan skips it under pytest).
env.pop("PYTEST_CURRENT_TEST", None)
# Pin the news scheduler ON rather than inheriting it. The child copies
# this process's environment, and the CI pytest job runs the whole suite
# with `-e LDR_NEWS_SCHEDULER_ENABLED=false` (.github/workflows/
# docker-tests.yml, to keep per-test daemon threads out of the xdist
# workers). Inherited, that left `news_scheduler` as None in EVERY probe
# -- the `if news_scheduler:` shutdown branch never ran, so
# `news_scheduler.stop` was missing from the trace and the
# TestNewsSchedulerDisabled pair below lost its contrast: both probes
# were the scheduler-off probe. It reproduced only under an environment
# nothing in the file set, which is exactly what this line removes.
env["LDR_NEWS_SCHEDULER_ENABLED"] = "true"
env.update(extra_env or {})
try:
completed = subprocess.run(
[sys.executable, str(script), mode, str(out)],
env=env,
capture_output=True,
text=True,
timeout=_BOOT_PROBE_TIMEOUT_SECONDS,
)
except subprocess.TimeoutExpired as exc:
stderr = exc.stderr or ""
if isinstance(stderr, bytes):
stderr = stderr.decode("utf-8", errors="replace")
# A child may write its JSON before hanging during interpreter exit.
# Its incomplete process lifecycle must still fail every guard check.
return {
"mode": mode,
"probe_returncode": None,
"probe_wrote_json": out.exists(),
"probe_stderr": f"Timed out after {exc.timeout}s\n{stderr[-3000:]}",
"shutdown_completed": False,
"health_status": None,
"trace": [],
}
# Deliberately NOT pytest.fail() here: a raise inside a fixture lands in
# pytest's separate ERRORS bucket, which is far easier to overlook than a
# FAILED. The probe's outcome is returned as DATA and asserted in the
# tests below, so a lifespan that lets an exception escape shows up as a
# named failing test.
result = {}
if out.exists():
try:
result = json.loads(out.read_text(encoding="utf-8"))
except ValueError:
result = {}
result["probe_returncode"] = completed.returncode
result["probe_wrote_json"] = out.exists()
result["probe_stderr"] = completed.stderr[-3000:]
result.setdefault("shutdown_completed", False)
result.setdefault("health_status", None)
result.setdefault("trace", [])
return result
def _assert_probe_ran(boot: dict) -> None:
"""The child must exit 0 — a non-zero exit IS the escaped exception."""
assert boot["probe_returncode"] == 0 and boot["probe_wrote_json"], (
f"the {boot.get('mode')!r} probe exited "
f"{boot['probe_returncode']} — an exception escaped the lifespan's "
f"shutdown block, or the app never completed a cycle.\n"
f"--- stderr ---\n{boot['probe_stderr']}"
)
@pytest.fixture(scope="module")
def broken_shutdown_boot(tmp_path_factory) -> dict:
"""A cycle in which EVERY guarded shutdown step raises."""
return _run_boot_probe(
"shutdown-broken", tmp_path_factory.mktemp("shutdown_broken")
)
@pytest.fixture(scope="module")
def no_news_scheduler_boot(tmp_path_factory) -> dict:
"""A cycle booted with the news scheduler switched off."""
return _run_boot_probe(
"no-news-scheduler",
tmp_path_factory.mktemp("no_news_scheduler"),
extra_env={"LDR_NEWS_SCHEDULER_ENABLED": "false"},
)
class TestShutdownGuardsAreIndependent:
"""A raise in one teardown step must not strand the steps after it.
All five failures happen in the SAME cycle, which is what makes this a
real test of independence rather than five separate "it survived one
failure" checks: the last step still has to run with four earlier
exceptions behind it.
"""
def test_the_shutdown_block_completes_despite_every_step_failing(
self, broken_shutdown_boot
):
_assert_probe_ran(broken_shutdown_boot)
assert broken_shutdown_boot["health_status"] == 200
assert broken_shutdown_boot["shutdown_completed"] is True
@pytest.mark.parametrize(
"step",
[
"news_scheduler.stop",
"cleanup_scheduler.shutdown",
"queue_processor.stop",
"flush_log_queue",
"close_all_databases",
],
)
def test_every_guarded_step_was_still_attempted(
self, broken_shutdown_boot, step
):
"""Each step ran even though the one before it raised. Remove any
one guard's ``except`` and this stops holding for every step after
it."""
_assert_probe_ran(broken_shutdown_boot)
assert step in broken_shutdown_boot["trace"], (
f"{step} never ran — an earlier shutdown failure aborted the "
f"rest of the block. trace={broken_shutdown_boot['trace']}"
)
def test_databases_are_closed_last(self, broken_shutdown_boot):
"""``flush_log_queue`` before ``close_all_databases`` is the
ordering ``database_sink`` depends on (it swallows write errors, so
anything flushed after the close is silently dropped) — and the
ordering has to survive the failures too."""
_assert_probe_ran(broken_shutdown_boot)
trace = broken_shutdown_boot["trace"]
assert trace.index("flush_log_queue") < trace.index(
"close_all_databases"
), trace
class TestNewsSchedulerDisabled:
"""``news.scheduler.enabled=false`` leaves ``news_scheduler`` as None.
The ``if news_scheduler:`` guard on the shutdown side is what keeps that
from being an ``AttributeError`` on every shutdown of a deployment that
turned the scheduler off.
"""
def test_the_app_boots_and_shuts_down_with_no_news_scheduler(
self, no_news_scheduler_boot
):
_assert_probe_ran(no_news_scheduler_boot)
assert no_news_scheduler_boot["health_status"] == 200
assert no_news_scheduler_boot["shutdown_completed"] is True
def test_no_news_scheduler_was_started_or_stopped(
self, no_news_scheduler_boot
):
_assert_probe_ran(no_news_scheduler_boot)
assert "news_scheduler.stop" not in no_news_scheduler_boot["trace"], (
"a news scheduler was stopped even though the setting is off — "
"the enabled gate is not being honoured"
)
def test_the_rest_of_shutdown_still_runs(self, no_news_scheduler_boot):
"""Positive control for the row above: "stop was not called" must
mean "there was no scheduler", not "shutdown never got that far"."""
_assert_probe_ran(no_news_scheduler_boot)
trace = no_news_scheduler_boot["trace"]
assert "close_all_databases" in trace, trace
assert "flush_log_queue" in trace, trace