Skip to content

Take the wall clock out of two backend guards that fail on a loaded runner - #10952

Merged
danielhanchen merged 6 commits into
mainfrom
fix-two-flaky-backend-guards
Sep 15, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
fix-two-flaky-backend-guards

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Three backend guards that fail on a busy runner rather than on a defect. All three were found failing on other people's open PRs, not hypothesised.

test_cache_does_not_bypass_expiry (failed on #10950)

AssertionError: assert None == 'unsloth'

It minted a key expiring 600ms out, validated it to warm the cache, slept 800ms and expected the second call to be rejected. The first call races the KDF, which on a loaded runner can outlast any margin short enough to keep the sleep cheap.

Now shaped like its neighbour test_cache_does_not_bypass_revocation: warm the cache on a comfortably valid key, expire it in the row, re-validate. It still proves the thing that matters, that a cache hit re-checks expires_at, and it no longer depends on the wall clock. It also asserts the cache was actually populated, so the second call is known to take the cache path rather than passing for free.

The ReDoS guards (test_no_backtrack_on_tilde_fence_spam failed on #10941)

AssertionError: guard took 60.9ms on ~~~ spam

These look for catastrophic backtracking, which costs seconds or minutes, not a few extra milliseconds. A wall clock cannot tell a regressed quantifier from the runner descheduling the process mid-match: the tilde case measures about 11ms of work and was seen at 60.9ms for that reason alone.

The quantity the budget is about is the regex's own CPU time, so it is measured with time.process_time(), which does not advance while the process is off the CPU, best of five for the contention process_time cannot see. A genuine blow-up survives both, since every repeat pays it in full. Same pairing as test_tool_loop_controller.py.

test_trickling_response_headers_cannot_outlive_the_probe_budget (failed on #10928)

AssertionError: fixture stopped trickling headers after 0 bytes

The probe had timed out exactly as intended. The fixture dribbles response headers from its own thread, and the test read state["lines"] >= 3 the instant the probe returned, which assumes 0.5s of probe bought that thread 0.5s of CPU.

The first fix waited for the fixture's first byte instead. That was too weak, and in the direction that matters: urllib applies a per-socket-operation timeout, so a peer that sends one byte and then goes quiet trips it on its own, the probe returns "timeout" in budget, and every assertion below passes against a build with no whole-request deadline at all.

So the fixture now records the longest silence it ever left. While every gap stays under the socket timeout, and the last write lands within one timeout of the probe returning, no per-operation timeout can have fired, so a probe that returned anyway returned on the whole-request deadline. That is the distinction the test exists to make, asserted rather than assumed. A counter could not express it, because a count cannot say when the bytes landed.

A runner too contended to hold that has not produced a failure, it has produced no evidence, so the test skips with the measured numbers instead of passing on a broken build or going red on a busy machine.

The companion lines < 100 bound went the same way. It was a proxy for "the fixture is not sending whole header lines" measured through a byte count that a slow runner made small and a fast one made large, so it is now asserted against the repeated chunk itself.

Verification

156 passed across the three files.

Mutation-checked both directions on the trickling guard: a fixture that stalls after one byte skips where the first version passed, and replacing worker.join(timeout) with worker.join() fails with the fixture still trickling.

Tests only, no production code.

…unner

test_cache_does_not_bypass_expiry issued a key expiring in 600ms and asserted
the first validate_api_key call still accepted it. That call runs PBKDF2, so on
a busy runner the key is already expired when the first assertion reads it:
assert None == 'unsloth'. Rewritten in the shape of its neighbour
test_cache_does_not_bypass_revocation, warming the cache on a key valid for a
day and then expiring the row, which also drops the 0.8s sleep. Verified it
still fails when the expiry check is made conditional on a cache miss.

The four ReDoS guards asserted a single wall-clock sample under 50ms. The tilde
case measures about 11ms and was seen at 60.9ms on CI, which is preemption, not
backtracking. They now take the fastest of five runs, since catastrophic
backtracking costs seconds and every repeat pays it.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 14, 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-15T05:09:19.648035Z f4f4e1c 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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

pre-commit-ci Bot and others added 3 commits September 14, 2026 22:13
perf_counter still counts the time this process spends off the CPU, so under
sustained contention every one of the five samples carries it and the minimum
can still cross the budget. The budget is about the regex's own CPU work, so
process_time measures it directly. Best-of-five stays on top for the
contention process_time cannot see. Same pairing as test_tool_loop_controller.
…xture

Third guard in the same family as the two already here. The fixture dribbles
response headers from its own thread, and the test asserted `lines >= 3`
immediately after the probe returned, which assumes 0.5s of probe bought that
thread 0.5s of CPU. On a loaded runner it does not: seen on #10928 as

    AssertionError: fixture stopped trickling headers after 0 bytes

on a probe that had timed out exactly as intended, so the assertion was
reporting contention as a broken fixture.

It now waits on an event the drip loop sets after its first byte instead of
sampling a counter at an arbitrary moment.

The companion `lines < 100` bound went the same way. It was a proxy for "the
fixture is not sending whole header lines", measured through a byte count that a
slow runner made small and a fast one made large. The property is about what the
fixture writes, not how often it got to write it, so it is now asserted against
the repeated chunk itself.

Both directions mutation-checked: a fixture that never trickles fails the first,
and a chunk carrying CRLF fails the second.
@danielhanchen

Copy link
Copy Markdown
Member Author

Added a third guard in the same family, found while sweeping the failing CI on the open PRs.

tests/studio/test_mac_tab_capability_warm_window.py::test_trickling_response_headers_cannot_outlive_the_probe_budget failed on #10928 with:

AssertionError: fixture stopped trickling headers after 0 bytes

The probe there had timed out exactly as intended. The fixture dribbles response headers from its own thread, and the test read state["lines"] >= 3 the instant the probe returned, which assumes that 0.5s of probe bought that thread 0.5s of CPU. On a loaded runner it does not, so the assertion reported contention as a broken fixture. It now waits on an event the drip loop sets after its first byte.

The companion lines < 100 bound had the same defect in the other direction: it was a proxy for "the fixture is not sending whole header lines", measured through a byte count that a slow runner made small and a fast one made large. That property is about what the fixture writes, so it is now asserted against the repeated chunk itself.

Both mutation-checked: a fixture that never trickles fails the first assertion, a chunk carrying CRLF fails the second.

For the two guards this PR already had, the sweep confirmed both are live failures on other people's PRs rather than hypothetical:

Local: 156 passed across the three files.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@debojyoti-dev

Copy link
Copy Markdown
Contributor

will this now merge the #10928 ? @danielhanchen

The previous version waited for the fixture's first byte and accepted it. That is
too weak, and in the direction that matters. urllib applies its own
per-socket-operation timeout, so a peer that sends one byte and then goes quiet
for longer than that trips it on its own, the probe returns "timeout" inside
budget, and every assertion below passes against a build with no whole-request
deadline at all. The old count-based check was reaching for this and could not
express it, because a count cannot say when the bytes landed.

The fixture now records the longest silence it ever left. While every gap stays
under the socket timeout, and the last write lands within one timeout of the
probe returning, no per-operation timeout can have fired, so a probe that
returned anyway returned on the whole-request deadline. That is the distinction
the test exists to make, asserted rather than assumed.

A runner too contended to hold that has not produced a failure, it has produced
no evidence, so the test skips with the measured numbers instead of passing on a
broken build or going red on a busy machine.

Mutation-checked both ways: a fixture that stalls after one byte now skips where
it previously passed, and replacing worker.join(timeout) with worker.join() fails
with the fixture still trickling.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 15, 2026
@danielhanchen
danielhanchen merged commit 4938d2b into main Sep 15, 2026
33 of 35 checks passed
@danielhanchen
danielhanchen deleted the fix-two-flaky-backend-guards branch September 15, 2026 06:07
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