Skip to content

Commit e69634e

Browse files
committed
Fix test_shutdown_worker leaving stale callback that breaks later tests
Root cause: test_shutdown_worker was an async test (@pytest.mark.asyncio), so pytest-asyncio ran it via loop.run_until_complete(). Inside the test, shutdown_worker() calls runner.close() -> loop.stop(). Calling loop.stop() from within loop.run_until_complete() leaves a stale _run_until_complete_cb in the event loop's ready queue. This callback calls loop.stop() when it fires, which poisons the next run_until_complete() call on the same loop with: RuntimeError: Event loop stopped before Future completed Why the next test is affected: The event_loop fixture in conftest.py returns manager.get_runner().loop. After test_shutdown_worker, this loop still has the stale callback. When a later test (e.g. test_calc_job_node_get_builder_restart) calls run_until_complete() on the same loop, the stale callback fires loop.stop() prematurely. Why the _reset_runner autouse fixture masked the issue: The _reset_runner fixture called manager.reset_runner() after every test, which called runner.close() -> loop.close(). This closed the loop entirely, so get_or_create_event_loop() created a fresh loop for the next test without the stale callback. Removing this fixture exposed the underlying bug. Why run_forever fixes it: In the real daemon, start_daemon_worker() uses loop.run_forever(), not loop.run_until_complete(). run_forever() does not add a _run_until_complete_cb, so loop.stop() cleanly exits the loop with no stale callbacks. The fix changes the test to use the same pattern: schedule shutdown_worker as a task, then call run_forever(). This mirrors production behavior and avoids the stale callback.
1 parent 57378d6 commit e69634e

2 files changed

Lines changed: 4 additions & 15 deletions

File tree

tests/conftest.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ class TestDbBackend(Enum):
5555
PSQL = 'psql'
5656

5757

58-
@pytest.fixture(autouse=True)
59-
def _reset_runner(request):
60-
yield
61-
get_manager().reset_runner()
62-
63-
6458
def pytest_collection_modifyitems(items, config):
6559
"""Automatically generate markers for certain tests.
6660

tests/engine/daemon/test_worker.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@
1616

1717

1818
@pytest.mark.requires_rmq
19-
@pytest.mark.asyncio
20-
async def test_shutdown_worker(manager):
19+
def test_shutdown_worker(manager):
2120
"""Test the ``shutdown_worker`` method."""
2221
runner = manager.get_runner()
23-
await shutdown_worker(runner)
24-
25-
try:
26-
assert runner.is_closed()
27-
finally:
28-
# Reset the runner of the manager, because once closed it cannot be reused by other tests.
29-
manager._runner = None
22+
runner.loop.create_task(shutdown_worker(runner))
23+
runner.loop.run_forever()
24+
assert runner.is_closed()
3025

3126

3227
@pytest.mark.usefixtures('aiida_profile_clean', 'started_daemon_client')

0 commit comments

Comments
 (0)