Skip to content

Warm the app before timing /api/health, instead of widening the ceiling - #9724

Merged
danielhanchen merged 4 commits into
mainfrom
health-budget-ceiling
Aug 26, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
health-budget-ceiling

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 25, 2026 •

Copy link
Copy Markdown
Member

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_slow fails intermittently in the (Python 3.13) leg of Backend CI:

AssertionError: /api/health took 1.63s with detection still running; the budget is 1.0s
and the desktop probe gives up at 2s

It is not specific to any branch:

  • The same test failed at 1.69s on feat/logging-export-zip, an unrelated branch, 54 minutes before the occurrence that prompted this.
  • Both failures were the only failure in a run of roughly 30,780 tests.
  • On the branch where I hit it, (Python 3.13) passed five times and then failed on the sixth, on a commit whose diff touches only tests/studio/. That job runs with working-directory: studio/backend, so its tests/ 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 in main.py was 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:

idle pinned to 2 CPUs, 6 spinners
timed request, warmed 1.012-1.013s 1.034-1.045s
the warm call itself 0.009s 0.063s

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 of preflight/backend.rs by 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_unbounded is a static AST check that health_check goes through _await_hardware_detection and does not call ensure_hardware_detected directly. It cannot flake.
  • test_the_budget_stays_under_the_desktop_probe_timeout pins the cross-language relationship by reading _HEALTH_DETECT_BUDGET_S out of main.py and the client timeout out of preflight/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

  • The budget test asserts its ceiling stays below the launcher deadline and below half the stubbed detection time, so moving it in either unsafe direction fails immediately rather than going quietly vacuous.
  • It asserts the warm call really was warm, so a warm-up that stops warming fails loudly instead of silently putting setup back into the number.
  • test_the_ceiling_still_catches_an_unbounded_wait builds 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:

Mutation Result
Ceiling pushed up to the launcher's 2s deadline Fails the new deadline guard
Ceiling pushed past the point of discriminating Fails that guard and the calibration test
Calibration handler made bounded Fails its own precondition
Warm call left waiting on detection Fails the warm-cost precondition

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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

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
danielhanchen force-pushed the health-budget-ceiling branch from a82a4bc to 5e69e9e Compare August 25, 2026 21:10
@danielhanchen danielhanchen changed the title Put the health-budget ceiling in the gap it measures, not at the edge of it Warm the app before timing /api/health, instead of widening the ceiling Aug 25, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…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.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen and others added 2 commits August 25, 2026 23:05
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.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 25, 2026
@danielhanchen
danielhanchen merged commit 540abc2 into main Aug 26, 2026
32 of 35 checks passed
@danielhanchen
danielhanchen deleted the health-budget-ceiling branch August 26, 2026 03:26
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