Skip to content

Commit 4cf3528

Browse files
committed
Bugfix don't handle Cancellation errors in lifespan
This fixes a regression caused by bfb0877 whereby the cancellation error was caught. It should instead be re-raised.
1 parent c405dea commit 4cf3528

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/hypercorn/asyncio/lifespan.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ def _call_soon(func: Callable, *args: Any) -> Any:
5959
partial(self.loop.run_in_executor, None),
6060
_call_soon,
6161
)
62-
except LifespanFailureError:
63-
# Lifespan failures should crash the server
62+
except (LifespanFailureError, asyncio.CancelledError):
6463
raise
6564
except (BaseExceptionGroup, Exception) as error:
6665
if isinstance(error, BaseExceptionGroup):
67-
failure_error = error.subgroup(LifespanFailureError)
68-
if failure_error is not None:
69-
# Lifespan failures should crash the server
70-
raise failure_error
66+
reraise_error = error.subgroup((LifespanFailureError, asyncio.CancelledError))
67+
if reraise_error is not None:
68+
raise reraise_error
7169

7270
self.supported = False
7371
if not self.startup.is_set():

src/hypercorn/trio/lifespan.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@ async def handle_lifespan(
4545
trio.to_thread.run_sync,
4646
trio.from_thread.run,
4747
)
48-
except LifespanFailureError:
49-
# Lifespan failures should crash the server
48+
except (LifespanFailureError, trio.Cancelled):
5049
raise
5150
except (BaseExceptionGroup, Exception) as error:
5251
if isinstance(error, BaseExceptionGroup):
53-
failure_error = error.subgroup(LifespanFailureError)
54-
if failure_error is not None:
55-
# Lifespan failures should crash the server
56-
raise failure_error
52+
reraise_error = error.subgroup((LifespanFailureError, trio.Cancelled))
53+
if reraise_error is not None:
54+
raise reraise_error
5755

5856
self.supported = False
5957
if not self.startup.is_set():

0 commit comments

Comments
 (0)