Skip to content

Cancellation: a call to a sync fn that may suspend unwinds its cancelled caller like an await does (#916); await inside a defer no longer recurses - #1987

Merged
ehartford merged 3 commits into
mainfrom
fix-916
Oct 3, 2026
Merged

ehartford merged 3 commits into
mainfrom
fix-916

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #916. Debug allocator + lldb: let _ = tasks |> await_all dropped the Result[Vec[i32], str] that a cancelled await_all never returned (invalid free, __drop_enum). MIR went straight from the call to the drop. Now Sema settles may-suspend once per declaration (generic specializations use the template's answer; pipeline stages and piped values counted), and MIR gives every call Sema says may suspend the await's cancelled-return check, on the existing emit_cancelled_return path, via terminate_with_span with Sema's resolved callee. Each defer body runs with only the defers registered before it pending (fixes an existing with check stack overflow on .await inside a defer). spec_ss14_11_await_combinator_cancel_joins.w passes for the right reason; its known-issue and linux-aarch64 skip lines are removed. Fixtures da_916_cancel_unwinds_through_sync_callee.w and behav_await_inside_defer.w red→green; 86 async/cancel fixtures ok; debug-alloc lane 146/146; drop-audit 254/0; gate green. Follow-ups filed: indirect calls (closure/dyn/fn value) and cancellation inside a defer during break/continue unwind. Bottom of the #1960 stack.

Eric Hartford added 3 commits October 2, 2026 20:07
…on (#916)

An async caller cancelled while suspended inside a sync callee that awaits
(`tasks |> await_all`, `eat(tasks)`) resumed past the call and consumed the
callee's never-written return slot: the debug allocator reported
"invalid free: pointer is not an allocated payload start" from
`__drop_enum_*` at spec_ss14_11_await_combinator_cancel_joins.w:33
(await_all_result_parent), and a Drop result ran its destructor on garbage.
The cancelled-return protocol (§14.7) lived only at await sites.

- Sema settles which fn declarations may suspend once checking is done
  (settle_may_suspend_facts: a least fixpoint over every body, keyed by
  declaration node, so cycles cannot cut it short and a specialization
  shares its template's answer). The walker now sees a pipeline stage's
  callee (it walked d1/d2 of NK_PIPELINE and missed both the piped value
  and the resolved stage).
- MirLower: a TK_CALL whose Sema-resolved callee (the call contract's
  symbol, else the fn constant) is a sync fn that may suspend continues
  into a FIBER_WAIT_CANCELLED check; set, the caller takes the cancelled
  return (set flag, defers, drops, return) before the result is read,
  exactly as the await it sits under. Async and gen callees spawn/build
  and never suspend the caller.
- emit_defers_for_range runs each defer body with only the defers
  registered before it pending, so a cancellation unwind inside a defer
  runs those and not itself: an `.await` inside a defer no longer
  recurses the lowering into a stack overflow (`with check` SIGSEGV).
- struct_field_type is a reader (`fn`, not `mut fn`); the seed flagged
  update_string_fields_after_aggregate's `count` view once lowering's
  call graph grew.

Tests: da_916_cancel_unwinds_through_sync_callee (direct + generic
pipeline callee; red before: resumed=2 batch_drops=1), and the #916
known-issue/skip-on directives are gone from the ss14_11 cancel test,
which now passes for the right reason on every host.
@ehartford
ehartford added this pull request to stack #1988 October 3, 2026 03:21
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on stack top 1033f91 (seed-driven, with run tools/battery.w; stack #1988 = #1987 → #1960, on main 9307016): gate rc=0 wall=616s, battery-checks rc=0 wall=635s — fixpoint, drop and move audits, every corpus. The earlier #1960 red (spec_ss14_11_await_combinator_cancel_joins passing under its #916 known-issue) is resolved by #1987 fixing #916 for real; the known-issue and aarch64 skip lines are gone. Merged by the coordinator per Eric's standing direction (2026-10-02: merge stacked PRs without waiting).

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.

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

1 participant