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.

3.4 KB
Raw Blame
"""Process dump policy applied explicitly when an LDR server starts.

Importing this module does not change process limits or dumpability.
"""

from os import name as _os_name
from sys import platform as _platform
import warnings

try:
    import resource as _resource
except ImportError:  # Windows
    _resource = None


_PR_GET_DUMPABLE = 3
_PR_SET_DUMPABLE = 4


class ProcessDumpPolicyError(RuntimeError):
    """A supported server process could not apply its dump policy."""


def enforce_process_dump_policy() -> bool:
    """Disable process dumps before starting server work.

    POSIX servers require both core-file size limits to be zero. Linux
    additionally requires a non-dumpable process because its piped core
    handlers do not honor ``RLIMIT_CORE``. Each setting is read back;
    failure raises ``ProcessDumpPolicyError`` and must stop startup.

    Returns False, with a warning, on non-POSIX platforms where these
    controls are unavailable. They require operating-system configuration.
    This policy is not restored at shutdown. A child started with exec
    inherits ``RLIMIT_CORE`` of zero, but Linux resets its dumpability, so
    the child is dumpable again unless it disables that itself. The
    document-parser worker (``security/isolated_worker.py``) does not: it
    sets ``RLIMIT_CORE`` to zero itself and holds document text, not the
    server's credentials.
    """
    if _os_name != "posix":
        warnings.warn(
            "Process dump controls are unavailable on this platform; "
            "configure crash-dump policy in the operating system.",
            RuntimeWarning,
            stacklevel=2,
        )
        return False

    if _resource is None:
        raise ProcessDumpPolicyError(
            "Cannot start server: POSIX core-file controls are unavailable."
        )
    try:
        _resource.setrlimit(_resource.RLIMIT_CORE, (0, 0))
        limits = _resource.getrlimit(_resource.RLIMIT_CORE)
    except (AttributeError, OSError, ValueError):
        raise ProcessDumpPolicyError(
            "Cannot start server: core-file limits could not be disabled."
        ) from None
    if limits != (0, 0):
        raise ProcessDumpPolicyError(
            "Cannot start server: core-file limits remain enabled."
        )

    if _platform.startswith("linux"):
        try:
            # Imported here, not at module level: ctypes is only needed on
            # Linux, and an interpreter built without _ctypes must still
            # fail with ProcessDumpPolicyError rather than ImportError.
            import ctypes

            prctl = ctypes.CDLL(None, use_errno=True).prctl
            # prctl is variadic: the trailing arguments must have the width
            # of unsigned long, including the unused zero arguments.
            prctl.argtypes = [ctypes.c_int] + [ctypes.c_ulong] * 4
            prctl.restype = ctypes.c_int
            configured = prctl(_PR_SET_DUMPABLE, 0, 0, 0, 0)
            dumpable = prctl(_PR_GET_DUMPABLE, 0, 0, 0, 0)
        except (
            AttributeError,
            ImportError,
            OSError,
            TypeError,
            ValueError,
        ):
            raise ProcessDumpPolicyError(
                "Cannot start server: Linux dumpability controls are unavailable."
            ) from None
        if configured != 0 or dumpable != 0:
            raise ProcessDumpPolicyError(
                "Cannot start server: Linux dumpability could not be disabled."
            )

    return True