cleanup.py 655 Bytes
"""Cleanup registration helpers for the web API bootstrap layer."""

from __future__ import annotations

import atexit
from typing import Callable

CleanupHandler = Callable[[], None]


def register_reload_safe_cleanup_handler(
    handler: CleanupHandler,
    previous_handler: CleanupHandler | None = None,
) -> CleanupHandler:
    """Register a cleanup handler while safely unregistering the previous one."""
    if previous_handler is not None:
        try:
            atexit.unregister(previous_handler)
        except Exception:
            pass

    atexit.register(handler)
    return handler


__all__ = ["register_reload_safe_cleanup_handler"]