Skip to content

Give the cancel drain a budget sized for a loaded runner - #10008

Merged
danielhanchen merged 1 commit into
mainfrom
fix/cancel-drains-worker-budgets
Aug 30, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix/cancel-drains-worker-budgets

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Backend CI → (Python 3.13) failed on main 8849f481d:

test_non_streaming_gguf_cancel_drains_worker - assert False

That assert is await asyncio.to_thread(started.wait, 1.0), so what timed out is a thread start. It is a bound on the scheduler, not on the code under test, and the job was running the rest of the backend suite beside it.

The change

What this test asserts is that cancelling the request drains the worker. None of the numbers are part of that claim; they only decide how long to wait before calling it hung. They move to one constant, 30.0, sized for a loaded runner rather than for the ~5ms this takes when it works.

The last assertion also stops being a sample:

assert released.is_set()          # before
assert await asyncio.to_thread(released.wait, self._DRAIN_BUDGET_S)   # after

Cancelling the task unblocks the awaiting coroutine; it does not join the worker, which is off polling cancel_event every 5ms and only then sets released. Reading it the instant the await returns is a race that happens to be won on an idle box. The drain is the point, not whether it had already finished by the time the test looked.

It keeps its teeth

With the worker changed to ignore cancel_event entirely:

FAILED tests/test_openai_tool_passthrough.py::TestGgufVisionToolRouting::test_non_streaming_gguf_cancel_drains_worker
1 failed, 346 deselected in 50.05s

What this is, honestly

A robustification, not a demonstrated fix. The original failure did not reproduce here. Pinned to one core against 16 spinners the test still passed, taking 173s against a normal 4.4s — a 39x slowdown that never came near the one-second bound. It is one failure in eight runs on main, with the four commits before it green.

I am not claiming to have proven the mechanism. The budgets were incidental to the assertion either way, and widening them costs nothing on a test that is only slow when something is genuinely wrong.

studio/backend/tests/test_openai_tool_passthrough.py: 347 passed.

Backend CI (Python 3.13) failed on main 8849f48:

  test_non_streaming_gguf_cancel_drains_worker - assert False

That assert is `await asyncio.to_thread(started.wait, 1.0)`, so what timed out
is a THREAD START. It is a bound on the scheduler, not on the code under test,
and the job was running the rest of the backend suite beside it.

What this test asserts is that cancelling the request drains the worker. None of
the numbers are part of that claim: they only decide how long to wait before
calling it hung. So they move to one constant, 30s, sized for a loaded runner
rather than for the ~5ms this takes when it works.

The last assertion also stops being a sample. Cancelling the task unblocks the
awaiting coroutine; it does not join the worker, which is off polling
cancel_event every 5ms and only then sets `released`. Reading `released.is_set()`
the instant the await returns is a race that happens to be won on an idle box.
It is waited on now, because the drain is the point, not whether it had already
finished by the time the test looked.

It keeps its teeth. With the worker changed to ignore cancel_event entirely, the
test fails, taking 50s to do it:

  FAILED ...::test_non_streaming_gguf_cancel_drains_worker
  1 failed, 346 deselected in 50.05s

Honest about what this is: a robustification, not a demonstrated fix. The
original failure did not reproduce here. Pinned to one core against 16 spinners
the test still passed, taking 173s against a normal 4.4s, a 39x slowdown that
never came near the one-second bound. One failure in eight runs on main, green
on the four commits before it. The budgets were incidental either way, and
widening them costs a test that is already only slow when something is wrong.

studio/backend/tests/test_openai_tool_passthrough.py: 347 passed.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-08-30T10:50:17.661493Z b2120cf PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b2120cf4a5

ℹ️ 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".

# instant the await returns is a race that happens to be won on an
# idle box, and it is the drain itself that matters, not whether it
# had already finished by the time we looked.
assert await asyncio.to_thread(released.wait, self._DRAIN_BUDGET_S)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the drain-before-slot-release assertion

Do not wait for released after the cancelled request task has already completed. If cancellation ever returns and releases the admission slot before the blocking worker exits, the worker will normally observe cancel_event milliseconds later and this new 30-second wait will still pass, so the test no longer detects the concurrency regression named in its title. The original immediate check is not inherently racy under the required behavior: _run_blocking_generation must drain the worker before propagating cancellation, and the worker sets released before it returns.

Useful? React with 👍 / 👎.

Comment on lines +5341 to +5344
# Wall-clock bound for the cancel drain. Only ever hit when something is
# genuinely stuck, so it is sized for a loaded runner rather than for the
# ~5ms this takes when it works.
_DRAIN_BUDGET_S = 30.0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Apply the budget to the test that actually failed

Use this new budget in the immediately following test_non_streaming_gguf_cancel_drains_worker, which is the exact test identified as flaky in the commit message. Its thread-start and task waits remain hard-coded to 1.0, so a loaded runner can reproduce the same failure despite this change; only the earlier tool-enabled sibling currently references _DRAIN_BUDGET_S.

Useful? React with 👍 / 👎.

@danielhanchen
danielhanchen merged commit f1fc268 into main Aug 30, 2026
34 of 36 checks passed
@danielhanchen
danielhanchen deleted the fix/cancel-drains-worker-budgets branch August 30, 2026 13:00
InfoSage05 pushed a commit to InfoSage05/unsloth that referenced this pull request Sep 23, 2026
…the drain check deterministic (unslothai#11662)

test_non_streaming_gguf_cancel_drains_worker failed again with assert False on its
1.0s start wait (Backend CI on unslothai#11644). unslothai#10008 sized this for a loaded runner, but its
hunk landed on the tool twin of the test it named, so that test and three others
shaped like it kept waiting 1.0s for a thread-pool worker to be scheduled.

All of them now use one module-level _DRAIN_BUDGET_S (30s, only reached when
something is stuck). The stub workers also linger 0.3s after seeing the cancel flag,
so a route that skips its drain is caught by released.is_set() every time rather
than whenever the assertion beats the worker's 5 ms poll.
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…the drain check deterministic (unslothai#11662)

test_non_streaming_gguf_cancel_drains_worker failed again with assert False on its
1.0s start wait (Backend CI on unslothai#11644). unslothai#10008 sized this for a loaded runner, but its
hunk landed on the tool twin of the test it named, so that test and three others
shaped like it kept waiting 1.0s for a thread-pool worker to be scheduled.

All of them now use one module-level _DRAIN_BUDGET_S (30s, only reached when
something is stuck). The stub workers also linger 0.3s after seeing the cancel flag,
so a route that skips its drain is caught by released.is_set() every time rather
than whenever the assertion beats the worker's 5 ms poll.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants