Repository navigation
Add Coro.timeoutOr - #260
Merged
Merged
Conversation
A catch of TimeoutException around Coro.timeout also catches the expiry of an enclosing timeout, because the cancellation cause is shared with the children. timeoutOr returns the result of a fallback function on its own expiry only and rethrows any other TimeoutException, so code that reacts to an expiry no longer handles the caller's cancellation as its own. See #259 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXEAxiai5UFR4qnZFxg
Member
Author
|
It took me forever to understand the distinction here. In Kotlin, |
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.
Closes #259.
A
catch (e:TimeoutException)aroundCoro.timeoutalso catches the expiry of an enclosingtimeout, because the cancellation cause is shared with the children. Code that reacts to the catch as its own expiry then carries on inside a cancelled task; in a retry loop it spins.This adds
Coro.timeoutOr(ms, fallback, lambda), which answers "is this expiry mine?" inside the library:fallback(). A timeout of 0 returns it without runninglambda; a negative timeout throwsArgumentException, as intimeout.fallbackis not called and that limit'sTimeoutExceptionis rethrown. The check compares the caught exception with the instance the call created itself.fallbackis a plain function and may throw, which covers converting an expiry into another exception.fallbackcomes beforelambdaso that the long closure stays the last argument.timeoutbehaves as before. Its doc comment now says that it may throw theTimeoutExceptionof an enclosing limit. Both functions share a non-coroutine helper, so the stack shape oftimeoutis unchanged (the callstack tests pass).Kotlin has the same behaviour in
withTimeoutand solves it inwithTimeoutOrNullwith the same kind of owner check. A fallback function was chosen over a null result because it stays unambiguous when the body can return null.Tests
Ten tests in
tests/src/structured/TestTimeout.hx: result, fallback, zero, negative, body throws, fallback throws, both nesting directions, and the loop from the issue. With the identity check disabled, the two enclosing-timeout tests and the loop test fail.Run locally:
-debug -D coro.stack-traces,HXCORO_DISPATCHER=threadpool) and callstack testsNot run locally: jvm, hl, cpp, php, neko, lua, the thread tests and the wiki tests. The wiki has no entry for
timeoutOryet.Generated by Claude Code