Repository navigation
[Async stack 4/6] Add cold async orchestration context - #361
andystaples wants to merge 1 commit into
Conversation
7c3a7d9 to
ffe3599
Compare
ffe3599 to
60f02ab
Compare
60f02ab to
f8347b8
Compare
Rebased onto main as a single commit. Includes: - Add cold async orchestration context - Adapt replay regressions to cold async context - Correct async sub-orchestration result typing - Check cold-context routing in wrapped orchestrator regressions - Add async context parity checks and reject non-finite timer delays - Close unstarted cold operations at dispatch teardown Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Dispatch teardown skips cleanup when an async-classified wrapper fails before initializing the loop, leaving retained operations unclosed and potentially causing never-awaited warnings.
1 open issue
What changed in this PR
Layer 4 of the async orchestration stack adds AsyncOrchestrationContext to the core SDK. It provides cold, single-use coroutine operations while preserving eager task handles through as_task_context() and leaving generator orchestrators unchanged.
Changes:
- Adds deferred operation scheduling, cancellation-owned timers and event waits, and dispatch cleanup for unstarted operations.
- Adds runtime and typing coverage, and adapts existing async tests to the new context.
- Documents async operation usage and extends Windows CI coverage. Functions integration and release notes follow in later stack layers.
| File | Description |
|---|---|
tests/durabletask/typing/async_context.py |
Checks coroutine result inference and orchestrator registration types. |
tests/durabletask/test_coroutine_orchestrators.py |
Adapts coroutine tests and verifies context selection and metadata parity. |
tests/durabletask/test_asyncio_orchestrators.py |
Migrates asyncio tests to cold operations and explicit eager handles. |
tests/durabletask/test_async_context.py |
Covers cold execution, cancellation ownership, replay, and API parity. |
durabletask/worker.py |
Supplies the async context and integrates unstarted-operation cleanup. |
durabletask/task.py |
Re-exports the async context and updates the async orchestrator type. |
durabletask/async_context.py |
Implements cold coroutine operations and eager-context interoperability. |
docs/async-orchestrators.md |
Explains deferred execution, result typing, and race patterns. |
.github/workflows/durabletask.yml |
Adds async-context tests to Windows CI. |
Review effort: Balanced
| with self._async_context._closing_unstarted_operations(): # pyright: ignore[reportPrivateUsage] | ||
| self._async_loop.close() |
9b79875 to
72ad607
Compare
Bernd Verst (berndverst)
left a comment
There was a problem hiding this comment.
The cold context is useful: it keeps generator orchestration on the existing engine and gives async code an explicit escape hatch for eager, reusable handles. I'd fix the teardown boundary before shipping: closing a never-started operation can release its inputs after the durable loop has restored the host loop. The scheduling example should also distinguish queuing a task from starting an operation; that affects durable action order.
| finally: | ||
| for operation in (*unstarted, *self._operations): | ||
| if _is_unstarted(operation): | ||
| operation.close() |
There was a problem hiding this comment.
This sweep runs after DurableEventLoop.close() has restored the previous running loop. Closing a never-started coroutine releases its captured arguments without executing the coroutine body, but it can still run an input's finalizer. I reproduced this with a cold activity holding the only reference to an object: the dispatch suspended on an event, and the object's finalizer saw the host loop and queued a callback there. Please close these operations inside the durable loop's cleanup boundary and add a regression that runs the executor from a host asyncio loop.
| An operation can be created before other awaits and awaited later; it is | ||
| scheduled at its first await, not where it was created. Dispatch teardown | ||
| closes operations that have not started yet, which schedules nothing and does | ||
| not report them as never awaited. To start an operation immediately, wrap it |
There was a problem hiding this comment.
asyncio.create_task() queues the coroutine; it doesn't start the durable operation inline. In this example the direct timer await schedules action #1 and the report activity schedules action #2. Please say that create_task() queues the operation for a concurrent start, and point to as_task_context() when the activity must be scheduled before the next statement.
| event: task.CancellableTask[Any] = self._context.wait_for_external_event(name, data_type=data_type) | ||
| timer: task.TimerTask | None = None | ||
| try: | ||
| if event.is_complete: |
There was a problem hiding this comment.
The buffered fast path consumes and returns the event before validating the new timeout argument. With timeout="typo", delivering value before go returns value, while delivering go first raises TypeError. Please validate that the timeout is None or a timedelta before registering or consuming the event, while keeping the documented buffered-event precedence for valid zero or negative timeouts.

Warning
This PR is part of a review-only stack and must not be merged individually. The whole stack will be squash-merged after every PR is approved.
Layer 4/6. Async orchestrators receive a cold, await-only
AsyncOrchestrationContext. Durable operations return native, single-use coroutines: validation, serialization, scheduling, and relative deadline evaluation occur when execution starts, not when the coroutine is constructed. Inputs are not copied. Timers and event receives own their local registrations and withdraw them when the waiter exits; cancelling remote-work waiters does not cancel the underlying work. An operation can be created before other awaits and awaited later, even in a later dispatch; it is scheduled at its first await. The context weakly tracks the operation coroutines it creates, and dispatch teardown closes any that never started. That schedules nothing, leaves the response unchanged, and avoids a never-awaitedRuntimeWarning. Operations that have already started are left to the normal loop teardown.as_task_context()is the explicit escape hatch to eager scheduling and reusable durable task handles, including independent borrowed waiters throughTask.wait(),task.when_anywinner identification, and completion checks.The synchronous
OrchestrationContextand generator orchestration behavior remain unchanged. Async sub-orchestration calls withoutreturn_typeare typed asAny.Tests
tests/durabletask/test_async_context.py, covering context selection for supported async callable shapes, replay/action parity, cancellation ownership, timeout ordering, operations awaited in a later dispatch without never-awaited warnings, public result-type inference, and the race-winner, completion-check, and result idioms.AsyncOrchestrationContextwithOrchestrationContextagainst an explicit allowlist of intentional differences (as_task_context, async-onlytimeout, and which members are coroutines), plus a metadata parity test forinstance_id,parent_instance_id,version,current_utc_datetime, andis_replaying.task.AsyncOrchestrationContext, usingctx.as_task_context()where they need eager handles.tests/durabletask/typing/async_context.pyfor static typing checks.Docs and CI
docs/async-orchestrators.mddocuments sub-orchestration result typing, un-awaited operations, operations created before other awaits (withasyncio.create_taskto start one immediately), and an example of the winner, completion-check, and result idioms.async-runtime-windowsCI job from [Async stack 3/6] Dispatch cleanup for coroutine orchestrators #360 to runtest_async_context.py.Builds on #360. Functions integration (#363) and final documentation and release notes (#364) follow. The stack splits the closed #293.