Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented lifespan.shutdown.failed #755

Merged
merged 3 commits into from
Feb 25, 2021
Merged
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
30 changes: 30 additions & 0 deletions tests/test_lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,33 @@ async def test():

loop = asyncio.new_event_loop()
loop.run_until_complete(test())


@pytest.mark.parametrize("mode", ("auto", "on"))
@pytest.mark.parametrize("raise_exception", (True, False))
def test_lifespan_with_failed_shutdown(mode, raise_exception):
async def app(scope, receive, send):
message = await receive()
assert message["type"] == "lifespan.startup"
await send({"type": "lifespan.startup.complete"})
message = await receive()
assert message["type"] == "lifespan.shutdown"
await send({"type": "lifespan.shutdown.failed"})

if raise_exception:
# App should be able to re-raise an exception if startup failed.
raise RuntimeError()

async def test():
config = Config(app=app, lifespan=mode)
lifespan = LifespanOn(config)

await lifespan.startup()
assert not lifespan.startup_failed
await lifespan.shutdown()
assert lifespan.shutdown_failed
assert lifespan.error_occured is raise_exception
assert lifespan.should_exit

loop = asyncio.new_event_loop()
loop.run_until_complete(test())
21 changes: 19 additions & 2 deletions uvicorn/lifespan/on.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, config: Config) -> None:
self.receive_queue: "Queue[LifespanReceiveMessage]" = asyncio.Queue()
self.error_occured = False
self.startup_failed = False
self.shutdown_failed = False
self.should_exit = False

async def startup(self) -> None:
Expand All @@ -43,7 +44,14 @@ async def shutdown(self) -> None:
self.logger.info("Waiting for application shutdown.")
await self.receive_queue.put({"type": "lifespan.shutdown"})
await self.shutdown_event.wait()
self.logger.info("Application shutdown complete.")

if self.shutdown_failed or (
self.error_occured and self.config.lifespan == "on"
):
self.logger.error("Application shutdown failed. Exiting.")
self.should_exit = True
else:
self.logger.info("Application shutdown complete.")

async def main(self) -> None:
try:
Expand All @@ -56,7 +64,7 @@ async def main(self) -> None:
except BaseException as exc:
self.asgi = None
self.error_occured = True
if self.startup_failed:
if self.startup_failed or self.shutdown_failed:
return
if self.config.lifespan == "auto":
msg = "ASGI 'lifespan' protocol appears unsupported."
Expand All @@ -73,6 +81,7 @@ async def send(self, message: LifespanSendMessage) -> None:
"lifespan.startup.complete",
"lifespan.startup.failed",
"lifespan.shutdown.complete",
"lifespan.shutdown.failed",
)

if message["type"] == "lifespan.startup.complete":
Expand All @@ -93,5 +102,13 @@ async def send(self, message: LifespanSendMessage) -> None:
assert not self.shutdown_event.is_set(), STATE_TRANSITION_ERROR
self.shutdown_event.set()

elif message["type"] == "lifespan.shutdown.failed":
assert self.startup_event.is_set(), STATE_TRANSITION_ERROR
assert not self.shutdown_event.is_set(), STATE_TRANSITION_ERROR
self.shutdown_event.set()
self.shutdown_failed = True
if message.get("message"):
self.logger.error(message["message"])

async def receive(self) -> LifespanReceiveMessage:
return await self.receive_queue.get()