Skip to content

Add Coro.timeoutOr - #260

Merged
Simn merged 1 commit into
masterfrom
claude/quirky-noether-ftuv09
Nov 22, 2026
Merged

Simn merged 1 commit into
masterfrom
claude/quirky-noether-ftuv09

Conversation

@Simn

@Simn Simn commented Nov 22, 2026

Copy link
Copy Markdown
Member

Closes #259.

A catch (e:TimeoutException) around Coro.timeout also catches the expiry of an enclosing timeout, 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:

final reply = timeoutOr(100, () -> defaultReply, _ -> {
	// long body
});
  • On its own expiry it returns fallback(). A timeout of 0 returns it without running lambda; a negative timeout throws ArgumentException, as in timeout.
  • If an enclosing time limit expires, fallback is not called and that limit's TimeoutException is rethrown. The check compares the caught exception with the instance the call created itself.
  • fallback is a plain function and may throw, which covers converting an expiry into another exception.
  • fallback comes before lambda so that the long closure stays the last argument.

timeout behaves as before. Its doc comment now says that it may throw the TimeoutException of an enclosing limit. Both functions share a non-coroutine helper, so the stack shape of timeout is unchanged (the callstack tests pass).

Kotlin has the same behaviour in withTimeout and solves it in withTimeoutOrNull with 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:

  • eval: main tests (release, -debug -D coro.stack-traces, HXCORO_DISPATCHER=threadpool) and callstack tests
  • js and python: main tests

Not run locally: jvm, hl, cpp, php, neko, lua, the thread tests and the wiki tests. The wiki has no entry for timeoutOr yet.


Generated by Claude Code

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
@Simn

Simn commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

It took me forever to understand the distinction here. In Kotlin, withTimeoutOrNull handles the exception itself, and that's what allows it to verify identity. We agreed that using null could be ambiguous though because a function might legitimately return it, which would then be indistinguishable from a timeout. The cost is a closure allocation, but there's always something...

@Simn
Simn merged commit 01cb229 into master Nov 22, 2026
30 checks passed
@Simn
Simn deleted the claude/quirky-noether-ftuv09 branch November 22, 2026 17:40
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.

A catch of TimeoutException around Coro.timeout also catches an enclosing timeout's expiry

2 participants