Skip to content

rt/MIR: select await no longer hangs on a loser parked in recv() — cancellation cuts the channel wait short and unwinds (#1293) - #1324

Merged
ehartford merged 2 commits into
mainfrom
fix-1293-select-recv-hang
Sep 22, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-1293-select-recv-hang

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1293.

Root cause (observed in lldb, not inferred)

Repro from the issue built to a binary and attached at the hang:

thread #1: with_fiber_cleanup_await  fiber_runtime.w:246   ← the select's loser join
           main                     repro.w

The main thread runs with_fiber_cleanup_await on the loser forever. The loser fiber sits in with_channel_recv's wait loop (rt/channel_runtime.w: while count == 0: … channel_block_until_progress()), which yields on every step and never reads the fiber's cancel request. .await has both halves of cancellation — with_fiber_await returns on with_runtime_current_cancel_requested() and the lowering's IS_CANCELLED check unwinds (lower_single_await) — but the channel waits, which Sema (method_may_suspend_current_fiber) and MirSuspendCheck already class as current-fiber suspension points, had neither half. Spec §14.7: "At that await point, instead of suspending, the fiber begins unwinding."

Change (two commits)

  1. MIR classifies Sender.send/close and Receiver.recv/close as channel intrinsics (classify_intrinsic). They were recognised only by symbol name in CodegenDispatch, so the MIR never carried CHAN_SEND/CHAN_RECV for user code and a &Sender[T]/&Receiver[T] parameter crashed the compiler (BUG: user generic call lacks a concrete contract — reproduces on the pinned seed). Test: behav_channel_borrowed_endpoint_params.
  2. Cancellation cuts a parked channel wait short; the fiber unwinds there. Runtime: with_channel_recv/with_channel_send do not park a fiber whose cancellation was requested — they set the fiber's cancelled_return, and return (recv → None consuming nothing; send destroys the never-enqueued value with the channel's drop_fn). The channel is untouched. MIR: after every CHAN_SEND/CHAN_RECV (method call and for msg in rx header) emit FIBER_WAIT_CANCELLED → 0: continue; else the §14.7 cancelled return (factored out of lower_single_await as emit_cancelled_return).

Why the runtime's verdict and not the raw cancel flag

A first cut unwound on IS_CANCELLED after every send/recv. That broke examples/async-auction.w phase 1 (3 bids collected instead of 9; bt: with_fiber_set_cancelled_return ← bidder at async-auction.w:49): with_scope_await_all (rt/rt_core.w:4200-4204) cancels every tracked fiber before joining it on the normal scope-exit path, and spec_ss14_8's test_scope_exit_cancels_unawaited_task pins that. So tracked tasks whose sends never need to wait must keep completing. A wait that completes without parking is therefore not a cancellation point; one the runtime cut short is.

Open spec question for Eric (not decided here): §14.9 says "all tasks tracked via s.track will complete (or be cancelled) before async scope returns" and "tracked tasks that haven't been awaited are cancelled and joined" at scope exit. The runtime cancels then joins, which means an un-awaited tracked task that reaches any .await (or now a parked send/recv) after the scope body ends is unwound rather than run to completion — s.track(worker(3)) with a real .await inside prints done=0 on main today. If that is the intent, fine; if scope exit should join and only cancel on panic/early-?, that is a separate runtime change.

Evidence

  • Repro prints now / after select, exits 0 (stage1 and stage2).
  • New tests: behav_async_select_cancels_parked_recv (loser's defer runs, code after recv never runs, a later send/recv delivers), behav_async_select_cancels_parked_send (loser parked on a full bounded channel; the unsent value never lands).
  • check --validate-all: ok (stage2) for both tests, the repro, for/while recv shapes, and &Receiver async recv.
  • run --debug-alloc: 0 leaks on both tests and test/debug_alloc/da_channel_task_fiber.w.
  • Lane behav_channel_*, behav_*select*, behav_async_scope*, spec_ss14_*, err_channel_*: 60 pass; spec_ss14_11_await_combinator_cancel_joins stays Cancellation protocol gap: async caller of a sync-but-suspending fn uses the cancelled callee's garbage return (invalid free) #916 red-as-expected (same on main and the seed).
  • examples/async-auction.w, examples/channels/pipeline.w run; examples/nebula checks.
  • :debug-alloc-tests and :drop-audit results appended below when they finish.

Found on the way (not this PR)

@ehartford

Copy link
Copy Markdown
Contributor Author

Gates (driven by the pinned seed, WITH=$PWD/src/main): src/main build :debug-alloc-tests rc=0 (full chain seed→stage1→stage2→link, lane 28.8 s, wall 414 s); src/main build :drop-audit rc=0 (no regressions vs baseline, 88.7 s).

Eric Hartford added 2 commits September 22, 2026 13:20
…nel intrinsics in MirLower, not by name in codegen

`classify_intrinsic` had no Sender/Receiver entries, so `tx.send(v)` and
`rx.recv()` lowered as plain `call const fn recv(...)` terminators that
CodegenDispatch recognised by symbol name (`classify_generic_call_intrinsic`,
the `gc_tail_name == "recv"` path). Two consequences:

- the MIR never carried CHAN_SEND/CHAN_RECV for user code, so
  MirSuspendCheck's channel cases and lower_intrinsic_call's
  `channel_endpoint_method` handling were dead for every user program;
- a `&Sender[T]` / `&Receiver[T]` parameter fell into the generic-call
  contract path and crashed the compiler:
  `BUG: user generic call lacks a concrete contract: body=...(take) call=0`
  (`mir_channel_endpoint_kind` only accepts a bare TY_GENERIC_INST).

Classifying the endpoint methods at MIR level routes them through
lower_intrinsic_call like every other builtin. Verified on stage1 and
stage2: the four shapes (sync/async recv through `&Receiver`, send through
`&Sender`, owned receiver) all compile and run; the channel/select/§14 test
lanes are unchanged.

New test: test/behavior/behav_channel_borrowed_endpoint_params.w.
…nwinds there (#1293)

`select await` hung when the losing branch was parked in `recv()` on an empty
channel. Observed in lldb on the issue's repro: the main thread spins in
`with_fiber_cleanup_await` (rt/fiber_runtime.w:246, the select's loser join)
while the loser sits in `with_channel_recv`'s wait loop
(rt/channel_runtime.w: `while count == 0: ... channel_block_until_progress()`),
which yields forever and never looks at the fiber's cancel request. `.await`
has both halves — `with_fiber_await` returns on
`with_runtime_current_cancel_requested()` and the lowering's IS_CANCELLED
check unwinds — but the channel waits, which Sema and MirSuspendCheck already
class as current-fiber suspension points, had neither.

Runtime: `with_channel_recv` / `with_channel_send` no longer park a fiber
whose cancellation was requested. The wait is cut short, the fiber's own
`cancelled_return` is set (`with_runtime_current_set_cancelled_return`), and
the call returns: recv returns -1 (None) having consumed nothing; send
destroys the never-enqueued value with the channel's drop_fn. The channel is
untouched, so a later send/recv works. New accessor
`with_runtime_current_cancelled_return` (darwin, windows, stubs) behind
`with_fiber_wait_cancelled`.

MIR: after every CHAN_SEND/CHAN_RECV (lower_intrinsic_call and the
`for msg in rx` header) the lowering emits FIBER_WAIT_CANCELLED → 0: continue;
else the §14.7 cancelled return (SET_CANCELLED_RETURN, pending resets, defers,
drops, return), factored out of lower_single_await as emit_cancelled_return.

Why the runtime's verdict and not the raw cancel flag: `with_scope_await_all`
cancels every tracked fiber before joining it on the normal scope-exit path
(rt/rt_core.w:4200-4204, pinned by spec_ss14_8's
test_scope_exit_cancels_unawaited_task), so tracked tasks that never park —
examples/async-auction.w's bidders, whose sends always have room — must keep
completing; with the raw flag they unwound at their first send and phase 1
collected 3 bids instead of 9 (bt: with_fiber_set_cancelled_return ← bidder
at async-auction.w:49). A wait that completes without parking is therefore
not a cancellation point; one the runtime cuts short is. Whether normal
`async scope` exit should join rather than cancel is a spec question raised
on the PR, not decided here.

Tests: behav_async_select_cancels_parked_recv (loser parked in recv unwinds,
its defer runs, the body after recv never runs, the channel delivers later
sends), behav_async_select_cancels_parked_send (loser parked on a full
bounded channel; the unsent value never lands). validate-all ok on stage2 for
both, the repro, and the for/while recv shapes; debug allocator: 0 leaks;
channel/select/scope/§14 lanes green (spec_ss14_11 stays #916 red-as-expected).
@ehartford
ehartford force-pushed the fix-1293-select-recv-hang branch from 869172b to b332c65 Compare September 22, 2026 17:34
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, rebased on main 980474c): build, :fixpoint, :drop-audit rc=0; :test red only in main's own known reds — behav_distinct_compat (#1319 gap, fix forward in progress), da_match_place_observes (#1322), fizzbuzz (#1325). None in this change's paths. Mergeable on its own.

@ehartford
ehartford merged commit da8d0c7 into main Sep 22, 2026
@ehartford
ehartford deleted the fix-1293-select-recv-hang branch September 22, 2026 18:57
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.

select await deadlocks when a losing branch is blocked in recv(): the ready branch is never taken

1 participant