Skip to content

std/async: firstof exceptions, strand scopes, canceled receives (alternative to #933, #935; includes #934) - #955

Draft
TimWhiting wants to merge 3 commits into
koka-lang:devfrom
TimWhiting:fix/std-async-combined
Draft

TimWhiting wants to merge 3 commits into
koka-lang:devfrom
TimWhiting:fix/std-async-combined

Conversation

@TimWhiting

Copy link
Copy Markdown
Collaborator

Three fixes to std/async, one commit each, each with a test. They cover the same three bugs as #933, #934 and #935; two of them take a different approach, for comparison.

firstof is decided by its first strand (alternative to #933)

An exception in a firstof strand was lost (a timeout returned Nothing), and if every strand raised, firstof aborted with "impossible: empty result list". The result started as Completed([]), which an exception never replaced, and a raising strand did not cancel the others.

Here the result starts as a new Pending variant, and the first strand to finish decides, with its value or its exception, and cancels the others; a canceled strand decides only if no other finishes. interleaved keeps its rules. #933 reuses Canceled as the initial state instead.

Test: test/async/firstof-first.kk

Interleaved strands resume within their cancelation scope (the same change as #934)

A strand that has awaited is resumed by the interleaving's loop, which ran outside the interleaving's cancelation scope, so a timeout in such a strand canceled the caller's scope.

Test: test/async/interleave-scope.kk

A canceled channel receive leaves the channel (alternative to #935)

A canceled receive stayed in the channel's list of listeners, so the next value went to it: the value was lost, or an ended strand was resumed a second time.

Here each listener carries a live flag (a ref, which gives it an identity); its dispose function clears the flag and drops the listeners that are no longer live. #935 threads an integer id through instead. await-noexn becomes public, as receive needs a dispose function without exn.

Test: test/async/channel-cancel.kk

🤖 Generated with Claude Code

@TimWhiting

Copy link
Copy Markdown
Collaborator Author

This is just another reference point. I ran into this at the same time or just prior to @pigoz, and I used AI to review both my fixes and his approach, and come up with something maybe a bit better than either individually. Still worth understanding the explanations in his PR descriptions and his tests thoroughly, and determining the right fixes. Thanks for your contributions @pigoz!

Tim Whiting and others added 3 commits October 27, 2026 06:43
An exception in a `firstof` strand was lost (a `timeout` returned `Nothing`),
and if every strand raised, `firstof` aborted with "impossible: empty result
list": the result started as `Completed([])`, which an exception never
replaced, and a raising strand did not cancel the others.

A `firstof` now starts as `Pending`, and the first strand to finish decides,
with its value or its exception, and cancels the others; a canceled strand
decides only if no other finishes. `interleaved` keeps its rules.

Test: test/async/firstof-first.kk

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A strand that has awaited is resumed by the interleaving's loop, which ran
outside the interleaving's cancelation scope. So a `timeout` in such a strand
canceled the caller's scope: later timeouts there aborted, and at the top
level every outstanding await was canceled. The loop now resumes strands in
the scope, and in the parent scope once it is canceled.

Test: test/async/interleave-scope.kk
From koka-lang#934.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A canceled receive stayed in the channel's list of listeners (its dispose
function did nothing), so the next value went to it: the value was lost, or,
if the interleaving that ran the receive was still running, the ended strand
was resumed a second time. Each listener now has a live flag; its dispose
function clears the flag and removes the listeners that are no longer live.
await-noexn becomes public, as receive needs a dispose function without exn.

Test: test/async/channel-cancel.kk

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TimWhiting
TimWhiting force-pushed the fix/std-async-combined branch from 7fc551a to 21dba4e Compare October 27, 2026 12:59

This branch has not been deployed

No deployments
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.

2 participants