Repository navigation
fix(core): record cancelled attempts that race stream open - #3681
Merged
bobleer merged 2 commits intoNov 2, 2026
Merged
Conversation
A model attempt cancelled after the provider returned response headers but before stream processing started returned Cancelled without writing its ledger record. tokio::select! picks randomly when both the send and cancel branches are ready, so under CI load the factory-backed cancellation test intermittently found only one of two records. Record the attempt as Cancelled on the post-open path, matching the mid-send and mid-stream cancellation paths, and add a regression test that pauses polling until the response head is buffered.
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.
Summary
Fixes the intermittent Linux CI failure of
agentic::execution::round_executor::tests::factory_backed_cancellation_releases_stream_without_cooldown_and_observes_revocation(timeout waiting for 2 ledger records; seen on #3668 and #3492). The cause is a production bug, not test isolation: a cancelled model attempt could skip its token-usage ledger record.Fixes #
Type and Areas
Type: bug fix (CI flake root cause)
Areas: Rust core (
openbitfun-coreround executor / token usage ledger)Motivation / Impact
Root cause.
RoundExecutorawaits the provider send intokio::select! { cancel, send_future }. Cancellation in that select records aCancelledattempt, and so does cancellation during stream processing. But if both branches are ready at the same poll,select!picks one at random. When it pickssend_future, execution reaches the "check cancellation before stream processing" branch, which returnedCancelledwithout callingrecord_attempt_usage. The provider had already accepted that request, so it was billable, and its ledger record was lost.The test cancels once the fixture server reports
opened_streams == 1, which is set after the response head is written. On a fast machine the round usually reaches stream processing first. Under the load of the 3147-test CI run, the round task often isn't polled between the head arriving and the cancel. That leaves a 50/50select!race, and losing it leaves one record instead of two. The 5 s wait then times out. Raising the timeout would not help: the record is never written.I ruled out the other suspects. The ledger writer is a per-call
tokio::spawnon the test's own runtime, with no global queue, debounce, or batch timer. The writes are serialized by per-path locks andflock. The test uses its ownTokenUsageServicein a temp dir.Fix. The post-open cancellation branch now records the attempt as
Cancelledwith unknown usage, the same as the mid-send and mid-stream branches. Users now get a ledger entry for every cancelled request that reached the provider.Verification
cancellation_racing_stream_open_still_records_the_sent_attempt: it delays the response head, pauses polling until the head is buffered, cancels, then resumes (16 iterations). Without the fix it fails (instrumentation confirmed it took the post-open path and left no record). With the fix it passes.cargo test --locked -p openbitfun-core --features product-full --lib round_executor: 68 passedcargo test --locked -p openbitfun-core --features product-full --lib token_usage: 12 passedcargo test --locked -p openbitfun-core --features product-full --lib: 3141 passed, 0 failed (macOS)pnpm run fmt:rsRemote scenarios: not exercised. The change is local to the core round executor and is the same for every host.
Reviewer Notes
No persisted-shape change. The record uses the existing
ModelRequestOutcome::Cancelledattempt shape already written by the other cancellation paths.Checklist