fix(tests): Enhance event loop cleanup in app_instance fixture

This commit is contained in:
2026-03-02 23:05:58 -05:00
parent 5e6c685b06
commit 5a0ec6646e

View File

@@ -1,4 +1,5 @@
import pytest import pytest
import asyncio
import subprocess import subprocess
import time import time
import requests import requests
@@ -49,9 +50,23 @@ def app_instance() -> Generator[App, None, None]:
): ):
app = App() app = App()
yield app yield app
# Cleanup: Ensure asyncio loop is stopped # Cleanup: Ensure asyncio loop is stopped and tasks are cancelled
if hasattr(app, '_loop') and app._loop.is_running(): if hasattr(app, '_loop'):
# 1. Identify all pending tasks in app._loop.
tasks = [t for t in asyncio.all_tasks(app._loop) if not t.done()]
# 2. Cancel them using task.cancel().
for task in tasks:
task.cancel()
# Stop background thread to take control of the loop thread-safely
if app._loop.is_running():
app._loop.call_soon_threadsafe(app._loop.stop) app._loop.call_soon_threadsafe(app._loop.stop)
if hasattr(app, '_loop_thread') and app._loop_thread.is_alive():
app._loop_thread.join(timeout=2.0)
# 3. Wait for them to complete using loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True)).
if tasks:
app._loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
# 4. Then stop the loop.
app._loop.stop()
@pytest.fixture @pytest.fixture
def mock_app(app_instance: App) -> App: def mock_app(app_instance: App) -> App: