Skip to content

Exit handler patch no longer works with uvicorn #132

@vxgmichel

Description

@vxgmichel

This Server.handle_exit patch does not work with the latest uvicorn implementation (0.34.2):

try:
from uvicorn.main import Server
AppStatus.original_handler = Server.handle_exit
Server.handle_exit = AppStatus.handle_exit # type: ignore
except ImportError:
logger.debug(
"Uvicorn not installed. Graceful shutdown on server termination disabled."
)

This is because the signal handlers are registered by uvicorn before the application module is imported:

    async def serve(self, sockets: list[socket.socket] | None = None) -> None:
        with self.capture_signals():  # <- Handlers set here
            await self._serve(sockets)

    async def _serve(self, sockets: list[socket.socket] | None = None) -> None:
        process_id = os.getpid()

        config = self.config
        if not config.loaded:
            config.load()  # <- Application module imported here

Source here

I suspect that the app module used to be imported earlier and that it is no longer the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions