INNER CODE UNIT · Python

_exit_without_finalization

RekklesNA/ProxmoxMCP-Plus · src/proxmox_mcp/server.py:75

def _exit_without_finalization(status: int = 0) -> NoReturn:
    """Terminate the process without running interpreter finalization.

    Raising ``SystemExit`` from a signal handler is unsafe under the stdio
    transport. The MCP SDK reads stdin from an AnyIO worker thread, using its
    own ``TextIOWrapper`` built on ``sys.stdin.buffer``
    (``mcp.server.stdio.stdio_server``). A signal arriving mid-read leaves that
    thread blocked inside ``readline()``, owning the lock of the
    ``BufferedReader`` it shares with ``sys.stdin``.

    ``SystemExit`` would then start interpreter finalization, which clears the
    ``sys`` module dict, deallocates ``sys.stdin`` and tries to close that same
    ``BufferedReader``. The lock is never released, so CPython gives up after
    its one-second grace period and calls ``Py_FatalError``::

        Fatal Python error: _enter_buffered_busy: could not acquire lock for
        <_io.BufferedReader name='<stdin>'> at interpreter shutdown, possibly
        due to daemon threads

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…