~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
"""Ported from two deleted Flask files with no successor on this branch.
Sources (both present on ``origin/main``, absent here):
* ``tests/web/services/test_socket_service_init.py``
* ``tests/web/services/test_socket_service_extra_coverage.py``
Most of what those files pinned DOES have a successor here and is not
repeated: the WebSocket origin policy and the origin-rejection logging hook
are covered by ``tests/security/test_socket_ownership_edges_fastapi.py``
(``TestWebSocketOriginPolicyDerivation``, ``TestOriginRejectionLoggingHook``);
the connect gate by ``test_socketio_connect_gate.py`` and
``test_socket_connect_session_gate.py``; the subscribe handler by
``tests/chat/test_chat_socket_events.py::TestSubscribeStatusPush`` and
``test_state_changing_flows.py::test_subscribe_ignores_missing_research_id``;
the disconnect sweep by ``test_socketio_asgi_contracts.py``
::TestDisconnectCleanupAcrossOwners and
``test_subscription_owner_scoping.py``.
What had NO successor, and is restored here:
1. ``test_socketio_async_mode_threading`` -- the server's async mode. Main
asserted ``async_mode == "threading"``; the ASGI translation is
``"asgi"``. Nothing on this branch asserts it: the mount tests pin the
PATH (``socket_app.engineio_path`` / the ``/ws`` Mount), which an
``AsyncServer`` built in the wrong mode still exposes.
2. ``test_cleanup_import_error_swallowed`` / ``test_cleanup_exception_swallowed``
-- the thread-local DB session cleanup at the end of ``disconnect`` is
best-effort. ``grep -rn cleanup_current_thread tests/`` finds nothing in
any socket test on this branch.
3. ``test_outer_exception_swallowed`` -- ``__handle_disconnect`` on main
wrapped its whole body in ``try/except`` so a disconnect could never
raise. See that test's own docstring for what the missing guard costs
here.
"""
import asyncio
import sys
from unittest.mock import MagicMock
import pytest
from local_deep_research.web.services import socketio_asgi as sio_mod
CLEANUP_MODULE = "local_deep_research.database.thread_local_session"
@pytest.fixture
def socket_state(monkeypatch):
"""Isolated sid/subscription state, as in test_subscription_owner_scoping."""
sio_mod.init_lock()
monkeypatch.setattr(sio_mod, "_subscriptions", {})
monkeypatch.setattr(sio_mod, "_sid_users", {})
monkeypatch.setattr(sio_mod, "_sid_sessions", {})
return sio_mod
def _seed(sid="client-1", username="alice", research_id="res-1"):
sio_mod._sid_users[sid] = username
sio_mod._sid_sessions[sid] = "sess-1"
sio_mod._subscriptions.setdefault(
sio_mod._subscription_key(username, research_id), set()
).add(sid)
# ---------------------------------------------------------------------------
# 1. Server construction
# ---------------------------------------------------------------------------
def test_the_live_server_is_built_for_the_asgi_transport():
"""Port of ``test_socketio_async_mode_threading``.
Main pinned ``async_mode="threading"`` because Flask-SocketIO would
otherwise pick an eventlet/gevent backend that its WSGI server could not
drive. The same argument holds inverted here: ``socketio.ASGIApp`` only
works with an ``AsyncServer`` built for ``asgi``, and a wrong mode is a
silently frozen realtime UI rather than a startup error.
"""
assert sio_mod.sio.async_mode == "asgi"
assert sio_mod.sio.eio.async_mode == "asgi"
# ---------------------------------------------------------------------------
# 2 + 3. disconnect() teardown is best-effort
# ---------------------------------------------------------------------------
class TestDisconnectThreadSessionCleanupIsBestEffort:
"""``disconnect`` ends by closing this thread's SQLAlchemy session.
That is a leak-prevention nicety, not part of severing the socket, so it
must never be able to abort the handler: python-socketio's
``_trigger_event`` does NOT swallow handler exceptions, and
``_handle_disconnect`` only calls ``manager.disconnect(...)`` -- which
removes the sid from engine.io's own room bookkeeping -- AFTER the
handler returns.
"""
def test_a_missing_cleanup_module_does_not_break_disconnect(
self, socket_state, monkeypatch
):
"""Port of ``test_cleanup_import_error_swallowed``."""
_seed()
monkeypatch.setitem(sys.modules, CLEANUP_MODULE, None)
asyncio.run(sio_mod.disconnect("client-1"))
assert sio_mod._sid_users == {}
assert sio_mod._sid_sessions == {}
assert sio_mod._subscriptions == {}
def test_a_raising_cleanup_current_thread_does_not_break_disconnect(
self, socket_state, monkeypatch
):
"""Port of ``test_cleanup_exception_swallowed``."""
_seed()
fake = MagicMock()
fake.cleanup_current_thread.side_effect = RuntimeError("cleanup fail")
monkeypatch.setitem(sys.modules, CLEANUP_MODULE, fake)
asyncio.run(sio_mod.disconnect("client-1"))
assert fake.cleanup_current_thread.called, (
"the failing cleanup was never reached, so 'it did not raise' "
"would pass for free"
)
assert sio_mod._sid_users == {}
assert sio_mod._subscriptions == {}
def test_disconnect_never_propagates_an_exception(socket_state, monkeypatch):
"""Port of ``test_cleanup_exception_swallowed``'s sibling,
``test_outer_exception_swallowed``.
Main's ``__handle_disconnect`` wrapped its entire body -- lock
acquisition, subscription sweep, thread cleanup -- in one ``try/except``
so that handling a disconnect could not raise. This branch's
``disconnect`` guards only the thread-cleanup tail, so anything that
fails while holding ``_lock`` escapes the handler.
That is not merely cosmetic here. ``AsyncServer._handle_disconnect``
awaits our handler and only then calls
``manager.disconnect(sid, ..., ignore_queue=True)``; an exception from
the handler skips that call, so the sid stays in engine.io's own room
membership while our maps are left half-swept -- the opposite of what a
disconnect is for.
Main's version made the lock's ``__enter__`` raise; this is the same
injection against ``__aenter__``.
"""
class _FailingLock:
async def __aenter__(self):
raise RuntimeError("lock fail")
async def __aexit__(self, *exc):
return False
_seed()
monkeypatch.setattr(sio_mod, "_lock", _FailingLock())
asyncio.run(sio_mod.disconnect("client-1"))