Skip to content

Vec/iterator intrinsics (map, fold, ...) over a closure that may suspend ignore a cancellation unwind inside the closure #2019

Closed

Description

@ehartford

Found on wave-cancel (#1985 / #2011). Spec: §14.3 INVARIANT 5 (may_suspend is callable type information), §14.7 (cancellation unwinds).

async fn double(n: i32) -> i32: n * 2
fn numbers -> Vec[i32]: [1, 2, 3]
fn f:
    let doubled = numbers().map(n => double(n).await)   // test/spec/spec_ss14_13_await_inside_iterators.w

Sema's settled call-site fact (#1985, call_site_may_suspend) records that this map call reaches a may-suspend closure. But the call lowers as a codegen intrinsic (VEC_MAP; likewise VEC_FOLD and the ITER_* adapters), and MirBuilder.call_may_cancel_return ignores every intrinsic except GENERIC_CALL and DYN_CALL. So when the fiber is cancelled inside the closure, the closure leaves by its cancellation unwind with no value. The intrinsic's loop calls it for the next element and builds the result from never-written slots, and the caller then resumes past the call. This is the #1985 class through intrinsic iteration.

Expected: the intrinsic loop stops when an invocation of the closure leaves by cancellation (FIBER_WAIT_CANCELLED after each invocation), releases what it built, and the call site takes the cancelled-return path. No Sema change is needed; the fact is already recorded.

Not fixed on wave-cancel: there the intrinsic tag was being read off the wrong call after #1985's check insertion. That is fixed, and these calls are back to main's behavior (no check).

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    wave-cancel: the eager Vec intrinsics (map, filter, fold) are fixed. MIR reads Sema's call_site_may_suspend for them, marks the call, and drops the partial result on the unwind edge; codegen leaves the loop at the invocation that unwound. Fixture: test/debug_alloc/da_2019_cancel_inside_intrinsic_closure.w.

    Still open: a lazy adapter chain, e.g. let items: Vec[Item] = xs.iter().map(n => forever_item(n).await).collect() (the same fixture with that line), still reports DOUBLE FREE ... second_drop=drop#struct. The closure runs inside the terminal's *_NEXT emitters, and Sema's site fact is recorded on the map(..) node, not on the collect() that drives it. Needs: (1) Sema extends the site fact to a terminal whose receiver chain hands over a may-suspend callable; (2) the NEXT emitters stop (end the iteration) after an invocation that unwound, under a marked terminal; (3) the same for MIR-level for x in it.map(f) loops.

  2. ehartford commented on Oct 4, 2026

    @ehartford
    ContributorAuthor

    Fixed on main in 8cc883a (stack #2072, ten PRs; seed-driven battery GREEN on 92add22, tree identical to main).

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