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.

5.9 KB
Raw Blame
"""Import-resolution failure in expired-session socket teardown.

``SessionManager.cleanup_expired_sessions`` deletes expired sessions and
then calls ``_disconnect_expired_sockets`` to sever the sockets still
authorised by them — a socket is authenticated once at handshake and frozen
afterwards, so without the teardown an idle-expired session's tab keeps
receiving that user's events (including ``settings_changed``, which carries
plaintext secrets). That per-session disconnect behavior, and its
per-session exception isolation, are already pinned in
``tests/web/auth/test_session_manager.py::TestExpiredSessionSocketTeardown``.

The FIRST failure mode this function has — resolving the socket layer
itself — is not new ground:
``tests/web/auth/test_session_manager.py::test_socket_layer_import_failure_is_best_effort``
(:511) already drives the same ``sys.modules[...] = None`` condition
through ``cleanup_expired_sessions`` and asserts deletion plus the
warning, and the same class pins the resolvable path's per-session
dispatch and isolation. What this file adds is that failure driven at
the narrowest seam: ``TestSocketLayerResolutionFailure`` calls the
teardown static method DIRECTLY in both directions, unresolvable and
resolvable, side by side — so an ``ImportError`` swallowed in the wrong
place, or a lookup result quietly discarded, is caught without a
SessionManager instance or an expiry clock in the way.

The middle test here,
``test_cleanup_still_deletes_sessions_when_socket_layer_unresolvable``,
is deliberately NOT at that seam: it goes through
``cleanup_expired_sessions``, which makes it a narrower restatement of
the existing :511 test: same condition, same entry point, with the
logger assertion dropped. It asserts ``session_id not in
manager.sessions`` and ``manager.get_user_sessions("alice") == []``, but
the second assertion adds nothing on its own: ``get_user_sessions``
(``session_manager.py:219-233``) has no per-user index, it just
linearly scans ``self.sessions``, so its emptiness for this user is
already entailed by the first assertion. The real delta against the
:511 test — which already asserts ``expired_sid not in
manager.sessions`` at :529 — is the dropped logger assertion. It is
kept as the end-to-end anchor for the two seam-level tests around it,
not as new coverage of a new failure mode.

The lazy import exists to break an import cycle and to tolerate the
socket layer's absence in non-web contexts (CLI, narrow test
environments). Its Flask ancestor raised ``ModuleNotFoundError`` into
a bare ``except`` and silently disconnected nothing — the exact
regression class the docstring on the function records. This file
pins the ASGI successor against repeating it in either direction:

1. An unresolvable socket layer must not break session cleanup — the
   sessions are already deleted by the time the teardown runs, so a raise
   here would abort ``cleanup_expired_sessions`` after the mutation but
   before its callers' remaining work. Its two callers are
   ``SessionManager.get_active_sessions_count``
   (``session_manager.py:215``) and ``cleanup_idle_connections``
   (``connection_cleanup.py:158``, an APScheduler job every ~300s that
   goes on to close idle DB connections). ``validate_session`` does NOT
   call it — it expires one session inline instead
   (``session_manager.py:89-95``) — so this is not on the per-request auth
   path; what a raise here breaks is the periodic connection reclaim and
   the session count, forever, for one missing import.
2. Nor may it silently return SUCCESS-shaped behavior forever: pinned
   alongside (1) is that when the layer IS resolvable, the same call
   reaches ``disconnect_session`` for every expired id (guarding against a
   future "defensive" try/except that swallows the lookup result).
"""

import sys

from local_deep_research.web.auth.session_manager import SessionManager

_SOCKET_MODULE = "local_deep_research.web.services.socketio_asgi"


class TestSocketLayerResolutionFailure:
    def test_unresolvable_socket_layer_is_survived(self, monkeypatch):
        # ``sys.modules[...] = None`` makes ``from ... import ...`` raise
        # ImportError ("import halted; None in sys.modules"), which is the
        # closest deterministic stand-in for "socket layer unresolvable"
        # without uninstalling real modules.
        monkeypatch.setitem(sys.modules, _SOCKET_MODULE, None)

        # Must not raise — session cleanup depends on this surviving.
        SessionManager._disconnect_expired_sockets(["sess-1", "sess-2"])

    def test_cleanup_still_deletes_sessions_when_socket_layer_unresolvable(
        self, monkeypatch
    ):
        monkeypatch.setitem(sys.modules, _SOCKET_MODULE, None)

        manager = SessionManager()
        session_id = manager.create_session("alice")
        # Force expiry without waiting out the timeout: age the last_access
        # past both the normal and remember-me windows.
        manager.sessions[session_id]["last_access"] -= (
            manager.remember_me_timeout * 2
        )

        manager.cleanup_expired_sessions()

        # Observed WITHOUT calling validate_session: that method expires a
        # stale session inline (session_manager.py:89-95), so asking it
        # would delete the session itself and the assertion would pass
        # even if cleanup_expired_sessions had done nothing at all. Read
        # the store directly instead, so what is observed is the SUT's own
        # deletion surviving the failed socket-layer import.
        assert session_id not in manager.sessions
        assert manager.get_user_sessions("alice") == []

    def test_resolvable_layer_still_disconnects_every_session(
        self, monkeypatch
    ):
        disconnected = []
        monkeypatch.setattr(
            f"{_SOCKET_MODULE}.disconnect_session",
            lambda sid: disconnected.append(sid),
        )

        SessionManager._disconnect_expired_sockets(["sess-1", "sess-2"])

        assert disconnected == ["sess-1", "sess-2"]