Skip to content

Cancellation protocol gap: async caller of a sync-but-suspending fn uses the cancelled callee's garbage return (invalid free) #916

Closed

Description

@ehartford

Minimal repro (30 lines, no combinators)

async fn tick() -> i32: 1

fn eat[T](tasks: impl IntoIter[Task[T]]) -> Vec[T]:   // SYNC fn that suspends via awaits inside
    let pending: Vec[Task[T]] = Vec.new()
    for task in tasks.into_iter(): pending.push(task)
    let values: Vec[T] = Vec.new()
    while pending.len() > 0:
        values.push(pending.remove(0).await)
    values

async fn parent -> i32:
    let tasks: Vec[Task[i32]] = Vec.new()
    tasks.push(wait_forever(1))
    tasks.push(wait_forever(2))
    let _ = tasks |> eat        // parent suspends INSIDE eat
    0
// main: spawn parent, drive, parent.cancel(), parent.join_cleanup()

invalid free: pointer is not an allocated payload start from __drop_struct_94 (= Vec[i32]).

Root cause (MIR-verified)

The cancelled-return protocol lives only at AWAIT sites (lower_single_await: FIBER_IS_CANCELLED / FIBER_WAS_CANCELLED_RETURN checks, unwind bb, FIBER_SET_CANCELLED_RETURN before TK_RETURN — src/MirLower.w:7842-7948). When the suspension happens inside a SYNC callee, that callee's await unwinds it correctly and returns with cancelled_return set — but the ASYNC CALLER has no post-call check: parent's MIR is call eat(move _1) -> _8; _7 = move _8; drop(_7) unconditionally. _8 was never written (the callee unwound), so the caller moves and drops stack garbage — the invalid free.

Pre-existing and older than the current seed (the seed cannot even compile the old combinator bodies to reach this path); it was one component of the red pinned on ss14_11's cancel test.

Fix shape

After any call whose callee MAY SUSPEND (sema's may-suspend effect — the §14 arena-scope machinery already tracks it), an async/generator caller must branch on FIBER_CANCELLED_RETURN: skip result consumption and unwind (defers + drops + set-cancelled-return + return), mirroring lower_single_await's unwind bb. Emit only for may-suspend callees so ordinary calls pay nothing. Acceptance: spec_ss14_11_await_combinator_cancel_joins (currently re-pinned to this issue) goes green; a da_ fixture pins the caller-side protocol with counted drops.

Found finishing D33/#724 — the combinators' own double-own is fixed; this is the residual on the cancel path only.

Activity

  1. ehartford commented on Sep 7, 2026

    @ehartford
    ContributorAuthor

    Nondeterminism evidence, 2026-09-07: test/spec/spec_ss14_11_await_combinator_cancel_joins.w carries known-issue: #916 and stays red on every selfhost-linux-aarch64 run on main (the harness prints "red as expected"), but in the nightly release workflow's linux-aarch64 job on branch windows-release-uat (run 34141543229, same tree as its green PR lane, seed v0.15.2.0) it passed, so the harness failed the file: "known-issue #916 expected this test to stay red, but it passed". The cancel/join outcome this fixture pins is therefore timing-dependent on the arm64 hosted runner, not stably wrong. Nothing in this run's tree touched async or fibers. The nightly jobs now upload out/test-graph/** on failure so the next occurrence carries the file's stdout/stderr.

  2. ehartford commented on Sep 16, 2026

    @ehartford
    ContributorAuthor

    Platform note from the release work (2026-09-16): on linux-aarch64 this issue does not reproduce. Built main (0a0ff13) natively in a linux/arm64 container (ubuntu 24.04, the lane's SDK/seed/runtime), native-spec-tests ran 210 files and the only failure was test/spec/spec_ss14_11_await_combinator_cancel_joins.w — and it failed the known-issue gate, not the test:

    error: known-issue #916 async caller of a cancelled sync-but-suspending callee consumes its garbage return
           expected this test to stay red, but it passed; if the issue is fixed, remove the known-issue directive
    

    So the garbage-return consumption is arch-dependent (it still reproduces on darwin-aarch64 and linux-x86_64 per the passing known-issue gate there). The release PR (#1155) scopes the test with skip-on: linux-aarch64 so that leg's battery can be green; when #916 is fixed, drop both directives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions