diff --git a/packages/server/tests/test_diagnostic_mode.py b/packages/server/tests/test_diagnostic_mode.py index 39f9f7d..5ec26dd 100644 --- a/packages/server/tests/test_diagnostic_mode.py +++ b/packages/server/tests/test_diagnostic_mode.py @@ -23,11 +23,23 @@ from fastapi.testclient import TestClient def _reset_state() -> None: - """Clear the module-level ``_active`` dict between tests so prior - activations don't bleed across cases.""" + """Clear the module-level ``_active`` dict and any pending fallback + tasks between tests so prior activations don't bleed across cases. + + The ``_bg_tasks`` set retains tasks created via the asyncio-fallback + schedule path; without this, a test that schedules a 30-minute revert + leaves a pending task that inflates the ``len(_bg_tasks)`` invariant + checked by ``test_fallback_task_retained_until_fire``. + """ from notify_bridge_server.services import diagnostic_mode as svc svc._active.clear() + # ``_bg_tasks`` from a previous test belong to that test's now-closed + # event loop — calling ``.cancel()`` here crosses loops and raises + # ``RuntimeError: Event loop is closed``. Dropping the references is + # enough: the tasks can't fire on a dead loop, and CPython will GC + # them once the prior loop releases them. + svc._bg_tasks.clear() @pytest.fixture(autouse=True)