"""POST /setup/warmup keeps its background model load alive until it finishes. The event loop holds tasks only weakly, so a fire-and-forget warmup could be garbage-collected partway through loading the model. """ from __future__ import annotations import asyncio import os import sys import types os.environ.setdefault("OMNIVOICE_MODEL", "test") os.environ.setdefault("OMNIVOICE_DISABLE_FILE_LOG", "1") def test_warmup_task_is_held_until_it_finishes(monkeypatch): from api.routers.setup import wizard async def scenario(): release = asyncio.Event() async def get_model(): await release.wait() monkeypatch.setitem( sys.modules, "services.model_manager", types.SimpleNamespace(get_model=get_model), ) assert await wizard.setup_warmup() == {"status": "warmup_started"} (task,) = wizard._warmup_tasks assert not task.done() release.set() await task await asyncio.sleep(0) assert not wizard._warmup_tasks asyncio.run(scenario())