refactor: defensive getattr guard and clarified cleanup comment in lifespan
- Use getattr(app.state, 'federation_client', None) in shutdown to guard against the theoretical case where AsyncClient constructor raised before setting app.state.federation_client - Rename 'Shutdown:' comment to 'Cleanup:' in the finally block to better describe that the block covers both the poll-task cancellation and the overall cleanup guarantee
This commit is contained in:
+4
-2
@@ -183,11 +183,13 @@ async def lifespan(app: FastAPI):
|
|||||||
yield
|
yield
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await app.state.federation_client.aclose()
|
client = getattr(app.state, "federation_client", None)
|
||||||
|
if client is not None:
|
||||||
|
await client.aclose()
|
||||||
except Exception:
|
except Exception:
|
||||||
_log.exception("federation_client aclose error")
|
_log.exception("federation_client aclose error")
|
||||||
finally:
|
finally:
|
||||||
# Shutdown: cancel the poll loop task and wait for it to finish.
|
# Cleanup: cancel the poll loop task and wait for it to finish.
|
||||||
if _poll_task is not None:
|
if _poll_task is not None:
|
||||||
_poll_task.cancel()
|
_poll_task.cancel()
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user