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.

15.4 KB
Raw Blame
"""
Automatic cleanup of idle database connections.

Periodically closes database connections for users who have no active sessions
and no active research, preventing resource leaks when users close their browser
without logging out.

Also periodically disposes all QueuePool engines to release accumulated WAL/SHM
file handles. See ADR-0004 for why this is necessary with SQLCipher + WAL mode.
"""

import os
import time
from pathlib import Path

from apscheduler.schedulers.background import BackgroundScheduler
from loguru import logger

from ...database.session_passwords import session_password_store
from ...database.thread_local_session import (
    cleanup_dead_threads,
    clear_user_credentials,
)
from ...web.research_state import get_usernames_with_active_research

# ---------------------------------------------------------------------------
# File Descriptor Monitoring
# ---------------------------------------------------------------------------
# WHY: After days of idle operation in Docker, the app crashed with
#   OSError: [Errno 24] Too many open files
# This monitoring logs the FD count every 5 minutes so we can correlate
# FD growth with specific events and find leaks.
#
# WHAT IT LOGS:
#   - open_fds: total open file descriptors for the process
#   - pool_engines: number of per-user QueuePool engines
#   - pool_checked_out: connections currently checked out from QueuePool
#   - protected_users: users with active sessions
#
# HOW TO USE: grep "Resource monitor" in container logs. If open_fds
# grows steadily over hours, something is leaking.
# ---------------------------------------------------------------------------

# Dispose all pool engines every 30 minutes to release WAL/SHM handles.
# SQLCipher + WAL mode leaks handles when connections close out of order
# (which QueuePool's pool_recycle causes). Periodic dispose() closes ALL
# pooled connections at once, resetting the handle state cleanly.
# The next DB operation transparently reopens a fresh connection.
_DISPOSE_INTERVAL_SECONDS = 1800
_last_dispose_time = 0.0


def _pop_per_user_locks(username: str) -> None:
    """Release safe-to-remove per-user lock-cache entries for ``username``.

    The library-init, backup, queue-processor, and library-RAG modules each
    maintain per-user lock registries. Plain locks cannot be safely removed
    because a caller may already hold a reference before acquisition, so their
    compatibility cleanup hooks retain stable identity. The library-RAG cache
    uses tracked acquisition and can safely evict idle entries.

    The research-start gate, queue admission view, library-init lock and backup
    lock are bounded by the user population.

    Lazy-imported here to keep this module's import graph shallow:
    ``connection_cleanup`` runs at startup and shouldn't pull in the
    queue / backup / library-init / library-RAG modules eagerly.
    """
    try:
        from ...database.library_init import pop_user_init_lock

        pop_user_init_lock(username)
    except Exception:
        # Surface at WARNING to match the sibling scheduler-unregister
        # error handler in this same module (line ~111). A failure
        # here means the lock-dict entry will accumulate on every
        # subsequent close cycle for this user; we want it visible.
        logger.warning(f"Failed to pop _user_init_locks for {username}")

    try:
        from ...database.backup.backup_service import pop_user_lock

        pop_user_lock(username)
    except Exception:
        logger.warning(f"Failed to pop _user_locks for {username}")

    try:
        from ...web.queue.processor_v2 import queue_processor

        queue_processor.pop_user_critical_lock(username)
    except Exception:
        logger.warning(f"Failed to pop _user_critical_locks for {username}")

    # NOTE: the per-user research-start gate (_user_research_start_gates in
    # web/research_state.py) is deliberately NOT popped here — it may be held
    # across a multi-second rekey and removing a held gate would let a
    # concurrent same-user check_and_start_research create a second gate
    # instance and bypass the exclusion. See the docstring above.

    try:
        from ...research_library.services.library_rag_service import (
            pop_faiss_locks_for_user,
        )

        pop_faiss_locks_for_user(username)
    except Exception:
        logger.warning(f"Failed to pop _faiss_write_locks for {username}")


def _evict_collection_engines_cache(username: str) -> None:
    """Best-effort drop of ``username``'s cached collection search engines.

    The collection-engine cache in ``search_engines_config`` holds the user's
    collection names/descriptions for up to its 30 s TTL (5 minutes under
    stale-serve after a DB error). Like the lock entries above it is
    per-user residue, so every teardown path calls this: logout, password
    change, and the idle-connection sweep (#6609). Lazy-imported for the
    same import-graph reason as ``_pop_per_user_locks``.
    """
    try:
        from ...web_search_engines.search_engines_config import (
            evict_collection_engines_cache,
        )

        evict_collection_engines_cache(username)
    except Exception:
        logger.warning(
            f"Failed to evict cached collection engines for {username}"
        )


def _disconnect_all_user_sockets(username: str) -> None:
    """Best-effort disconnect of ALL of ``username``'s live sockets.

    Called from the idle-connection sweep, where the user has no active
    session at all — so every one of their still-open sockets (authorised
    once at handshake and never re-checked) should be severed. Lazy-imported
    to keep this module's startup import graph shallow and to tolerate
    non-web contexts (the socket server may not exist).

    Retargeted from the deleted Flask ``SocketIOService`` (#5535) onto the
    ASGI socket layer. Behaviour is equivalent: ``disconnect_user`` severs
    every sid authenticated as this user, and the resulting ``disconnect``
    handler drops their subscriptions. It schedules onto the main loop and
    returns False rather than raising when no loop is running, which is
    the non-web case main handled by catching ValueError.
    """
    try:
        from ...web.services.socketio_asgi import disconnect_user

        disconnect_user(username)
    except Exception:
        logger.warning(f"Failed to disconnect sockets for idle user {username}")


def _count_open_fds() -> int:
    """Count open file descriptors for the current process."""
    proc_fd = Path("/proc/self/fd")
    if proc_fd.is_dir():
        try:
            return len(list(proc_fd.iterdir()))
        except OSError:
            pass
    import resource

    soft_limit = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
    count = 0
    for fd in range(soft_limit):
        try:
            os.fstat(fd)
            count += 1
        except OSError:
            pass
    return count


def cleanup_idle_connections(session_manager, db_manager):
    """Close db connections for users with no active sessions and no active research."""
    from .session_manager import SessionValidationUnavailableError

    # 1. Purge expired sessions first
    session_manager.cleanup_expired_sessions()

    # 2. Get protected usernames (active sessions OR active research)
    active_usernames = session_manager.get_active_usernames()
    researching_usernames = get_usernames_with_active_research()
    protected = active_usernames | researching_usernames

    # 3. Get usernames with open connections
    connected_usernames = db_manager.get_connected_usernames()

    # 4. Find idle candidates
    candidates = connected_usernames - protected

    # 5. Double-check before closing (narrows race window)
    closed = 0
    for username in candidates:
        try:
            has_active_sessions = session_manager.has_active_sessions_for(
                username
            )
        except SessionValidationUnavailableError:
            # Unknown activity is insufficient evidence for shared teardown.
            # Request authentication still refuses an unreadable epoch.
            logger.warning("Deferred idle cleanup: session state unavailable")
            continue
        if has_active_sessions:
            logger.debug(
                f"Skipped {username} (active session appeared since snapshot)"
            )
            continue  # User logged in since snapshot
        if username in get_usernames_with_active_research():
            logger.debug(
                f"Skipped {username} (active research appeared since snapshot)"
            )
            continue  # Research started since snapshot
        # Unregister news scheduler jobs (matches logout pattern in routes.py)
        try:
            from ...scheduler.background import (
                get_background_job_scheduler,
            )

            sched = get_background_job_scheduler()
            if sched.is_running:
                sched.unregister_user(username)
        except Exception:
            logger.warning(
                f"Failed to unregister scheduler for {username}",
            )
        try:
            db_manager.close_user_database(username)
            session_password_store.clear_all_for_user(username)
            closed += 1
            logger.debug(f"Closed idle connection for {username}")
        except Exception:
            logger.warning(f"Connection cleanup failed for {username}")
        # Drop cached plaintext credentials on pooled worker threads, for the
        # same reason logout does. This sweep is the teardown path for the
        # MAJORITY of users — most close the tab rather than clicking logout —
        # so omitting it left the SQLCipher master key in a process-global
        # dict indefinitely after the server had already decided the user was
        # gone and closed their database. Outside the try above for the same
        # reason _pop_per_user_locks is: independent of engine teardown, and
        # it matters most on the path where close raises.
        clear_user_credentials(username)
        # This user has no active session at all (that's why we're closing
        # their DB), so tear down every one of their still-open sockets — a
        # socket authorised at handshake is never re-checked and would
        # otherwise keep receiving the user's events after the session lapsed.
        _disconnect_all_user_sockets(username)
        # Run lock-cache cleanup regardless of whether close succeeded.
        # Stable plain-lock registries keep their identities; tracked caches
        # can evict idle entries. This remains independent of engine teardown.
        _pop_per_user_locks(username)
        # The user's cached collection engines are teardown residue too,
        # dropped outside the try for the same reason (#6609).
        _evict_collection_engines_cache(username)

    if closed:
        logger.info(f"Connection cleanup: closed {closed} idle connection(s)")
    logger.debug(
        f"Connection cleanup: evaluated {len(candidates)} candidate(s), "
        f"closed {closed}, protected {len(protected)} active user(s)"
    )

    # Sweep dead-thread sessions and credentials — safety net when neither
    # HTTP requests nor the queue processor are triggering sweeps.
    cleanup_dead_threads()

    # --- Periodic pool dispose to release WAL/SHM handles ---
    # SQLCipher + WAL mode accumulates file handles when QueuePool recycles
    # connections out of open-order (ADR-0004). Periodically calling
    # dispose() on all engines closes ALL pooled connections, releasing any
    # leaked handles. The pool is transparently recreated on the next DB
    # operation.
    #
    # Safe to run against engines with checked-out connections: SA 2.0
    # `QueuePool.dispose` only drains idle queue entries and
    # `Engine.dispose` calls `pool.recreate()`; a thread holding a
    # checked-out connection keeps using it until return. SA docs are
    # explicit — "Connections that are still checked out will not be
    # closed". The post-login bulk write (_perform_post_login_tasks in
    # web/auth/routes.py) is additionally protected by being a single
    # atomic transaction, so any interruption (dispose, crash, OOM)
    # rolls back cleanly without leaving partial state.
    #
    # Do not add a `checkedout() > 0` skip guard here without first
    # reproducing a real torn-write against the actual SA source path:
    # see PR #3487 discussion — the speculative skip introduces an
    # unbounded "skip forever" risk on busy engines in exchange for
    # preventing a failure mode that SA 2.0 does not produce.
    global _last_dispose_time
    now = time.monotonic()
    if now - _last_dispose_time >= _DISPOSE_INTERVAL_SECONDS:
        _last_dispose_time = now
        # Delegate to the fenced pool-reset API: it takes each user's
        # rekey lifecycle lock and the on-disk sidecar lock before
        # checkpointing and disposing, so this periodic sweep can never
        # race a live password rekey (checkpointing a mid-rekey WAL, or
        # disposing the engine a rekey is about to evict anyway). Engines
        # whose sidecar is contended are skipped, not torn down. Individual
        # dispose failures are logged (type name only) inside the API.
        try:
            disposed = db_manager.reset_connection_pools()
        except Exception as exc:
            # The sweep must never take the scheduler down with it. Only
            # the exception TYPE NAME is logged — the value can carry
            # sensitive locals (DB paths, query fragments).
            logger.warning(
                f"Periodic pool dispose failed: {type(exc).__name__}"
            )
            disposed = 0
        if disposed:
            logger.info(
                f"Pool dispose: reset {disposed} engine(s) to release "
                f"WAL/SHM handles"
            )

    # --- FD monitoring ---
    try:
        fd_count = _count_open_fds()
        pool_engine_count = len(db_manager.connections)
        pool_checked_out = 0
        with db_manager._connections_lock:
            for engine in db_manager.connections.values():
                try:
                    pool_checked_out += engine.pool.checkedout()
                except Exception:  # noqa: silent-exception
                    pass
        logger.debug(
            f"Resource monitor: open_fds={fd_count}, "
            f"pool_engines={pool_engine_count}, "
            f"pool_checked_out={pool_checked_out}, "
            f"protected_users={len(protected)}"
        )
        if fd_count > 800:
            logger.warning(
                f"High FD count ({fd_count}) — approaching system limit. "
                f"Check for resource leaks."
            )
    except Exception:
        logger.debug("FD monitoring failed")  # noqa: silent-exception


def start_connection_cleanup_scheduler(
    session_manager, db_manager, interval_seconds=300
):
    """Start APScheduler job for periodic connection cleanup.

    Args:
        session_manager: The SessionManager singleton.
        db_manager: The DatabaseManager singleton.
        interval_seconds: How often to run cleanup (default: 5 minutes).

    Returns:
        The BackgroundScheduler instance (for shutdown registration).
    """
    scheduler = BackgroundScheduler()
    scheduler.add_job(
        cleanup_idle_connections,
        "interval",
        seconds=interval_seconds,
        args=[session_manager, db_manager],
        id="cleanup_idle_connections",
        jitter=30,
    )
    scheduler.start()
    logger.info(
        f"Connection cleanup scheduler started "
        f"(interval={interval_seconds}s, jitter=30s)"
    )
    return scheduler