Repository navigation
[Async stack 2/6] asyncio composition and durable timers - #358
andystaples wants to merge 4 commits into
Conversation
1b5625c to
1e5bf2f
Compare
0cfb7b3 to
44eccfc
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Valid wrapped synchronous partials fail registration due to incorrect cycle detection, and shared-handle waiter cleanup becomes quadratic during replay.
2 open issues
What changed in this PR
This layer extends the core SDK’s coroutine dispatch with replay-safe asyncio composition and durable timers. It is part of the coordinated async stack, intended to be squash-merged together.
Changes:
- Adds task composition, deterministic cancellation ordering, and retirement of completed tasks and observed failures.
- Backs asyncio sleeps and timeouts with durable timers and provides actionable errors for unsupported loop operations.
- Adds replay, cancellation, memory-retention, and scaling coverage, plus async authoring documentation.
| File | Description |
|---|---|
tests/durabletask/test_orchestration_generator_cleanup.py |
Updates the expected teardown warning. |
tests/durabletask/test_generator_replay_compat.py |
Checks compatibility with existing generator histories. |
tests/durabletask/test_coroutine_orchestrators.py |
Covers coroutine dispatch, callable classification, and replay parity. |
tests/durabletask/test_asyncio_orchestrators.py |
Tests composition, timers, cancellation, memory retirement, and scaling. |
durabletask/worker.py |
Integrates coroutine classification, dispatch, and cleanup. |
durabletask/task.py |
Adds awaitable durable handles, borrowed waiters, and orchestrator type aliases. |
durabletask/internal/shared.py |
Classifies synchronous and asynchronous orchestrator callables. |
durabletask/internal/asyncio_loop.py |
Implements deterministic scheduling, durable timers, and unsupported-operation guards. |
docs/supported-patterns.md |
Connects durable patterns to async authoring and cancellation behavior. |
docs/features.md |
Introduces coroutine orchestration alongside generators. |
docs/async-orchestrators.md |
Documents async APIs, replay constraints, cancellation, and dispatch cleanup. |
Review effort: Balanced
cd0afdb to
adc2ff1
Compare
Bernd Verst (berndverst)
left a comment
There was a problem hiding this comment.
Per-completion replay and durable timers are useful additions. Before this ships, I'd address the Python 3.11 wait_for cancellation race and preserve FIFO between cancellation wakeups and ordinary callbacks. Both can change which durable activities get scheduled. Relative sleep delays also need to be rounded before conversion through the float clock so they don't fire early.
adc2ff1 to
cfb24a5
Compare
Bernd Verst (berndverst)
left a comment
There was a problem hiding this comment.
The waiter, timer, and Python 3.11 timeout changes address the earlier feedback. The FIFO fix still misses a callback queued between two cancellations; I reproduced a different durable action order on replay.
760c90c to
4f2ea50
Compare
Rebased onto main as a single commit. Includes: - Add asyncio composition and durable timers for coroutine orchestrators - Fix same-runtime task-group cancellation replay order - Use exact microsecond ceiling for durable call_at deadlines - Harden durable loop API coverage and task bookkeeping - Cover every inherited asyncio loop API in the durable loop Co-authored-by: Copilot App <[email protected]>
Cancellation wakeups now keep their ready-queue position, relative timer delays round up on the exact orchestration clock, shared-handle waiters are removed in constant time, and the Python 3.11 wait_for cancellation race is documented and covered. Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>
A later cancellation in the same loop step no longer moves its wakeup ahead of a callback or due timer queued between the two cancellations. Co-authored-by: Copilot App <[email protected]>
4f2ea50 to
504b22e
Compare
Bernd Verst (berndverst)
left a comment
There was a problem hiding this comment.
The cancellation batch now preserves FIFO across intervening callbacks and due timers while retaining creation-order sorting for consecutive TaskGroup wakeups. I verified both interleavings against recorded history.

Warning
Do not merge this PR, or any other PR in this stack, on its own. The stack was split out of the closed #293 to make review easier. Once every PR in the stack is approved, the whole stack will be squash-merged together.
Stack layer 2/6, built on the coroutine dispatch layer (#357).
Summary
This layer adds general
asynciocomposition and durable timers to the replay-safe event loop that coroutine orchestrators run on.Task composition
_DurableAsyncTask. A task is retired once its result or exception has been observed, so replaying discardedgatherbatches and handled failures does not keep decoded payloads or tracebacks alive.TaskGroupiterates a set of tasks to cancel them, so this keeps cancellation order independent of memory addresses; the reason is documented in code.TaskGroup,gather,wait_for, andasyncio.timeoutcompose over durable work:TaskGroupis cancelled stay the same across replays.asyncio.Taskconstructed directly rather than throughasyncio.create_task) no longer raises during garbage collection. Directly constructed tasks are documented as unsupported.Durable timers
call_laterandcall_atare backed by durable timers and return_DurableTimerHandle, which compares by identity.call_atdeadlines are rounded up to the next microsecond, so a timer never fires before the loop's deadline.call_later(and soasyncio.sleep) rounds a positive relative delay up to whole microseconds and adds it to the exact orchestration time. Timezone-aware orchestration clocks are normalized.asyncio.sleepand timeouts use the existing chunking for long timers.Unsupported work
NotImplementedError.get_exception_handler()returnsNone, because callback failures always fail the dispatch. A test enumeratesasyncio.AbstractEventLoopso that any public loop API that is neither implemented nor in the table, including one added by a future Python version, fails the suite.asyncio.waitandasyncio.as_completedare not rejected at run time; the contract documents them as unsupported because their ordering is not replay-stable.Docs (
docs/async-orchestrators.md)asyncio.create_taskor a task group.asyncio.wait_forreturns the result instead of raisingCancelledErrorwhen its task is cancelled after the awaited operation completes, and recommendsasyncio.timeout()on 3.11 when cancellation must stop the branch. A test pins the per-version behavior.Code docs
asyncio_loop.pymodule docstring, and clarifies theTask.wait()docstring forasyncio.create_taskand task groups.Scope
Left for later layers
close()from layer 1/6, extended only to release the new timer and cancellation state.OrchestrationContext. The coldAsyncOrchestrationContextarrives in layer 4/6 ([Async stack 4/6] Add cold async orchestration context #361), which updates these tests to use it.Tests
tests/durabletask/test_asyncio_orchestrators.py, covering:AbstractEventLoopAPI