Skip to content

Studio: re-run admission when a cancelled waiter was blocking the line - #9415

Merged
danielhanchen merged 3 commits into
mainfrom
studio/admission-cancel-regrant
Aug 23, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio/admission-cancel-regrant

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #9392, which added the KV budget to llama admission. Reviewing that PR turned up a deadlock in LlamaAdmissionQueue.cancel() that the budget makes reachable, and I got the diagnosis wrong the first time, so this fixes the mechanism that is actually live.

The bug

Every path that frees capacity ends by re-running admission. cancel() did not:

release()                   :706-718   -> self._grant_waiters_locked()
try_park()                  :720-731   -> self._grant_waiters_locked()
acquire_parked_slot() finally :772-778 -> self._grant_waiters_locked()
cancel()                    :780-800   -> nothing

A cancelled waiter owns no slot and no tokens, so on the face of it there is nothing to hand back and nothing to re-run. That is true for a waiter in the middle of the line. It is not true for the head, because _grant_waiters_locked tests the head's own cost against the remaining budget and stops there:

while self._waiters and self._can_admit_locked(
    len(self._unpark_tickets), self._waiters[0].tokens
):

That head-of-line rule is deliberate (granting past a large waiter whenever a small one fits would starve it). But it means a large head blocks smaller waiters behind it, and when that head cancels, nothing re-opens the line.

Reproduction

Capacity 4, budget 2048. A=1000 admitted. B=1500 queued, blocking on budget. C=500 queued behind it. Cancel B through the ordinary public path, reservation.cancel():

A lease: True
B granted immediately? False
C granted immediately? False
C after b.cancel(): STILL QUEUED
snapshot: capacity=4, active=1, queued=1, free=3, committed=1000, budget=2048

C wants 500 tokens with three free slots and 1048 free budget, and stays queued. It is only reconsidered on the next reserve or release. When the one active lease is parked waiting on a tool approval prompt, that never arrives.

The fix

One call, inside cancel(), still under _lock:

self._grant_waiters_locked()

The cancelled waiter is already off _waiters, so the loop sees the new head. In the granted_lease branch those tokens are still in _committed at that point, so granting here is conservative and the lease_to_release.release() below re-grants anyway. _grant_waiters_locked only does call_soon_threadsafe, so there is no re-entrant lock acquisition. FIFO is unchanged.

Tests

TestCancellingTheBlockingHeadReopensTheLine in tests/test_llama_admission_kv_budget.py:

  • test_a_smaller_waiter_runs_once_the_oversized_head_is_cancelled reproduces the deadlock. Fails before this commit, passes after.
  • test_cancelling_a_waiter_that_is_not_the_head_admits_nobody_early guards the head-of-line trade in the other direction: cancelling a tail waiter must not let anyone skip the oversized head. Passes before and after, so the fix does not quietly turn FIFO into best-fit.

test_llama_admission_kv_budget, test_llama_admission_kv_budget_media_and_tools, test_llama_admission, test_anthropic_admission: 130 passed.

Note on the review of #9392

An earlier revision of this was raised there and I turned it down, on the grounds that a cancelled waiter can never be left at the head because cancel() removes it under the lock before cancelling its future. That part is correct and still is. It was the wrong thing to check: the bug is not a dead waiter sitting at the head, it is a live head being removed with nothing re-running admission behind it. I claimed a control run through cancel() granted the waiter immediately. It does not, and re-running it cleanly is what turned this up.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 923bcb3b01

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

Confirmed against main: cancel() in studio/backend/core/inference/llama_admission.py is the only capacity-freeing path that does not re-run _grant_waiters_locked, so the waiters behind a cancelled oversized head sit there until unrelated traffic arrives. Will get this reviewed.

@danielhanchen
danielhanchen force-pushed the studio/admission-cancel-regrant branch from 923bcb3 to 9337ebc Compare August 23, 2026 01:13
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 9337ebcbe6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 53fea3e350

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit c55e609 into main Aug 23, 2026
36 checks passed
@danielhanchen
danielhanchen deleted the studio/admission-cancel-regrant branch August 23, 2026 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant