From 9aada7538198de7975654e32760e522f97e77638 Mon Sep 17 00:00:00 2001 From: "alexei.dolgolyov" Date: Thu, 28 May 2026 15:26:11 +0300 Subject: [PATCH] fix(tests): clear diagnostic_mode _bg_tasks between cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test_fallback_task_retained_until_fire asserts len(_bg_tasks) == 1, but the set carries pending tasks from earlier tests' fallback schedules, so the assertion saw the accumulated count instead. Drop the references (no .cancel() — the tasks belong to closed loops, cross-loop cancel raises RuntimeError on the next test's setup). --- packages/server/tests/test_diagnostic_mode.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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)