~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 automatic database connection cleanup.
"""
import datetime
from datetime import UTC
from unittest.mock import MagicMock, patch
import pytest
from local_deep_research.web.auth.connection_cleanup import (
cleanup_idle_connections,
start_connection_cleanup_scheduler,
)
from local_deep_research.web.auth.session_manager import SessionManager
@pytest.fixture
def sm():
"""Create a fresh SessionManager with short timeouts for testing."""
with patch(
"local_deep_research.web.auth.session_manager.get_security_default",
return_value=1,
):
mgr = SessionManager()
# Use a very short timeout for testing
mgr.session_timeout = datetime.timedelta(seconds=1)
mgr.remember_me_timeout = datetime.timedelta(seconds=2)
return mgr
@pytest.fixture
def db():
"""Create a mock DatabaseManager."""
mock = MagicMock()
mock.get_connected_usernames.return_value = set()
return mock
class TestCleanupIdleConnections:
"""Tests for cleanup_idle_connections()."""
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_closes_connection_no_sessions_no_research(
self, _mock_research, _mock_sched, sm, db
):
"""Connection closed when user has no active sessions and no research."""
db.get_connected_usernames.return_value = {"alice"}
cleanup_idle_connections(sm, db)
db.close_user_database.assert_called_once_with("alice")
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_keeps_connection_with_active_session(self, _mock, sm, db):
"""Connection NOT closed when user still has an active session."""
sm.create_session("bob")
db.get_connected_usernames.return_value = {"bob"}
cleanup_idle_connections(sm, db)
db.close_user_database.assert_not_called()
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value={"carol"},
)
def test_keeps_connection_with_active_research(self, _mock, sm, db):
"""Connection NOT closed when user has active research."""
db.get_connected_usernames.return_value = {"carol"}
cleanup_idle_connections(sm, db)
db.close_user_database.assert_not_called()
@patch(
# The Flask socket_service module was deleted by the migration; the
# idle sweep now calls the ASGI layer's module-level disconnect_user.
"local_deep_research.web.services.socketio_asgi.disconnect_user",
)
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_disconnects_all_sockets_for_idle_user(
self, _mock_research, _mock_sched, mock_disconnect_user, sm, db
):
"""Idle-close disconnects ALL of the idle user's sockets.
The user has no active session at all here, so every one of their
still-open sockets (authorised once at handshake and never re-checked)
must be severed — disconnect_user, not disconnect_session.
"""
db.get_connected_usernames.return_value = {"alice"}
cleanup_idle_connections(sm, db)
db.close_user_database.assert_called_once_with("alice")
# The ASGI layer exposes disconnect_user as a module-level function,
# not a service class to instantiate.
mock_disconnect_user.assert_called_once_with("alice")
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_multiple_users_handled_independently(
self, _mock_research, _mock_sched, sm, db
):
"""Each user is evaluated independently."""
sm.create_session("dave") # active session
db.get_connected_usernames.return_value = {"dave", "eve"}
cleanup_idle_connections(sm, db)
# eve has no session, should be closed; dave should not
db.close_user_database.assert_called_once_with("eve")
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_double_check_prevents_race(self, _mock, sm, db):
"""If user logs in between snapshot and close, connection is kept."""
db.get_connected_usernames.return_value = {"frank"}
# Simulate: frank has no session at snapshot time, but gains one
# during the candidate iteration (via has_active_sessions_for).
original_has = sm.has_active_sessions_for
def fake_has(username):
if username == "frank":
# Simulate login between snapshot and close
sm.create_session("frank")
return original_has("frank")
return original_has(username)
sm.has_active_sessions_for = fake_has
cleanup_idle_connections(sm, db)
db.close_user_database.assert_not_called()
def test_double_check_research_prevents_race(self, sm, db):
"""If user starts research between snapshot and close, connection is kept."""
db.get_connected_usernames.return_value = {"heidi"}
call_count = 0
def research_side_effect():
nonlocal call_count
call_count += 1
if call_count == 1:
return set() # First call: no research (snapshot phase)
return {
"heidi"
} # Second call: research started (double-check phase)
with patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
side_effect=research_side_effect,
):
cleanup_idle_connections(sm, db)
db.close_user_database.assert_not_called()
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_expired_sessions_purged_before_check(
self, _mock_research, _mock_sched, sm, db
):
"""Expired sessions are cleaned up before determining active users."""
# Create session, then expire it
sid = sm.create_session("grace")
with sm._lock:
sm.sessions[sid]["last_access"] = datetime.datetime.now(
UTC
) - datetime.timedelta(hours=5)
db.get_connected_usernames.return_value = {"grace"}
cleanup_idle_connections(sm, db)
# Session expired, so connection should be closed
db.close_user_database.assert_called_once_with("grace")
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_no_connections_is_noop(self, _mock, sm, db):
"""No-op when there are no open connections."""
db.get_connected_usernames.return_value = set()
cleanup_idle_connections(sm, db)
db.close_user_database.assert_not_called()
def test_close_failure_does_not_abort_loop(self, sm, db):
"""If close_user_database raises for one user, others are still closed."""
db.get_connected_usernames.return_value = {"alice", "bob"}
def selective_raise(username):
if username == "alice":
raise RuntimeError("simulated failure")
db.close_user_database.side_effect = selective_raise
with (
patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
),
patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
),
):
cleanup_idle_connections(sm, db)
assert db.close_user_database.call_count == 2
db.close_user_database.assert_any_call("alice")
db.close_user_database.assert_any_call("bob")
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_unregister_user_called_on_idle_close(
self, _mock_research, mock_get_sched, sm, db
):
"""Scheduler unregister_user is called before closing idle connection."""
mock_scheduler = MagicMock()
mock_scheduler.is_running = True
mock_get_sched.return_value = mock_scheduler
db.get_connected_usernames.return_value = {"alice"}
cleanup_idle_connections(sm, db)
mock_scheduler.unregister_user.assert_called_once_with("alice")
db.close_user_database.assert_called_once_with("alice")
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_scheduler_failure_does_not_block_close(
self, _mock_research, mock_get_sched, sm, db
):
"""If scheduler unregister raises, db close still proceeds."""
mock_scheduler = MagicMock()
mock_scheduler.is_running = True
mock_scheduler.unregister_user.side_effect = RuntimeError(
"scheduler down"
)
mock_get_sched.return_value = mock_scheduler
db.get_connected_usernames.return_value = {"alice"}
cleanup_idle_connections(sm, db)
db.close_user_database.assert_called_once_with("alice")
@patch(
"local_deep_research.web.auth.connection_cleanup.session_password_store.clear_all_for_user"
)
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_clear_all_for_user_called_on_idle_close(
self, _mock_research, mock_get_sched, mock_clear_pwd, sm, db
):
"""Session password store is cleared when closing idle connection."""
mock_scheduler = MagicMock()
mock_scheduler.is_running = True
mock_get_sched.return_value = mock_scheduler
db.get_connected_usernames.return_value = {"alice"}
cleanup_idle_connections(sm, db)
mock_clear_pwd.assert_called_once_with("alice")
@patch(
"local_deep_research.web.auth.connection_cleanup.clear_user_credentials"
)
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_thread_credentials_cleared_on_idle_close(
self, _mock_research, mock_get_sched, mock_clear_creds, sm, db
):
"""Cached plaintext SQLCipher keys are dropped on the idle path too.
Mirrors the logout assertion in
``tests/security/test_logout_clears_thread_credentials.py``. Pooled
AnyIO worker threads cache ``(username, password)``; Flask released
that entry every request from the request-serving thread, but the
FastAPI hook is async and runs on the event-loop thread, so it can
never reach a worker's entry.
Logout was fixed for this; the idle sweeper was not — and the sweeper
is the teardown path for the majority of users, who close the tab
rather than clicking logout. Without this the master key outlived the
server's own decision that the user was gone.
"""
mock_scheduler = MagicMock()
mock_scheduler.is_running = True
mock_get_sched.return_value = mock_scheduler
db.get_connected_usernames.return_value = {"alice"}
cleanup_idle_connections(sm, db)
mock_clear_creds.assert_called_once_with("alice")
@patch(
"local_deep_research.web.auth.connection_cleanup.clear_user_credentials"
)
@patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
)
@patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
)
def test_thread_credentials_cleared_even_if_close_raises(
self, _mock_research, mock_get_sched, mock_clear_creds, sm, db
):
"""The credential clear must not sit behind a failing DB close.
Same reasoning already applied to ``_pop_per_user_locks``: the failure
path is exactly where leftover state matters most.
"""
mock_scheduler = MagicMock()
mock_scheduler.is_running = True
mock_get_sched.return_value = mock_scheduler
db.get_connected_usernames.return_value = {"alice"}
db.close_user_database.side_effect = RuntimeError("cleanup exploded")
cleanup_idle_connections(sm, db)
mock_clear_creds.assert_called_once_with("alice")
def test_socket_disconnect_failure_does_not_skip_other_users_or_cleanup(
self, sm, db
):
"""One broken Socket.IO teardown cannot abort the idle-user sweep."""
users = {"alice", "bob"}
db.get_connected_usernames.return_value = users
def _disconnect(username):
if username == "alice":
raise RuntimeError("socket loop stopped")
with (
patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
),
patch(
"local_deep_research.scheduler.background.get_background_job_scheduler"
),
patch(
"local_deep_research.web.services.socketio_asgi.disconnect_user",
side_effect=_disconnect,
) as disconnect,
patch(
"local_deep_research.web.auth.connection_cleanup.clear_user_credentials"
) as clear_credentials,
patch(
"local_deep_research.web.auth.connection_cleanup._pop_per_user_locks"
) as pop_locks,
):
cleanup_idle_connections(sm, db)
assert {call.args[0] for call in disconnect.call_args_list} == users
assert {
call.args[0] for call in clear_credentials.call_args_list
} == users
assert {call.args[0] for call in pop_locks.call_args_list} == users
assert {
call.args[0] for call in db.close_user_database.call_args_list
} == users
def test_password_store_failure_isolated_per_user_and_cleanup_continues(
self, sm, db
):
"""Credential-store teardown is best-effort for each idle user."""
users = {"alice", "bob"}
db.get_connected_usernames.return_value = users
def _clear_passwords(username):
if username == "alice":
raise RuntimeError("password store unavailable")
with (
patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
),
patch(
"local_deep_research.scheduler.background.get_background_job_scheduler"
),
patch(
"local_deep_research.web.auth.connection_cleanup.session_password_store.clear_all_for_user",
side_effect=_clear_passwords,
) as clear_passwords,
patch(
"local_deep_research.web.auth.connection_cleanup.clear_user_credentials"
) as clear_credentials,
patch(
"local_deep_research.web.auth.connection_cleanup._disconnect_all_user_sockets"
) as disconnect,
patch(
"local_deep_research.web.auth.connection_cleanup._pop_per_user_locks"
) as pop_locks,
):
cleanup_idle_connections(sm, db)
assert {
call.args[0] for call in clear_passwords.call_args_list
} == users
assert {
call.args[0] for call in db.close_user_database.call_args_list
} == users
for cleanup in (clear_credentials, disconnect, pop_locks):
assert {call.args[0] for call in cleanup.call_args_list} == users
def test_sweep_evicts_idle_users_collection_engine_cache(self, sm, db):
"""The sweep is the teardown path for most users, so it must drop
an idle user's cached collection engines (collection names and
descriptions) like logout does (#6609). It does so even when the DB
close raises, and leaves a still-protected user's entry alone."""
import time
from local_deep_research.web_search_engines import (
search_engines_config as sec,
)
idle = "test-sweep-evict-idle-zzz"
active = "test-sweep-evict-active-zzz"
db.get_connected_usernames.return_value = {idle, active}
db.close_user_database.side_effect = RuntimeError(
"simulated DB close failure"
)
with sec._COLLECTION_CACHE_LOCK:
for user in (idle, active):
sec._COLLECTION_ENGINES_CACHE[user] = (
time.monotonic(),
{"collection_x": {"display_name": "x (Collection)"}},
)
try:
sm.create_session(active)
with (
patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
),
patch(
"local_deep_research.scheduler.background.get_background_job_scheduler"
),
):
cleanup_idle_connections(sm, db)
with sec._COLLECTION_CACHE_LOCK:
assert idle not in sec._COLLECTION_ENGINES_CACHE
assert active in sec._COLLECTION_ENGINES_CACHE
finally:
with sec._COLLECTION_CACHE_LOCK:
for user in (idle, active):
sec._COLLECTION_ENGINES_CACHE.pop(user, None)
class TestPopPerUserLocks:
"""Tests for ``_pop_per_user_locks`` cache cleanup.
Plain per-user locks keep stable identity because deleting one behind a
held or looked-up reference defeats same-user serialisation. The tracked
FAISS cache can safely evict idle entries.
"""
def test_preserves_plain_lock_identity_but_pops_tracked_faiss_cache(self):
from local_deep_research.web.auth.connection_cleanup import (
_pop_per_user_locks,
)
from local_deep_research.database.library_init import (
_get_user_init_lock,
_user_init_locks,
)
from local_deep_research.database.backup.backup_service import (
_get_user_lock,
_user_locks,
)
from local_deep_research.web.queue.processor_v2 import (
queue_processor,
)
from local_deep_research.research_library.services.library_rag_service import (
_faiss_write_locks,
_get_faiss_write_lock,
)
# Populate each dict with a unique test username so we don't
# collide with state any other test might have left behind.
u = "test-pop-locks-user-zzz"
init_lock = _get_user_init_lock(u)
backup_lock = _get_user_lock(u)
queue_lock = queue_processor._get_user_critical_lock(u)
_get_faiss_write_lock(u, "/tmp/test-pop-locks/idx.faiss")
assert u in _user_init_locks
assert u in _user_locks
assert u in queue_processor._user_critical_locks
assert any(k[0] == u for k in _faiss_write_locks)
_pop_per_user_locks(u)
assert _get_user_init_lock(u) is init_lock
assert _get_user_lock(u) is backup_lock
assert u in _user_init_locks
assert u in _user_locks
assert queue_processor._get_user_critical_lock(u) is queue_lock
assert u in queue_processor._user_critical_locks
assert not any(k[0] == u for k in _faiss_write_locks)
def test_idempotent_on_missing_user(self):
"""Pop on a username that was never inserted must not raise."""
from local_deep_research.web.auth.connection_cleanup import (
_pop_per_user_locks,
)
# Should silently no-op.
_pop_per_user_locks("never-registered-user-zzz")
def test_one_pop_failure_does_not_skip_the_remaining_lock_registries(self):
"""The four caches have independent best-effort boundaries."""
from local_deep_research.web.auth.connection_cleanup import (
_pop_per_user_locks,
)
with (
patch(
"local_deep_research.database.library_init.pop_user_init_lock",
side_effect=RuntimeError("library init lock busy"),
) as pop_init,
patch(
"local_deep_research.database.backup.backup_service.pop_user_lock"
) as pop_backup,
patch(
"local_deep_research.web.queue.processor_v2.queue_processor.pop_user_critical_lock"
) as pop_queue,
patch(
"local_deep_research.research_library.services.library_rag_service.pop_faiss_locks_for_user"
) as pop_faiss,
patch(
"local_deep_research.web.auth.connection_cleanup.logger"
) as mock_logger,
):
_pop_per_user_locks("alice")
pop_init.assert_called_once_with("alice")
pop_backup.assert_called_once_with("alice")
pop_queue.assert_called_once_with("alice")
pop_faiss.assert_called_once_with("alice")
mock_logger.warning.assert_any_call(
"Failed to pop _user_init_locks for alice"
)
def test_pop_called_from_idle_close_path(self, sm, db):
"""Integration: ``cleanup_idle_connections`` calls
``_pop_per_user_locks`` for each user it closes. Plain-lock identities
remain stable while the tracked FAISS cache safely evicts idle locks.
"""
from local_deep_research.database.library_init import (
_get_user_init_lock,
_user_init_locks,
)
from local_deep_research.database.backup.backup_service import (
_get_user_lock,
_user_locks,
)
from local_deep_research.web.queue.processor_v2 import (
queue_processor,
)
from local_deep_research.research_library.services.library_rag_service import (
_faiss_write_locks,
_get_faiss_write_lock,
)
# Use a dedicated test username (not "alice") to avoid colliding
# with other tests that may also touch these module-level dicts.
u = "test-idle-close-user-zzz"
init_lock = _get_user_init_lock(u)
backup_lock = _get_user_lock(u)
queue_lock = queue_processor._get_user_critical_lock(u)
_get_faiss_write_lock(u, "/tmp/test-idle-close/idx.faiss")
assert u in _user_init_locks
assert u in _user_locks
assert u in queue_processor._user_critical_locks
assert any(k[0] == u for k in _faiss_write_locks)
db.get_connected_usernames.return_value = {u}
with (
patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
),
patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
),
):
cleanup_idle_connections(sm, db)
assert _get_user_init_lock(u) is init_lock
assert _get_user_lock(u) is backup_lock
assert u in _user_init_locks
assert u in _user_locks
assert queue_processor._get_user_critical_lock(u) is queue_lock
assert u in queue_processor._user_critical_locks
assert not any(k[0] == u for k in _faiss_write_locks)
def test_pop_runs_even_when_close_user_database_fails(self, sm, db):
"""Regression for the original PR: ``_pop_per_user_locks`` was
previously inside the same try/except as ``close_user_database``,
so a DB-close failure (the very path
``test_close_failure_does_not_abort_loop`` exercises) would skip
the pop and leak the lock-dict entry. Now the pop is outside
the try; this test pins that behavior.
"""
from local_deep_research.database.library_init import (
_get_user_init_lock,
_user_init_locks,
)
u = "test-close-fails-user-zzz"
init_lock = _get_user_init_lock(u)
assert u in _user_init_locks
db.get_connected_usernames.return_value = {u}
db.close_user_database.side_effect = RuntimeError(
"simulated DB close failure"
)
with (
patch(
"local_deep_research.web.auth.connection_cleanup.get_usernames_with_active_research",
return_value=set(),
),
patch(
"local_deep_research.scheduler.background.get_background_job_scheduler",
),
):
cleanup_idle_connections(sm, db)
# Despite close_user_database raising, cleanup still reaches the
# compatibility hook without replacing the canonical lock.
assert _get_user_init_lock(u) is init_lock
assert u in _user_init_locks
def test_research_start_gate_identity_stable_across_pop(self):
"""The per-user research-start gate is INTENTIONALLY NOT popped on
user-close, just like the queue processor's direct-start lock above.
It is a mutual-exclusion primitive that may be HELD across a
multi-second SQLCipher rekey (see ``change_password``). If
``_pop_per_user_locks`` removed a held gate, a concurrent same-user
``check_and_start_research`` would find no entry, create a SECOND
``threading.Lock`` instance, acquire it uncontended, and start a
worker that writes the user's DB concurrently with the in-flight
rekey — bypassing the exclusion the gate exists to provide.
This pins that the gate's IDENTITY is stable across a logout /
idle-close: ``_get_user_research_start_gate`` returns the SAME object
before and after ``_pop_per_user_locks`` for the same username, so
the gate is never removed-and-recreated behind a held reference.
"""
from local_deep_research.web.auth.connection_cleanup import (
_pop_per_user_locks,
)
# routes/globals is a re-export shim and only re-exports the PUBLIC
# gate helper; these private ones live in research_state itself.
from local_deep_research.web.research_state import (
_get_user_research_start_gate,
_user_research_start_gates,
_user_research_start_gates_lock,
)
u = "test-gate-identity-user-zzz"
try:
gate_before = _get_user_research_start_gate(u)
# The user-close cleanup must NOT drop the gate.
_pop_per_user_locks(u)
gate_after = _get_user_research_start_gate(u)
# SAME object: the gate was neither removed nor recreated.
assert gate_after is gate_before
assert u in _user_research_start_gates
finally:
# The gate is never popped in production; clear this test's
# entry directly so it doesn't leak into other tests.
with _user_research_start_gates_lock:
_user_research_start_gates.pop(u, None)
class TestStartConnectionCleanupScheduler:
"""Tests for start_connection_cleanup_scheduler()."""
@patch(
"local_deep_research.web.auth.connection_cleanup.BackgroundScheduler"
)
def test_returns_running_scheduler(self, MockScheduler, sm, db):
"""Verify scheduler starts and returns a BackgroundScheduler."""
mock_instance = MagicMock()
MockScheduler.return_value = mock_instance
result = start_connection_cleanup_scheduler(sm, db)
assert result is mock_instance
mock_instance.start.assert_called_once()
@patch(
"local_deep_research.web.auth.connection_cleanup.BackgroundScheduler"
)
def test_uses_correct_interval_and_jitter(self, MockScheduler, sm, db):
"""Verify the job is added with correct interval and jitter."""
mock_instance = MagicMock()
MockScheduler.return_value = mock_instance
start_connection_cleanup_scheduler(sm, db)
mock_instance.add_job.assert_called_once_with(
cleanup_idle_connections,
"interval",
seconds=300,
args=[sm, db],
id="cleanup_idle_connections",
jitter=30,
)
@patch(
"local_deep_research.web.auth.connection_cleanup.BackgroundScheduler"
)
def test_custom_interval(self, MockScheduler, sm, db):
"""Verify custom interval_seconds parameter is respected."""
mock_instance = MagicMock()
MockScheduler.return_value = mock_instance
start_connection_cleanup_scheduler(sm, db, interval_seconds=60)
mock_instance.add_job.assert_called_once_with(
cleanup_idle_connections,
"interval",
seconds=60,
args=[sm, db],
id="cleanup_idle_connections",
jitter=30,
)
class TestSessionManagerThreadSafety:
"""Verify SessionManager operations don't crash under concurrent access."""
def test_concurrent_create_and_cleanup(self, sm):
"""Create and cleanup sessions concurrently without RuntimeError."""
import threading
errors = []
def create_sessions():
try:
for i in range(50):
sm.create_session(f"user_{i}")
except Exception as e:
errors.append(e)
def cleanup_sessions():
try:
for _ in range(50):
sm.cleanup_expired_sessions()
except Exception as e:
errors.append(e)
t1 = threading.Thread(target=create_sessions)
t2 = threading.Thread(target=cleanup_sessions)
t1.start()
t2.start()
t1.join()
t2.join()
assert not errors, f"Concurrent access errors: {errors}"
def test_get_active_usernames_snapshot(self, sm):
"""get_active_usernames returns a set, not a view."""
sm.create_session("user_a")
sm.create_session("user_b")
result = sm.get_active_usernames()
assert isinstance(result, set)
assert result == {"user_a", "user_b"}
def test_has_active_sessions_for_returns_false_when_none(self, sm):
assert sm.has_active_sessions_for("nobody") is False
def test_has_active_sessions_for_returns_true_when_active(self, sm):
sm.create_session("active_user")
assert sm.has_active_sessions_for("active_user") is True