Repository navigation
Warm the app before timing /api/health, instead of widening the ceiling - #9724
Merged
Merged
Conversation
test_health_answers_within_the_budget_while_detection_is_slow fails intermittently: 1.63s and 1.69s against a 1.5s ceiling, on two unrelated branches 54 minutes apart, each time as the only failure in a run of about 30,780 tests. Neither was the endpoint being slow. The budget wait is deadline-accurate, polling against loop.time(), and measures 1.02s whether the box is idle or pinned to two CPUs under six spinners. The rest was one-time setup charged to the endpoint because the test timed the first-ever call: the lazy imports inside health_check, the anyio threadpool the auth dependency hops onto, and TestClient's portal. That setup is the only part that moves under load, and it moved by 0.63s and 0.69s against the 0.24s the budget beside it in main.py was tuned for, on a job that runs -n 4 on a 4-vCPU runner and whose own workflow comment says the suite "fails tests serial does not, in more than one way and not the same way twice". So the fix is to stop measuring the setup, not to allow more of it. The probe now warms the app first, with detection already settled so that call returns at once, and times the second request. Measured that way it is 1.012-1.013s idle and 1.034-1.045s under the same contention: about 35ms of movement across a 6x change in load, against 630ms before. The variance goes where it belongs, into the warm call, which moves from 0.009s to 0.063s over the same range. Eight consecutive runs of the real test under six spinners on two CPUs pass. The ceiling therefore stays at 1.5s rather than being widened, and is now derived from the launcher rather than chosen: the desktop probe gives up at _desktop_probe_timeout_s() and does not retry, so the test asserts its own ceiling is below that. A ceiling at or past the launcher's deadline would accept responses that dead-end the launch, which is what widening the allowance would have cost. That timeout was already being read out of the Rust by the cross-language test here; it is now a helper both use. Widening or tightening either bound is checked rather than trusted. The budget test asserts its ceiling stays below the launcher deadline and below half the stubbed detection time, and asserts the warm call really was warm, so a warm-up that stops warming fails instead of quietly putting setup back into the number. test_the_ceiling_still_catches_an_unbounded_wait builds the unbounded wait the static sibling guard forbids, times it, and asserts it lands above the ceiling. Worth saying plainly: this assertion is a backstop. What actually protects the invariant is static and cannot flake. test_health_does_not_await_detection_unbounded checks by AST that health_check goes through _await_hardware_detection and never calls ensure_hardware_detected directly, and test_the_budget_stays_under_the_desktop_probe_timeout pins the budget against the Rust timeout with 0.9s of required headroom. The stubbed detection time is a named constant now, since the ceiling checks itself against it. Checked by mutation: pushing the ceiling to the launcher deadline fails the new deadline guard; pushing it past the point of discriminating fails that guard and the calibration test; making the calibration handler bounded fails its own precondition; and leaving the warm call to wait on detection fails the warm-cost precondition.
danielhanchen
force-pushed
the
health-budget-ceiling
branch
from
August 25, 2026 21:10
a82a4bc to
5e69e9e
Compare
…ad of timing it Two corrections to the warm-up this change introduced. The portal was not being warmed. A TestClient used outside its context manager leaves self.portal unset, and _portal_factory then starts and tears down a fresh blocking portal for every request, so the warm call warmed its own portal and the timed call built another. Portal setup was one of the three costs named as the reason for warming, so the claim in the comment was wrong even though the headline numbers were not: they were measured on that same unwarmed path, so the portal was already inside them. Measured on its own it is 3.4ms idle and 3 to 33ms under contention, against the 630ms that made this flaky, so it was never the cause. It is now excluded rather than tolerated: the client is entered once and both requests share the portal, which improves the timed request by a few milliseconds and makes the comment true. The warm-up was then being validated by a stopwatch. Asserting the warm call took under half the budget is the same subsecond wall-clock ceiling this change exists to remove, on the same contended runners, and it can exceed 0.5s from descheduling or a slow one-time import, which is exactly the overhead documented here. Worse, it says nothing about the property: DETECTION_COMPLETE is set before that call, so its duration cannot distinguish a warm call from a slow one. The property is that the warm call took the settled path, and health reports that directly by omitting hardware_detecting when detection is complete. That is what is asserted now. warm_cost is still reported, for the failure message only, and never asserted. The entered client is checked the same way, on state rather than duration: TestClient.portal is None until the client is entered, so the probe reports whether it was non-None and the test asserts it. A timing check could not have caught this at all, since 3 to 33ms sits well inside any usable allowance, and the mutation that un-enters the client passed every test until this assertion existed. Re-measured after both changes: the timed request is 1.011-1.012s idle and 1.027-1.038s pinned to two CPUs under six spinners, and eight consecutive runs of the real test under that contention pass. The ceiling stays at 1.5s, still derived from _desktop_probe_timeout_s() and still asserted to be below the launcher deadline. Checked by mutation: un-entering the client fails the portal assertion; leaving the warm call to wait on detection fails the warm-state assertion; pushing the ceiling to the launcher deadline or past the point of discriminating fails the two guards on it; and making the calibration handler bounded fails its own precondition.
probe_ownerless_spawned_backend calls /api/health immediately after startup with no warm-up and does not retry, so the first call the process ever serves is the one on its 2s deadline: the lazy imports inside health_check and the anyio threadpool the auth dependency hops onto are all on that critical path. The previous shape timed only the warmed request, which left that path unguarded, so a regression adding a second of first-call import would have passed. The probe now takes two measurements of the same request. The cold one runs first, with detection unset and slow, and is bounded against the launcher's own timeout read out of preflight/backend.rs, because setup legitimately counts against that deadline. The warm one repeats it with that setup already paid and keeps the tight ceiling, because what remains is the budget wait and it does not move. Neither bound replaces the other: warm catches the endpoint regressing, cold catches setup growing until the launch dead-ends, and timing only the cold one is what made this file flaky in the first place. The warm-up is still validated on state rather than on elapsed time. The cold reply has to carry hardware_detecting, or it never exercised the wait and the timed request below is measuring one-time setup again.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The evidence this is a repo-wide flake
studio/backend/tests/test_health_answers_within_probe_budget.py::test_health_answers_within_the_budget_while_detection_is_slowfails intermittently in the(Python 3.13)leg of Backend CI:It is not specific to any branch:
feat/logging-export-zip, an unrelated branch, 54 minutes before the occurrence that prompted this.(Python 3.13)passed five times and then failed on the sixth, on a commit whose diff touches onlytests/studio/. That job runs withworking-directory: studio/backend, so itstests/is a different tree and it never collects those files at all.The setup was being charged to the endpoint
Neither failure was the endpoint being slow. The two terms behave differently.
The budget wait does not move. It polls a monotonic deadline (
loop.time()), and measures 1.02s whether the box is idle or pinned to two CPUs under six spinners.The one-time setup does move, and the test was timing the first-ever call, so all of it was charged to the endpoint: the lazy imports inside
health_check, the anyio threadpool the auth dependency hops onto, and TestClient's portal. That was 0.63s and 0.69s in the two failures, against the 0.24s the budget beside it inmain.pywas tuned for.So the fix is to stop measuring the setup, not to allow more of it. The probe now warms the app first, with detection already settled so that call returns at once, and times the second request:
About 35ms of movement across a 6x change in load, against 630ms before. The variance goes where it belongs. Eight consecutive runs of the real test under that contention pass.
The ceiling stays at 1.5s, and is now derived from the launcher
Because the noisy term is gone, the ceiling does not need widening, which matters: the desktop probe gives up at 2s and does not retry, so any ceiling at or past that would accept responses that dead-end the launch. The test now asserts its own ceiling is below
_desktop_probe_timeout_s(), read out ofpreflight/backend.rsby a helper the cross-language test here already needed.The regression being guarded, an unbounded
await asyncio.to_thread(ensure_hardware_detected), measures 10.01s (built and timed, not assumed).The wall-clock assertion is a backstop, not the load-bearing guard
This is the strongest reason widening it is safe, so it should not be buried:
test_health_does_not_await_detection_unboundedis a static AST check thathealth_checkgoes through_await_hardware_detectionand does not callensure_hardware_detecteddirectly. It cannot flake.test_the_budget_stays_under_the_desktop_probe_timeoutpins the cross-language relationship by reading_HEALTH_DETECT_BUDGET_Sout ofmain.pyand the client timeout out ofpreflight/backend.rs, and requires 0.9s of headroom. Also static, also cannot flake.Nothing depends on the wall-clock ceiling being tight.
Both bounds are checked rather than trusted
test_the_ceiling_still_catches_an_unbounded_waitbuilds the unbounded handler the static guard forbids, times it, and asserts it lands above the ceiling. It uses a 6s detection to stay cheap and asserts up front that it really did model an unbounded wait.The stubbed detection time is a named constant (
_SLOW_DETECT_S) now, since the ceiling checks itself against it.Verification
Four mutations, each caught, each naming the fault:
Eight consecutive runs of the budget test under six spinners pinned to two CPUs: 0 failures.
studio/backend/tests/test_health_answers_within_probe_budget.py: 9 passed. Ruff clean.No product code changes; this is a test-only fix.