From cde84654bf71bb4f458d19ca3ca4b3e1bbe5d8d6 Mon Sep 17 00:00:00 2001 From: adavyas Date: Thu, 30 Jul 2026 13:01:22 -0400 Subject: [PATCH] test: terminate leaked backend if the real-DB cancellation test fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On assertion failure the backend under test is parked 'idle in transaction' — exactly the bug this PR fixes — and would poison the shared test pool for the rest of the session. Terminate it in a finally regardless of pass/fail. (CodeRabbit review, tests/test_dependencies.py) Co-Authored-By: Claude Fable 5 --- tests/test_dependencies.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/test_dependencies.py b/tests/test_dependencies.py index 442931a3..a4d59adb 100644 --- a/tests/test_dependencies.py +++ b/tests/test_dependencies.py @@ -612,10 +612,17 @@ async def test_tracked_db_no_idle_in_transaction_after_real_cancellation() -> No with pytest.raises(asyncio.CancelledError): await task - state = await backend_state(pid) - for _ in range(20): # allow shielded cleanup a moment to land - if state != "idle in transaction": - break - await asyncio.sleep(0.1) + try: state = await backend_state(pid) - assert state != "idle in transaction", f"backend left {state!r} after cancel" + for _ in range(20): # allow shielded cleanup a moment to land + if state != "idle in transaction": + break + await asyncio.sleep(0.1) + state = await backend_state(pid) + assert state != "idle in transaction", f"backend left {state!r} after cancel" + finally: + # On failure the backend is parked exactly like the bug under test; + # terminate it so it can't poison the shared pool for later tests. + if await backend_state(pid) == "idle in transaction": + async with read_engine.connect() as obs: + await obs.execute(text("SELECT pg_terminate_backend(:p)"), {"p": pid})