From 49b89de3dc0887bd25da32be0f1c4eb41121e0c2 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 27 Jul 2026 10:16:38 +0000 Subject: [PATCH] Restore the diffusion engine selection after each router test The active engine is module state, and several tests set it by plain assignment because what _activate does to it is the thing under test, so monkeypatch could not undo it. A leaked ENGINE_SD_CPP left get_active_diffusion_engine() handing back the sd.cpp backend for the rest of the process, and every later route that reads the active engine then saw an unloaded model: eight tests in test_openai_images_generations_route.py returned 503 in a full-suite run while passing on their own. The autouse fixture now snapshots and restores it. --- .../backend/tests/test_diffusion_engine_router.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/studio/backend/tests/test_diffusion_engine_router.py b/studio/backend/tests/test_diffusion_engine_router.py index e8e4270e1b..c0f9ececbf 100644 --- a/studio/backend/tests/test_diffusion_engine_router.py +++ b/studio/backend/tests/test_diffusion_engine_router.py @@ -40,7 +40,19 @@ def _clean_env_and_state(monkeypatch): # runnability probe, so neither reaches the real install/exec path. monkeypatch.setattr(r, "ensure_sd_server_binary", lambda **_: None) monkeypatch.setattr(r, "_server_binary_runnable", lambda *_a, **_k: True) - yield + # The selection is module state, and several tests below set it by plain assignment (the point + # of those tests is what _activate does to it), so monkeypatch cannot undo it. Restore it here: + # a leaked ENGINE_SD_CPP left get_active_diffusion_engine() returning the sd.cpp backend for the + # rest of the process, and every later test whose route reads the active engine saw an unloaded + # model -- eight tests in test_openai_images_generations_route.py 503'd in a full-suite run + # while passing on their own. + saved_engine = r._active_engine_name + saved_reason = r._fallback_reason + try: + yield + finally: + r._active_engine_name = saved_engine + r._fallback_reason = saved_reason def _set_device(monkeypatch, backend):