From 4cf352890e6869bfbc0940e75420ea897fa92518 Mon Sep 17 00:00:00 2001 From: pgjones Date: Mon, 3 Jun 2024 20:51:44 +0100 Subject: [PATCH] Bugfix don't handle Cancellation errors in lifespan This fixes a regression caused by bfb087756da53c25fd9390b4ea0acf914408ecbf whereby the cancellation error was caught. It should instead be re-raised. --- src/hypercorn/asyncio/lifespan.py | 10 ++++------ src/hypercorn/trio/lifespan.py | 10 ++++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/hypercorn/asyncio/lifespan.py b/src/hypercorn/asyncio/lifespan.py index bd22c8ff..39803457 100644 --- a/src/hypercorn/asyncio/lifespan.py +++ b/src/hypercorn/asyncio/lifespan.py @@ -59,15 +59,13 @@ def _call_soon(func: Callable, *args: Any) -> Any: partial(self.loop.run_in_executor, None), _call_soon, ) - except LifespanFailureError: - # Lifespan failures should crash the server + except (LifespanFailureError, asyncio.CancelledError): raise except (BaseExceptionGroup, Exception) as error: if isinstance(error, BaseExceptionGroup): - failure_error = error.subgroup(LifespanFailureError) - if failure_error is not None: - # Lifespan failures should crash the server - raise failure_error + reraise_error = error.subgroup((LifespanFailureError, asyncio.CancelledError)) + if reraise_error is not None: + raise reraise_error self.supported = False if not self.startup.is_set(): diff --git a/src/hypercorn/trio/lifespan.py b/src/hypercorn/trio/lifespan.py index cd809845..4aeba249 100644 --- a/src/hypercorn/trio/lifespan.py +++ b/src/hypercorn/trio/lifespan.py @@ -45,15 +45,13 @@ async def handle_lifespan( trio.to_thread.run_sync, trio.from_thread.run, ) - except LifespanFailureError: - # Lifespan failures should crash the server + except (LifespanFailureError, trio.Cancelled): raise except (BaseExceptionGroup, Exception) as error: if isinstance(error, BaseExceptionGroup): - failure_error = error.subgroup(LifespanFailureError) - if failure_error is not None: - # Lifespan failures should crash the server - raise failure_error + reraise_error = error.subgroup((LifespanFailureError, trio.Cancelled)) + if reraise_error is not None: + raise reraise_error self.supported = False if not self.startup.is_set():