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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: wake admission waits on queue state changes instead of their next poll (#15885)
Daniel Han6165c444eda9
diff --git a/studio/backend/core/inference/llama_admission.py b/studio/backend/core/inference/llama_admission.pyindex e651bcf6ff4..061c6ae39b2 100644--- a/studio/backend/core/inference/llama_admission.py+++ b/studio/backend/core/inference/llama_admission.py@@ -420,6 +420,8 @@ class LlamaAdmissionLease:deadline = None if not timeout_s or timeout_s <= 0 else time.monotonic() + timeout_stry:while True:+ # Read before the test, so a change between the test and the wait is not missed.+ seen = queue._changesif queue.try_reclaim_commitment(want):with self._release_lock:if self._released:@@ -436,7 +438,7 @@ class LlamaAdmissionLease:return self._give_up_repark(queue, held, cancelled = True)if deadline is not None and time.monotonic() >= deadline:return self._give_up_repark(queue, held, cancelled = False)- time.sleep(poll_s)+ queue.wait_for_change(seen, poll_s)except BaseException:self._give_up_repark(queue, held, cancelled = True)raise@@ -576,6 +578,10 @@ class LlamaAdmissionQueue:"_committed","_budget","_reparking",+ "_changes",+ "_changed",+ "_change_sleepers",+ "_change_events",)def __init__(self, key: str):@@ -601,6 +607,11 @@ class LlamaAdmissionQueue:self._budget = 0# Reparkers still hold a slot, so they are not in _waiters. See yield_commitment.self._reparking = 0+ # Bumped by every _grant_waiters_locked; the poll stays for what never notifies (cancel event, deadline).+ self._changes = 0+ self._changed = threading.Condition(self._lock)+ self._change_sleepers = 0+ self._change_events: dict = {}def _resize_pool_locked(self, capacity: int) -> None:# Slots past a shrunk capacity retire when their holder releases them.@@ -869,15 +880,20 @@ class LlamaAdmissionQueue:) -> Optional[int]:"""Ordered by ticket so approvals resume in return order; the claim never admits over budget."""tokens = max(0, int(tokens or 0))+ loop = asyncio.get_running_loop()+ changed = asyncio.Event()with self._lock:self._unpark_seq += 1ticket = self._unpark_seqself._unpark_tickets.append(ticket)self._unpark_tokens[ticket] = tokens+ self._change_events[ticket] = (loop, changed)deadline = None if not timeout_s or timeout_s <= 0 else time.monotonic() + timeout_stry:while True:with self._lock:+ # Cleared under the lock before the test: a later change sets it again.+ changed.clear()if deadline is not None and time.monotonic() >= deadline:self._unpark_lapsed.add(ticket)deadline = None@@ -890,9 +906,14 @@ class LlamaAdmissionQueue:return Noneif abandoned is not None and abandoned():return None- await asyncio.sleep(poll_s)+ timer = loop.call_later(poll_s, changed.set)+ try:+ await changed.wait()+ finally:+ timer.cancel()finally:with self._lock:+ self._change_events.pop(ticket, None)try:self._unpark_tickets.remove(ticket)except ValueError:@@ -933,7 +954,27 @@ class LlamaAdmissionQueue:# A parked holder will return here; evicting would resume it against a fresh 1-slot pool.return self._in_use == 0 and not self._waiters and not self._parked+ def wait_for_change(self, seen: int, timeout_s: float) -> None:+ """Sleeps up to ``timeout_s``, returning early once the state has changed since ``seen``."""+ with self._lock:+ if self._changes == seen:+ self._change_sleepers += 1+ try:+ self._changed.wait(max(0.0, timeout_s))+ finally:+ self._change_sleepers -= 1+def _grant_waiters_locked(self) -> None:+ self._changes += 1+ # notify_all probes lock ownership even with no sleeper, doubling reserve + release.+ if self._change_sleepers:+ self._changed.notify_all()+ if self._change_events:+ for loop, event in self._change_events.values():+ try:+ loop.call_soon_threadsafe(event.set)+ except RuntimeError:+ pass# Reparkers go first, or steady arrivals hold a growing run at its old size.if self._reparking > 0:returndiff --git a/studio/backend/tests/test_llama_admission_wakeup.py b/studio/backend/tests/test_llama_admission_wakeup.pynew file mode 100644index 00000000000..7c79ac28170--- /dev/null+++ b/studio/backend/tests/test_llama_admission_wakeup.py@@ -0,0 +1,124 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Waits for room wake when the room appears, not on their next poll; the poll only bounds what+does not notify (the cancel event, a release-only predicate, the deadline)."""++from __future__ import annotations++import asyncio+import threading+import time++from core.inference.llama_admission import LlamaAdmissionConfig, LlamaAdmissionQueue++# Far longer than any wake below may take, so a wait that only polls fails these tests.+_SLOW_POLL_S = 5.0+++def _lease(+ queue,+ *,+ capacity,+ tokens = None,+ budget = None,+):+ reservation = queue.reserve(+ capacity = capacity, config = LlamaAdmissionConfig(), tokens = tokens, budget = budget+ )+ lease = reservation.lease_nowait()+ assert lease is not None+ return lease+++def test_a_growth_waiting_for_room_wakes_when_a_holder_releases():+ async def scenario():+ queue = LlamaAdmissionQueue("wake-recost")+ first = _lease(queue, capacity = 4, tokens = 2000, budget = 4096)+ second = _lease(queue, capacity = 4, tokens = 2000, budget = 4096)+ done: list = []++ def grow():+ done.append(second.recost_waiting(3500, poll_s = _SLOW_POLL_S))++ thread = threading.Thread(target = grow, daemon = True)+ thread.start()+ thread.join(0.2)+ assert thread.is_alive(), "expected the growth to wait"+ start = time.monotonic()+ first.release()+ thread.join(_SLOW_POLL_S * 2)+ waited = time.monotonic() - start+ assert done == [True]+ assert waited < 1.0, f"woke {waited:.2f}s after the release, on the poll"+ assert queue.snapshot().committed == 3500++ asyncio.run(scenario())+++def test_a_cancel_still_ends_the_growth_wait_within_the_poll():+ async def scenario():+ queue = LlamaAdmissionQueue("wake-recost-cancel")+ _lease(queue, capacity = 4, tokens = 2000, budget = 4096)+ second = _lease(queue, capacity = 4, tokens = 2000, budget = 4096)+ cancel = threading.Event()+ done: list = []++ def grow():+ done.append(second.recost_waiting(3500, cancel_event = cancel, poll_s = 0.05))++ thread = threading.Thread(target = grow, daemon = True)+ thread.start()+ thread.join(0.2)+ assert thread.is_alive()+ cancel.set()+ thread.join(5)+ assert done == [False]+ assert queue.snapshot().committed == 4000++ asyncio.run(scenario())+++def test_a_parked_holder_resumes_when_the_slot_frees():+ async def scenario():+ queue = LlamaAdmissionQueue("wake-unpark")+ config = LlamaAdmissionConfig()+ a = queue.reserve(capacity = 1, config = config)+ a_lease = a.lease_nowait()+ b = queue.reserve(capacity = 1, config = config)+ a_lease.park()+ b_lease = await asyncio.wait_for(b.wait(timeout_s = 1), timeout = 2)+ assert b_lease is not None++ resumed = asyncio.ensure_future(a_lease.unpark_async(poll_s = _SLOW_POLL_S))+ await asyncio.sleep(0.05)+ assert not resumed.done()+ start = time.monotonic()+ threading.Thread(target = b_lease.release).start()+ await asyncio.wait_for(resumed, timeout = _SLOW_POLL_S * 2)+ waited = time.monotonic() - start+ assert a_lease.slot is not None+ assert waited < 1.0, f"woke {waited:.2f}s after the release, on the poll"+ assert queue._change_events == {}, "a finished wait must not stay registered"++ asyncio.run(scenario())+++def test_a_parked_holder_still_gives_up_on_cancel_within_the_poll():+ async def scenario():+ queue = LlamaAdmissionQueue("wake-unpark-cancel")+ config = LlamaAdmissionConfig()+ a = queue.reserve(capacity = 1, config = config)+ a_lease = a.lease_nowait()+ b = queue.reserve(capacity = 1, config = config)+ a_lease.park()+ assert await asyncio.wait_for(b.wait(timeout_s = 1), timeout = 2) is not None+ cancel = threading.Event()+ waiting = asyncio.ensure_future(a_lease.unpark_async(cancel_event = cancel, poll_s = 0.05))+ await asyncio.sleep(0.1)+ assert not waiting.done()+ cancel.set()+ await asyncio.wait_for(waiting, timeout = 2)+ assert a_lease.slot is None++ asyncio.run(scenario())