Repository navigation
rt/MIR: select await no longer hangs on a loser parked in recv() — cancellation cuts the channel wait short and unwinds (#1293) - #1324
Merged
Conversation
Contributor
Author
|
Gates (driven by the pinned seed, |
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
force-pushed
the
fix-1293-select-recv-hang
branch
from
September 22, 2026 17:34
869172b to
b332c65
Compare
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1293.
Root cause (observed in lldb, not inferred)
Repro from the issue built to a binary and attached at the hang:
The main thread runs
with_fiber_cleanup_awaiton the loser forever. The loser fiber sits inwith_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..awaithas both halves of cancellation —with_fiber_awaitreturns onwith_runtime_current_cancel_requested()and the lowering'sIS_CANCELLEDcheck unwinds (lower_single_await) — but the channel waits, which Sema (method_may_suspend_current_fiber) andMirSuspendCheckalready 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)
Sender.send/closeandReceiver.recv/closeas channel intrinsics (classify_intrinsic). They were recognised only by symbol name in CodegenDispatch, so the MIR never carriedCHAN_SEND/CHAN_RECVfor 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.with_channel_recv/with_channel_senddo not park a fiber whose cancellation was requested — they set the fiber'scancelled_return, and return (recv → None consuming nothing; send destroys the never-enqueued value with the channel'sdrop_fn). The channel is untouched. MIR: after everyCHAN_SEND/CHAN_RECV(method call andfor msg in rxheader) emitFIBER_WAIT_CANCELLED→ 0: continue; else the §14.7 cancelled return (factored out oflower_single_awaitasemit_cancelled_return).Why the runtime's verdict and not the raw cancel flag
A first cut unwound on
IS_CANCELLEDafter every send/recv. That brokeexamples/async-auction.wphase 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, andspec_ss14_8'stest_scope_exit_cancels_unawaited_taskpins 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.trackwill complete (or be cancelled) beforeasync scopereturns" 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.awaitinside printsdone=0on 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
now/after select, exits 0 (stage1 and stage2).behav_async_select_cancels_parked_recv(loser's defer runs, code afterrecvnever 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/whilerecv shapes, and&Receiverasync recv.run --debug-alloc: 0 leaks on both tests andtest/debug_alloc/da_channel_task_fiber.w.behav_channel_*,behav_*select*,behav_async_scope*,spec_ss14_*,err_channel_*: 60 pass;spec_ss14_11_await_combinator_cancel_joinsstays 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.wrun;examples/nebulachecks.:debug-alloc-testsand:drop-auditresults appended below when they finish.Found on the way (not this PR)
analyze audit:allon anyuse std.channelprogram reports 4 violations (Drop-impl extern marshalling facts) identical before/after — noted on analyze audit:all reports two pre-existing violations on any program that uses std.box (with_free marshalling fact, sig receiver requirement) #1300.check --validate-*hangs at 7 GB on any program (main's own stage1 too) — check --validate-all on a bootstrap stage1 parks at 7 GB in the drop-state driver; the release binary answers in 0.1 s #1282/bootstrap stage1check --validate-alltakes 15+ min at 7 GB RSS on a 20-line file (release binary: 0.07 s) #1315; validation here was done on stage2.