~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
"""Tests for the daemon-thread excepthook.
Covers:
- _install_thread_excepthook logs uncaught exceptions from daemon threads.
- _perform_post_login_tasks wraps its body so an otherwise-uncaught exception
from inside the `with` context managers is logged instead of disappearing.
"""
import threading
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
def _exception_args(exc, thread):
return SimpleNamespace(
exc_type=type(exc),
exc_value=exc,
exc_traceback=exc.__traceback__,
thread=thread,
)
@contextmanager
def _installed_hook(previous_hook):
"""Install the production hook over a controlled predecessor."""
from local_deep_research.web.app import _install_thread_excepthook
original = threading.excepthook
threading.excepthook = previous_hook
_install_thread_excepthook()
installed = threading.excepthook
try:
yield installed
finally:
threading.excepthook = original
def test_install_thread_excepthook_logs_uncaught_exception():
"""After installing the hook, an uncaught exception on a daemon thread
must be logged at ERROR level including the exception type and the
thread name. Without this, silent crashes in the queue processor
or APScheduler jobs are the mechanism by which the login path
gradually starves.
"""
from local_deep_research.web.app import _install_thread_excepthook
previous = threading.excepthook
chained_hook = MagicMock()
threading.excepthook = chained_hook
_install_thread_excepthook()
try:
with patch("local_deep_research.web.app.logger") as mock_logger:
error = RuntimeError("boom from daemon")
args = SimpleNamespace(
exc_type=RuntimeError,
exc_value=error,
exc_traceback=error.__traceback__,
thread=SimpleNamespace(name="test-daemon"),
)
threading.excepthook(args)
assert mock_logger.error.called, "excepthook did not log"
chained_hook.assert_called_once_with(args)
logged = mock_logger.error.call_args[0][0]
assert "test-daemon" in logged
assert "RuntimeError" in logged
assert "boom from daemon" in logged
finally:
threading.excepthook = previous
def test_install_thread_excepthook_ignores_system_exit_without_chaining():
"""Match threading's default: ``SystemExit`` in a thread is intentional."""
previous_hook = MagicMock()
args = _exception_args(
SystemExit(0), SimpleNamespace(name="intentional-exit")
)
with _installed_hook(previous_hook) as hook:
with patch("local_deep_research.web.app.logger") as mock_logger:
hook(args)
mock_logger.error.assert_not_called()
previous_hook.assert_not_called()
def test_logger_failure_still_chains_to_the_previous_excepthook():
"""Last-ditch logging must not suppress Python's normal hook."""
previous_hook = MagicMock()
args = _exception_args(
RuntimeError("logger unavailable"), SimpleNamespace(name="worker")
)
with _installed_hook(previous_hook) as hook:
with patch("local_deep_research.web.app.logger") as mock_logger:
mock_logger.error.side_effect = RuntimeError("sink failed")
hook(args)
mock_logger.error.assert_called_once()
previous_hook.assert_called_once_with(args)
def test_previous_excepthook_failure_is_swallowed_after_logging():
"""A broken predecessor cannot make the replacement hook raise."""
previous_hook = MagicMock(side_effect=RuntimeError("old hook failed"))
args = _exception_args(
ValueError("worker failed"), SimpleNamespace(name="worker")
)
with _installed_hook(previous_hook) as hook:
with patch("local_deep_research.web.app.logger") as mock_logger:
hook(args)
mock_logger.error.assert_called_once()
previous_hook.assert_called_once_with(args)
def test_missing_thread_object_is_logged_as_unknown():
"""Interpreter-shutdown edge cases may not carry a thread object."""
previous_hook = MagicMock()
args = _exception_args(RuntimeError("orphaned failure"), None)
with _installed_hook(previous_hook) as hook:
with patch("local_deep_research.web.app.logger") as mock_logger:
hook(args)
logged = mock_logger.error.call_args.args[0]
assert "thread 'unknown'" in logged
assert "orphaned failure" in logged
previous_hook.assert_called_once_with(args)
def test_perform_post_login_tasks_catches_outer_exceptions(
real_post_login_tasks,
):
"""If the outer structure of _perform_post_login_tasks itself raises
(for example from inside a `with` context manager's __enter__) —
not inside a per-step try/except — the wrapper must log the
exception and not let the daemon thread die silently.
The crash log is a loguru brace-format call with positional
arguments (``"... user {} ({})", sanitize_for_log(username),
type(err).__name__``), not an f-string, so ``call_args[0][0]`` is
only the template. The assertions render the template with its
arguments the way loguru does (``str.format``) instead of reading
the bare first argument, which no longer carries the username or
the exception type.
"""
from local_deep_research.web.routers import auth as routes
with (
patch.object(
routes,
"_perform_post_login_tasks_body",
side_effect=RuntimeError("outer failure"),
),
patch.object(routes, "logger") as mock_logger,
):
# Must not raise.
real_post_login_tasks("alice", "pw", "sess-1")
# Logged as a WARNING with the exception TYPE only: this frame
# holds the plaintext password, so no traceback may be rendered
# from it (no logger.exception, no exc_info, no opt(exception=)).
mock_logger.exception.assert_not_called()
mock_logger.opt.assert_not_called()
assert mock_logger.warning.called
args = mock_logger.warning.call_args.args
assert not mock_logger.warning.call_args.kwargs
msg = args[0].format(*args[1:])
assert "alice" in msg
assert "crashed" in msg.lower() or "post-login" in msg.lower()
assert "RuntimeError" in msg
assert "outer failure" not in msg
assert all("outer failure" not in str(arg) for arg in args)
def test_perform_post_login_tasks_body_runs_when_no_outer_error(
real_post_login_tasks,
):
"""Positive path: when the body succeeds, the wrapper forwards to
it exactly once with the right args, and does not log any
crash.
"""
from local_deep_research.web.routers import auth as routes
body = MagicMock()
with (
patch.object(routes, "_perform_post_login_tasks_body", body),
patch.object(routes, "logger") as mock_logger,
):
real_post_login_tasks("bob", "pw", "sess-1")
body.assert_called_once_with("bob", "pw", "sess-1")
mock_logger.exception.assert_not_called()
mock_logger.warning.assert_not_called()