From 976df967b414997d21dbd9658c0d467058364be7 Mon Sep 17 00:00:00 2001 From: whilenot Date: Thu, 5 Dec 2024 16:27:38 +0100 Subject: [PATCH] treat uvicorn_worker_id as optional in lifespan state --- uvicorn/lifespan/on.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/uvicorn/lifespan/on.py b/uvicorn/lifespan/on.py index 90c9ec35d..dba8f86f5 100644 --- a/uvicorn/lifespan/on.py +++ b/uvicorn/lifespan/on.py @@ -3,7 +3,7 @@ import asyncio import logging from asyncio import Queue -from typing import Any, Union +from typing import cast, Any, Optional, Union from uvicorn import Config from uvicorn._types import ( @@ -80,7 +80,9 @@ async def main(self) -> None: app = self.config.loaded_app # inject worker id into app state - app.app.state.uvicorn_worker_id = self.state["uvicorn_worker_id"] + uvicorn_worker_id = cast(Optional[int], self.state.get("uvicorn_worker_id")) + if uvicorn_worker_id is not None: + app.app.state.uvicorn_worker_id = uvicorn_worker_id scope: LifespanScope = { "type": "lifespan",