Skip to content

Assert the companion scan's thread, not how fast the machine is - #11288

Merged
danielhanchen merged 1 commit into
mainfrom
fix/companion-scan-thread-assertion
Sep 18, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix/companion-scan-thread-assertion

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

test_companion_root_scan_does_not_block_the_event_loop failed on main in Backend CI (Python 3.13, l-r) (run 35379669649, job 105712593993):

tests/test_openai_auto_switch.py:2670: in _drive
    assert loop_was_responsive
E   assert False

on a shard that took 565s against a 371s baseline, i.e. a slower runner.

The assertion measured the machine, not the invariant

started = time.monotonic()
task = asyncio.create_task(inference_route._maybe_auto_switch_model(...))
assert await asyncio.to_thread(entered.wait, 2.0)
loop_was_responsive = time.monotonic() - started < 0.5

The window it measures includes dispatching asyncio.to_thread(entered.wait, ...) through the default executor, so a busy runner blows the 0.5s budget while the loop is behaving perfectly. Against a 1.0s block that is a 2x margin to spend on scheduling.

What it should assert

routes.inference awaits the scan through

gguf_companion_roots = await asyncio.to_thread(
    local_gguf_companion_roots, target_id, repo_level = repo_level_companion_resolution,
)

so running on another thread is the mechanism the wall clock was standing in for. The test now records the thread the scan executed on and compares it with the loop's own thread, captured from inside the coroutine. Same claim, no timing, cannot be defeated by a slow machine.

The Event.wait timeouts that remain are generous upper bounds so a broken run fails rather than hangs. Nothing is measured against them.

Verification

make routes.inference call the scan synchronously instead of via to_thread
  ->  AssertionError: the companion scan ran on the event loop thread ...
      assert 124774791708800 != 124774791708800
restored
  ->  694 passed, 1 skipped

What I could not do

I did not reproduce this locally first, unlike the sibling fix in #11284 where a pinned cpuset reproduced the flake 2 times in 12. Here CPU contention on a 2-CPU and then a 1-CPU cpuset left it passing 10 of 10, and saturating the default executor left it passing 8 of 8.

That is stated rather than glossed because it changes what the evidence is. The diagnosis does not rest on reproducing the timing: the CI traceback names the failing assertion, and that assertion is a wall-clock threshold on a machine-dependent window. The fix removes the timing dependence entirely rather than widening a budget, so there is no threshold left to tune against a runner I cannot measure.

test_companion_root_scan_does_not_block_the_event_loop failed on main in Backend
CI (Python 3.13, l-r) at 'assert loop_was_responsive', on a shard that took 565s
against a 371s baseline.

The assertion was time.monotonic() - started < 0.5, a proxy for the loop staying
responsive. It is only a proxy, and it measures the wrong thing: the elapsed
window includes dispatching asyncio.to_thread(entered.wait, ...) through the
default executor, so a runner that is merely busy blows the 0.5s budget while
the loop is behaving perfectly. Against a 1.0s block it had a 2x margin to spend
on scheduling.

routes.inference awaits the scan through

    await asyncio.to_thread(local_gguf_companion_roots, ...)

so running on another thread IS the mechanism the wall clock was standing in
for. The test records the thread the scan executed on and compares it with the
loop's own thread, which is the same claim with no timing in it and cannot be
defeated by a slow machine. The waits that remain are generous upper bounds for
a hung test rather than thresholds anything is measured against.

Proved by making routes.inference call the scan synchronously instead of through
to_thread: the test fails with both idents equal. Restored, 694 passed.

Not reproduced locally first, unlike the sibling fix in #11284: CPU contention
on a pinned cpuset and a saturated default executor both left it passing 10 and
8 times. The CI traceback names the assertion, and the assertion is a wall-clock
threshold, so the diagnosis does not depend on reproducing the timing.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 18, 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-09-18T19:48:41.261341Z c9c6e3c Manual request
🔒 Security Review ✅ Completed 2026-09-18T19:49:54.540664Z c9c6e3c Manual request
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: c9c6e3ccc6

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

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: c9c6e3ccc6

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@danielhanchen
danielhanchen merged commit 9f76c5a into main Sep 18, 2026
37 of 43 checks passed
@danielhanchen
danielhanchen deleted the fix/companion-scan-thread-assertion branch September 18, 2026 20:41
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…othai#11288)

test_companion_root_scan_does_not_block_the_event_loop failed on main in Backend
CI (Python 3.13, l-r) at 'assert loop_was_responsive', on a shard that took 565s
against a 371s baseline.

The assertion was time.monotonic() - started < 0.5, a proxy for the loop staying
responsive. It is only a proxy, and it measures the wrong thing: the elapsed
window includes dispatching asyncio.to_thread(entered.wait, ...) through the
default executor, so a runner that is merely busy blows the 0.5s budget while
the loop is behaving perfectly. Against a 1.0s block it had a 2x margin to spend
on scheduling.

routes.inference awaits the scan through

    await asyncio.to_thread(local_gguf_companion_roots, ...)

so running on another thread IS the mechanism the wall clock was standing in
for. The test records the thread the scan executed on and compares it with the
loop's own thread, which is the same claim with no timing in it and cannot be
defeated by a slow machine. The waits that remain are generous upper bounds for
a hung test rather than thresholds anything is measured against.

Proved by making routes.inference call the scan synchronously instead of through
to_thread: the test fails with both idents equal. Restored, 694 passed.

Not reproduced locally first, unlike the sibling fix in unslothai#11284: CPU contention
on a pinned cpuset and a saturated default executor both left it passing 10 and
8 times. The CI traceback names the assertion, and the assertion is a wall-clock
threshold, so the diagnosis does not depend on reproducing the timing.
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.

1 participant