Skip to content

Send lifespan.shutdown.failed event on lifespan shutdown error#1205

Closed
JayH5 wants to merge 4 commits intomasterfrom
jh/on-shutdown-failed
Closed

Send lifespan.shutdown.failed event on lifespan shutdown error#1205
JayH5 wants to merge 4 commits intomasterfrom
jh/on-shutdown-failed

Conversation

@JayH5
Copy link
Contributor

@JayH5 JayH5 commented Jun 19, 2021

Fixes #1138

We supported lifespan.startup.failed but not lifespan.shutdown.failed for some reason. These were added in the Lifespan 2.0 ASGI spec.

Previously, we used single-iteration for-loops to communicate with the lifespan generator. This changes to an explicit try/except StopAsyncIteration, closer to what pytest/AnyIO do when calling test fixtures.

Added additional tests for the incorrect number of yields. Previously we just had an assert on a boolean flag, but now we explicitly raise a RuntimeError (again, similar to pytest which raises this error type in a similar situation).

async def _lifespan_iter(
self, gen: typing.AsyncGenerator, receive: Receive, send: Send
) -> None:
try:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just making lifespan context an asynccontextmanager factory makes all this easier

@JayH5
Copy link
Contributor Author

JayH5 commented Jul 3, 2021

Superseded by #1227

@JayH5 JayH5 closed this Jul 3, 2021
@JayH5 JayH5 deleted the jh/on-shutdown-failed branch July 3, 2021 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When on_shutdown function raises error, starlette doesn't catch it and uvicorn thinks that lifespan is not supported

2 participants