Repository navigation
std/async: firstof exceptions, strand scopes, canceled receives (alternative to #933, #935; includes #934) - #955
Draft
TimWhiting wants to merge 3 commits into
Draft
TimWhiting wants to merge 3 commits into
TimWhiting wants to merge 3 commits into
Conversation
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! |
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
force-pushed
the
fix/std-async-combined
branch
from
October 27, 2026 12:59
7fc551a to
21dba4e
Compare
This branch has not been deployed
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.
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.firstofis decided by its first strand (alternative to #933)An exception in a
firstofstrand was lost (atimeoutreturnedNothing), and if every strand raised,firstofaborted with "impossible: empty result list". The result started asCompleted([]), which an exception never replaced, and a raising strand did not cancel the others.Here the result starts as a new
Pendingvariant, 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.interleavedkeeps its rules. #933 reusesCanceledas the initial state instead.Test:
test/async/firstof-first.kkInterleaved 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
timeoutin such a strand canceled the caller's scope.Test:
test/async/interleave-scope.kkA canceled channel
receiveleaves the channel (alternative to #935)A canceled
receivestayed 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
liveflag (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-noexnbecomes public, asreceiveneeds a dispose function withoutexn.Test:
test/async/channel-cancel.kk🤖 Generated with Claude Code