From f0863b1304256a079002ee9aa4d75adee782e69f Mon Sep 17 00:00:00 2001 From: ant1fr Date: Thu, 19 Nov 2026 14:32:02 +0100 Subject: [PATCH] fix: stop the worker loops from hanging on shutdown under Python 3.11 The delivery and ephemeral worker loops waited with asyncio.wait_for, which on Python 3.11 can swallow a cancellation (the inner wait and the cancel race). stop() then awaited a loop that never ended, hanging the TestClient teardown (seen in CI as an intermittent Python 3.11 pytest hang, captured by the faulthandler dump). asyncio.timeout does not have that race. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01VzFCH4ztqBYejkvLvFgnaq --- app/services/delivery.py | 5 ++++- app/services/ephemeral.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/services/delivery.py b/app/services/delivery.py index d394ec1..adeb8ec 100644 --- a/app/services/delivery.py +++ b/app/services/delivery.py @@ -509,7 +509,10 @@ async def _run(self) -> None: if timeout is None: await self._wake.wait() else: - await asyncio.wait_for(self._wake.wait(), timeout) + # asyncio.timeout, not wait_for: on Python 3.11 wait_for can swallow a + # cancellation, and stop() would then wait for this loop forever. + async with asyncio.timeout(timeout): + await self._wake.wait() except TimeoutError: timed_out = True self._wake.clear() diff --git a/app/services/ephemeral.py b/app/services/ephemeral.py index 842e3cd..cfa6632 100644 --- a/app/services/ephemeral.py +++ b/app/services/ephemeral.py @@ -249,7 +249,10 @@ def _timeout(self) -> float: async def _run(self) -> None: while True: with contextlib.suppress(TimeoutError): - await asyncio.wait_for(self._wake.wait(), self._timeout()) + # asyncio.timeout, not wait_for: on Python 3.11 wait_for can swallow a + # cancellation, and stop() would then wait for this loop forever. + async with asyncio.timeout(self._timeout()): + await self._wake.wait() self._wake.clear() try: await self.expire_if_due()