"""Entry point: uvicorn with a drain phase before the graceful shutdown. Plain uvicorn stops accepting connections immediately on SIGTERM. Behind HAProxy that turns every stop into a burst of refused connections until the active health check notices. Instead, on the first signal: t = 0 readiness -> 503, keep serving (Connection: close); t = drain (3 s) HAProxy has marked the server DOWN (inter 1 s x fall 2), uvicorn closes the listener and waits for in-flight requests up to SHUTDOWN_TIMEOUT_SECONDS (8 s). stop_grace_period in compose.yaml (15 s) is larger than 3 + 8 s. A second signal skips the drain phase. """ import logging import threading import uvicorn from app.config import settings from app.lifecycle import draining logger = logging.getLogger("messenger.server") class DrainingServer(uvicorn.Server): def handle_exit(self, sig, frame) -> None: if draining.is_set(): super().handle_exit(sig, frame) return draining.set() logger.warning( "signal %s: draining for %.1f s before shutdown", sig, settings.shutdown_drain_seconds, ) # Set should_exit directly instead of super().handle_exit(): uvicorn # would re-raise the captured SIGTERM after shutdown and the process # would exit with 143 instead of a clean 0. timer = threading.Timer( settings.shutdown_drain_seconds, self._begin_shutdown, ) timer.daemon = True timer.start() def _begin_shutdown(self) -> None: logger.warning("drain finished: closing listener") self.should_exit = True def main() -> None: config = uvicorn.Config( "app.main:app", host=settings.app_host, port=settings.app_port, proxy_headers=False, # Longer than HAProxy "timeout http-keep-alive 10s": the LB closes an # idle connection first, so it never reuses one the API just closed. timeout_keep_alive=30, timeout_graceful_shutdown=settings.shutdown_timeout_seconds, log_level="info", ) DrainingServer(config).run() if __name__ == "__main__": main()