diff --git a/backend/app.py b/backend/app.py index 63b567e..ee45d08 100644 --- a/backend/app.py +++ b/backend/app.py @@ -105,6 +105,7 @@ class App: has_error = await asyncio.to_thread(processing.process_all_jobs, list(self.environments.values()), emit) if self.shutdown_event.is_set(): return + # Skip the wait when a newer update arrived while the current processing pass was running. if self.environment_update_version != update_version: continue timeout = 5 if has_error else 60 @@ -138,6 +139,7 @@ class App: asyncio.run(self.runHeadlessAsync()) async def _run_web_server(self, web_app: Any) -> None: + """Raise a sentinel exception when the server stops so TaskGroup cancels processing.""" await web_app.start_async() raise _WebServerStopped() diff --git a/backend/web.py b/backend/web.py index 6f29501..74c961c 100644 --- a/backend/web.py +++ b/backend/web.py @@ -96,6 +96,7 @@ class WebApp: def _schedule_async(self, coro: Any) -> None: if self._event_loop is None or not self._event_loop.is_running(): + logger.warning("Dropping scheduled coroutine because the web event loop is not running") coro.close() return try: diff --git a/tests/test_app_asyncio.py b/tests/test_app_asyncio.py index 92ff143..1fe7e26 100644 --- a/tests/test_app_asyncio.py +++ b/tests/test_app_asyncio.py @@ -1,9 +1,11 @@ import asyncio import sys import types +from typing import Callable import pytest +from enironment import Environment import processing from app import App @@ -17,10 +19,10 @@ class TestAppAsyncio: loop = asyncio.get_running_loop() web_app_args: dict[str, object] = {} - def fake_process_all_jobs(environments: object, onupdate: object) -> bool: + def fake_process_all_jobs(environments: list[Environment], onupdate: Callable[[], None]) -> bool: nonlocal process_calls process_calls += 1 - assert callable(onupdate) + assert environments == [] onupdate() loop.call_soon_threadsafe(processing_started.set) return False diff --git a/tests/test_containertest.py b/tests/test_containertest.py index decdb57..5fe5279 100644 --- a/tests/test_containertest.py +++ b/tests/test_containertest.py @@ -59,6 +59,7 @@ class TestDockerContainer: processing.cancel() with suppress(asyncio.CancelledError): await processing + assert processing.cancelled() # TODO Verify DockerContainerDeploy step status is running and has correct image and ports # {