Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def __init__(
headers: list[tuple[str, str]] | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
shutdown_delay: float = 0,
):
self.app = app
self.host = host
Expand Down Expand Up @@ -268,6 +269,7 @@ def __init__(
self.encoded_headers: list[tuple[bytes, bytes]] = []
self.factory = factory
self.h11_max_incomplete_event_size = h11_max_incomplete_event_size
self.shutdown_delay = shutdown_delay

self.loaded = False
self.configure_logging()
Expand Down
4 changes: 4 additions & 0 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ def main(
app_dir: str,
h11_max_incomplete_event_size: int | None,
factory: bool,
shutdown_delay: float = 0,
) -> None:
run(
app,
Expand Down Expand Up @@ -457,6 +458,7 @@ def main(
factory=factory,
app_dir=app_dir,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
shutdown_delay=shutdown_delay,
)


Expand Down Expand Up @@ -509,6 +511,7 @@ def run(
app_dir: str | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
shutdown_delay: float = 0,
) -> None:
if app_dir is not None:
sys.path.insert(0, app_dir)
Expand Down Expand Up @@ -560,6 +563,7 @@ def run(
use_colors=use_colors,
factory=factory,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
shutdown_delay=shutdown_delay,
)
server = Server(config=config)

Expand Down
10 changes: 9 additions & 1 deletion uvicorn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def __init__(self) -> None:
self.default_headers: list[tuple[bytes, bytes]] = []


class ShutdownTrigger:
is_shutdown_triggered: bool = False


class Server:
def __init__(self, config: Config) -> None:
self.config = config
Expand Down Expand Up @@ -259,8 +263,12 @@ async def on_tick(self, counter: int) -> bool:
return False

async def shutdown(self, sockets: list[socket.socket] | None = None) -> None:
logger.info("Shutting down")
if self.config.shutdown_delay:
logger.info(f"Shutting down in {self.config.shutdown_delay} seconds")
ShutdownTrigger.is_shutdown_triggered = True
await asyncio.sleep(self.config.shutdown_delay)

logger.info("Shutting down")
# Stop accepting new connections.
for server in self.servers:
server.close()
Expand Down
Loading