Skip to content

tests: the image-cancel test cancels once the first encode has begun - #180

Merged
IsuraManchanayake merged 1 commit into
rc-0.2.1from
fix/image-cancel-test-off-the-clock
Nov 23, 2026
Merged

IsuraManchanayake merged 1 commit into
rc-0.2.1from
fix/image-cancel-test-off-the-clock

Conversation

@IsuraManchanayake

Copy link
Copy Markdown
Collaborator

Into rc-0.2.1 (#179).

The windows-2025 job on rc-0.2.0 at 441c3ba (run 44578431542) failed on one test, a_request_cancelled_while_its_images_encode_ends_and_frees_its_slot, at media_admission.rs:189: assert_eq!(out.tokens_generated, 0) saw left: 4, right: 0. The test started a three-image request on a mock that takes 100 ms an image, slept 50 ms, cancelled the session through d.cancel_registry().cancel(s) and expected no tokens.

Nothing tied the cancel to the request. Two orders give 4 tokens, and both were reproduced by hand with the sleep changed and the state printed at the cancel:

  • The cancel comes before the request has registered (no sleep): tokens=4 registered_at_cancel=false encodes_begun_at_cancel=0 cancel_left_in_set=false. A generation drops any cancel held for its session as it registers (cancels.remove(&session) in generate_streaming_inner, "a cancel aimed at the previous generation … must not stop this one"), so the cancel is gone and the request runs to the end.
  • The cancel comes after the request has finished (600 ms): tokens=4 gen_finished_at_cancel=true cancel_left_in_set=true.

The Windows log has only the assertion, so it does not say which. A starved run does: under taskpolicy -c background on a Mac, in 40 runs of 40 the old test's 50 ms sleep took 115–150 ms and the request had registered but had not begun its first encode. The request falls behind the test thread, not ahead of it, which is the first order one step further; the second needs the test thread to stall past three 100 ms encodes while the request runs on. The dropped cancel itself did not reproduce on the Mac by starvation (0 of 40).

This is the test's fault, not the daemon's. The daemon's own cancels from outside a generation (Request::Cancel in api.rs, Link::abandon in keepalive.rs) take a session only when it is in the active set, and the rest cancel from inside the generation's event callback. The test called CancelSet::cancel directly, which has no such check.

The same 40 starved runs show a second fault: with no encode begun at the cancel (encodes_total=0), the test passed having cancelled a request that held no image, so "its encoded images were let go" checked nothing.

What changes

  • The test passes the mock an encode log (EngineConfig::media_encode_log) and cancels when the log holds the first encode, instead of after a sleep. An encode is in the log only once the request has registered and is admitting, so the cancel is not dropped, and the request holds an image when it lands.
  • The cancel goes in under the log's lock. The mock notes each encode under that lock, so no further encode begins between reading the log and the cancel.
  • A new assertion: at most one encode began after the cancel (the one already asked for when it went in). The request stopped encoding rather than encoding all three and ending.
  • What it proved before is kept: 0 tokens, and a three-image request after it fits under max_live_media: 3.

One wait is left on the clock: the test polls the log every 1 ms, and a test thread stalled for more than 200 ms between two polls, while the request's other two encodes run, could still cancel too late. The failure message names how many encodes had begun.

The CHANGELOG is left for the release commit.

Checks (macOS arm64, 18 cores)

  • cargo clippy -p superfluid --all-targets -- -D warnings: clean, on main at 3240988.
  • cargo test -p superfluid --test media_admission, 40 runs on 441c3ba: 40 passed, 5 tests each, 4.5 s a run. 10 more on main at 3240988: 10 passed.
  • taskpolicy -c background cargo test -p superfluid --test media_admission, 40 runs on 441c3ba: 40 passed, 16.6–19.4 s a run.
  • By hand: the old test with the cancel before registration fails with the CI failure's left: 4, right: 0; the new test with the request started 300 ms late passes.

Not run: Windows, and the whole cargo test. This pull request's own windows-2025 job is the check.

a_request_cancelled_while_its_images_encode_ends_and_frees_its_slot slept
50 ms after starting its three-image request and then cancelled the session
through the cancel registry. Nothing tied that moment to the request. A
generation drops any cancel already held for its session as it registers
(one meant for the session's previous generation), so on a runner where the
request's thread had not registered by then the cancel was lost and the
request ran to its 4 tokens: the windows-2025 failure on rc-0.2.0 at
441c3ba. On a starved Mac the same sleep ended before the first encode in
40 runs of 40, where the test passed having cancelled a request that held
no image.

The test now reads the mock's encode log and cancels when the first encode
is in it, holding the log's lock while the cancel goes in so no further
encode begins meanwhile. It also checks that the request stopped encoding.
@IsuraManchanayake
IsuraManchanayake merged commit dd9bae5 into rc-0.2.1 Nov 23, 2026
7 checks passed
@IsuraManchanayake
IsuraManchanayake deleted the fix/image-cancel-test-off-the-clock branch November 23, 2026 01:36
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