Open-source inference server and production cluster for all the models your agent needs.
Python bgecolbertdata-pipelinedeep-learningembeddingsinferenceinference-serverinformation-retrievalllmmlmlopsnatural-language-processingnlppythonrerankingretrievalretrieval-augmented-generationsemantic-searchspladevector-search
fix(server): exit on SIGTERM after starting the drain (#637)
svonavaecb957db6f45
diff --git a/packages/sie_server/src/sie_server/core/shutdown.py b/packages/sie_server/src/sie_server/core/shutdown.pyindex 76406fd0e..1a2d70a11 100644--- a/packages/sie_server/src/sie_server/core/shutdown.py+++ b/packages/sie_server/src/sie_server/core/shutdown.py@@ -11,7 +11,10 @@ import loggingimport signalimport sysimport time+from collections.abc import Callablefrom dataclasses import dataclass, field+from types import FrameType+from typing import Anyfrom starlette.responses import JSONResponsefrom starlette.types import ASGIApp, Receive, Scope, Send@@ -101,6 +104,13 @@ class ShutdownState:def setup_signal_handlers(shutdown_state: ShutdownState) -> None:"""Install SIGTERM handler for graceful shutdown.+ The handler marks the state as shutting down (new requests get 503) and+ then calls the handler it replaced. Under uvicorn that is+ ``Server.handle_exit``, which stops the server: it closes the listening+ socket, lets in-flight requests finish and runs the lifespan shutdown,+ which waits for the drain. Without that call nothing ever told uvicorn to+ exit, so a server that got SIGTERM kept running until it was SIGKILLed.+Args:shutdown_state: The shutdown state to update on signal.@@ -108,10 +118,17 @@ def setup_signal_handlers(shutdown_state: ShutdownState) -> None:Signal handlers can only be set in the main thread. In test environmentsor when running under certain frameworks, this may silently skip setup."""+ previous = signal.getsignal(signal.SIGTERM) if sys.platform != "win32" else None+ # SIG_DFL and SIG_IGN are ints; anything else that is set is a Python handler.+ chained: Callable[[int, FrameType | None], Any] | None = (+ None if previous is None or isinstance(previous, int) else previous+ )- def handle_sigterm(_signum: int, _frame: object) -> None:+ def handle_sigterm(signum: int, frame: FrameType | None) -> None:logger.info("Received SIGTERM, initiating graceful shutdown")shutdown_state.start_shutdown()+ if chained is not None:+ chained(signum, frame)# Only set up signal handlers on Unix (not Windows)if sys.platform != "win32":diff --git a/packages/sie_server/tests/core/test_shutdown.py b/packages/sie_server/tests/core/test_shutdown.pyindex 833c7020b..9ad0267bd 100644--- a/packages/sie_server/tests/core/test_shutdown.py+++ b/packages/sie_server/tests/core/test_shutdown.py@@ -3,9 +3,13 @@from __future__ import annotationsimport asyncio+import os+import signal+import sys+from collections.abc import Iteratorimport pytest-from sie_server.core.shutdown import ShutdownState+from sie_server.core.shutdown import ShutdownState, setup_signal_handlersclass TestShutdownState:@@ -194,3 +198,34 @@ class TestSignalHandler:shutdown_state = ShutdownState()# Should not raise even in test threadsetup_signal_handlers(shutdown_state)++[email protected](sys.platform == "win32", reason="SIGTERM handlers are Unix-only")+class TestSigtermChaining:+ """SIGTERM marks the server as draining and still reaches the handler it replaced (uvicorn's exit)."""++ @pytest.fixture(autouse=True)+ def _restore_sigterm(self) -> Iterator[None]:+ original = signal.getsignal(signal.SIGTERM)+ yield+ signal.signal(signal.SIGTERM, original)++ def test_sigterm_starts_shutdown_and_calls_previous_handler(self) -> None:+ calls: list[int] = []+ signal.signal(signal.SIGTERM, lambda signum, _frame: calls.append(signum))+ state = ShutdownState()+ setup_signal_handlers(state)++ os.kill(os.getpid(), signal.SIGTERM)++ assert state.shutting_down+ assert calls == [signal.SIGTERM]++ def test_sigterm_without_a_callable_previous_handler_only_starts_shutdown(self) -> None:+ signal.signal(signal.SIGTERM, signal.SIG_IGN)+ state = ShutdownState()+ setup_signal_handlers(state)++ os.kill(os.getpid(), signal.SIGTERM)++ assert state.shutting_down