Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Wait for the claim release, not the teardown, in the idle-unload test (#11257)

Daniel Han11e7998a0dab
diff --git a/studio/backend/tests/multi_account/test_chat_unload_ownership.py b/studio/backend/tests/multi_account/test_chat_unload_ownership.py
index 276e5d12299..2635a75b66a 100644
--- a/studio/backend/tests/multi_account/test_chat_unload_ownership.py
+++ b/studio/backend/tests/multi_account/test_chat_unload_ownership.py
@@ -5,6 +5,7 @@
import asyncio
import sys
+import threading
from pathlib import Path
from types import SimpleNamespace
@@ -93,8 +94,11 @@ def test_manual_unload_releases_chat_ownership(chat_resident, accounts):
response = bob_status(accounts)
assert response.status_code == 200, response.text
body = response.json()
- # The hidden-resident answer is exactly {"loaded": True, "yours": False}; the real
- # status has no "yours" key and reports "loaded" as the list of resident models.
+ # "yours" is the load-bearing assertion, not "loaded". This route's mask is
+ # hidden_chat_status_response(), {"loaded": [], "loading": [], "yours": False}, so an
+ # empty "loaded" is what BOTH answers report here and only the "yours" key tells a real
+ # empty GPU from a hidden resident. test_only_the_yours_key_separates_the_two_answers
+ # pins that, so neither assertion can be dropped as redundant.
assert "yours" not in body, f"phantom resident after unload: {body}"
assert body.get("loaded") == [], f"phantom resident after unload: {body}"
assert body.get("active_model") is None, body
@@ -102,7 +106,8 @@ def test_manual_unload_releases_chat_ownership(chat_resident, accounts):
assert gpu_arbiter.owner_account() is None
-def test_idle_auto_unload_releases_chat_ownership(chat_resident, accounts, monkeypatch):
+def arm_the_idle_loop(monkeypatch):
+ """Everything the idle loop reads, set so one tick unloads."""
monkeypatch.setattr(llama_keepwarm, "_is_idle", lambda ttl: True)
monkeypatch.setattr(llama_keepwarm, "_note_activity", lambda: None)
monkeypatch.setattr(
@@ -111,11 +116,95 @@ def test_idle_auto_unload_releases_chat_ownership(chat_resident, accounts, monke
monkeypatch.setattr("utils.openai_auto_switch_settings.get_auto_unload_api_only", lambda: False)
monkeypatch.setattr("utils.openai_auto_switch_settings.get_auto_unload_keep_kv", lambda: False)
+
+def test_only_the_yours_key_separates_the_two_answers(chat_resident, accounts):
+ """Why both assertions above are needed, and why "loaded" alone would not do.
+
+ With alice's claim still held, bob is supposed to see the mask, and the mask for this
+ route reports an EMPTY "loaded" - the same value a genuinely free GPU reports. So a test
+ written against "loaded" alone passes just as happily on a phantom resident. Pinning the
+ masked body here is what lets the two teardown tests above assert on "yours".
+ """
+ assert gpu_arbiter.current_owner() == gpu_arbiter.CHAT
+ assert bob_status(accounts).json() == {"loaded": [], "loading": [], "yours": False}
+
+
+def test_a_torn_down_backend_does_not_yet_mean_a_released_claim(
+ chat_resident, accounts, monkeypatch
+):
+ """Between the unload and the release, the claim is still alice's and bob sees the mask.
+
+ That interval is the reason the test below waits on the release rather than on the
+ teardown: a status read landing inside it gets the masked body, correctly, and a test that
+ called that a phantom would be blaming the product for its own timing.
+
+ The window is held open with a hook on the release itself, not by winning a scheduler
+ race. Racing would make this depend on undocumented asyncio wakeup order, and worse, it
+ would fail against an implementation that made teardown and release atomic - punishing a
+ genuine improvement. A hook on release still fires in that implementation, so this test
+ keeps asking its question rather than becoming a tripwire.
+ """
+ arm_the_idle_loop(monkeypatch)
+
+ reached_release = threading.Event()
+ may_release = threading.Event()
+ real_release = inference.release_chat_gpu_claim
+
+ def held_open():
+ reached_release.set()
+ assert may_release.wait(timeout = 5.0), "the test never let the release proceed"
+ return real_release()
+
+ monkeypatch.setattr(inference, "release_chat_gpu_claim", held_open)
+
+ async def tick():
+ task = asyncio.ensure_future(llama_keepwarm.idle_unload_loop(poll_seconds = 0.01))
+ for _ in range(500):
+ await asyncio.sleep(0.01)
+ if reached_release.is_set():
+ break
+ assert reached_release.is_set(), "the loop never reached the claim release"
+
+ # Inside the window now, and it stays open until may_release is set.
+ assert chat_resident.unloaded, "the backend should already be down here"
+ assert gpu_arbiter.current_owner() == gpu_arbiter.CHAT
+ assert bob_status(accounts).json() == {"loaded": [], "loading": [], "yours": False}
+
+ may_release.set()
+ for _ in range(500):
+ await asyncio.sleep(0.01)
+ if gpu_arbiter.current_owner() is None:
+ break
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
+
+ asyncio.run(tick())
+
+ # And once it closes, the claim is gone and bob gets the real answer.
+ assert gpu_arbiter.current_owner() is None
+ assert "yours" not in bob_status(accounts).json()
+
+
+def test_idle_auto_unload_releases_chat_ownership(chat_resident, accounts, monkeypatch):
+ arm_the_idle_loop(monkeypatch)
+
async def one_tick():
task = asyncio.ensure_future(llama_keepwarm.idle_unload_loop(poll_seconds = 0.01))
for _ in range(500):
await asyncio.sleep(0.01)
- if chat_resident.unloaded:
+ # Not chat_resident.unloaded on its own. The loop tears the backend down several
+ # awaits before it drops the claim: unload_model, then the reload stash, then
+ # clear_resident and release_chat_gpu_claim. Breaking on the teardown lands the
+ # status query inside that window, where the claim is still held and bob is
+ # correctly handed the masked answer, and the test then reports the mask as a
+ # phantom. That is what failed on main in Backend CI (Python 3.13, rest) with the
+ # body {"loaded": [], "loading": [], "yours": False}, which is exactly
+ # account_access.hidden_chat_status_response(). The release is what this test is
+ # about, so the release is what the wait watches.
+ if chat_resident.unloaded and gpu_arbiter.current_owner() is None:
break
task.cancel()
try:
@@ -129,8 +218,11 @@ def test_idle_auto_unload_releases_chat_ownership(chat_resident, accounts, monke
response = bob_status(accounts)
assert response.status_code == 200, response.text
body = response.json()
- # The hidden-resident answer is exactly {"loaded": True, "yours": False}; the real
- # status has no "yours" key and reports "loaded" as the list of resident models.
+ # "yours" is the load-bearing assertion, not "loaded". This route's mask is
+ # hidden_chat_status_response(), {"loaded": [], "loading": [], "yours": False}, so an
+ # empty "loaded" is what BOTH answers report here and only the "yours" key tells a real
+ # empty GPU from a hidden resident. test_only_the_yours_key_separates_the_two_answers
+ # pins that, so neither assertion can be dropped as redundant.
assert "yours" not in body, f"phantom resident after idle unload: {body}"
assert body.get("loaded") == [], f"phantom resident after idle unload: {body}"
assert body.get("active_model") is None, body