Repository navigation
[Async stack 1/6] Coroutine dispatch - #357
andystaples wants to merge 7 commits into
Conversation
e01cc4a to
1c76780
Compare
1c76780 to
e73a705
Compare
92de85f to
345729f
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The wrapper traversal mistakes a shared target for a cycle, causing registration of a valid metadata-preserving partial to fail with ValueError.
1 open issue
What changed in this PR
Adds coroutine dispatch to the core Durable Task SDK as layer 1/6 of the async orchestrator stack. This layer enables replay-driven async def execution; broader asyncio support and cleanup hardening follow in later layers.
Changes:
- Classifies orchestrators at registration and validates their return types at dispatch.
- Adds a minimal deterministic event loop, awaitable durable tasks, and sync/async orchestrator aliases.
- Adds coroutine and generator replay coverage, including rejection of invalid generator yields.
| File | Description |
|---|---|
tests/durabletask/test_orchestration_generator_cleanup.py |
Updates the expected teardown warning text. |
tests/durabletask/test_generator_replay_compat.py |
Verifies replay compatibility with earlier generator histories. |
tests/durabletask/test_coroutine_orchestrators.py |
Covers coroutine dispatch, callable classification, replay, and validation. |
durabletask/worker.py |
Integrates classification, coroutine execution, validation, and teardown. |
durabletask/task.py |
Adds durable-task awaiting, wait(), and orchestrator aliases. |
durabletask/internal/shared.py |
Adds callable classification and wrapper-cycle detection. |
durabletask/internal/asyncio_loop.py |
Implements dispatch-local scheduling and durable completion delivery. |
Review effort: Balanced
Bernd Verst (berndverst)
left a comment
There was a problem hiding this comment.
Using the existing history engine for both execution styles is the right approach. The documented Pydantic generator bridge still works in the activity replay check. I'd hold this layer until registration preserves valid synchronous adapters and fixes the existing partial-wrapper cycle report.
Co-authored-by: Copilot App <[email protected]>
Track identities across wrapped, partial, and callable nodes; recognize intermediate coroutine functions before following their wrapped targets. Cover bounded worker failures and nested decorated dispatch. Co-authored-by: Copilot App <[email protected]>
Check each callable before following functools.wraps metadata so generator wrappers of async functions stay synchronous, and classify once when the orchestrator is registered so wrapper cycles fail at registration. Normalize timezone-aware orchestration clocks in loop.time(), give loop timers an actionable error, document the private asyncio APIs the loop depends on, and add a generator history replay compatibility test. Remove the changelog entry for unreleased behavior. Co-authored-by: Copilot App <[email protected]>
…le loop for await A generator orchestrator whose first yield is not a durabletask.Task now fails with the existing 'yielded a non-Task object' TypeError plus the async authoring hint. Awaiting a durable Task with no running loop now raises the same RuntimeError as awaiting it on a foreign loop. Co-authored-by: Copilot App <[email protected]>
A plain def that copies an async function's metadata with functools.wraps now keeps synchronous dispatch, so generator adapters keep working. __call__ is resolved on the type, and wrapper cycles are detected only on the active traversal path. Co-authored-by: Copilot App <[email protected]>
345729f to
dbeeb71
Compare
Bernd Verst (berndverst)
left a comment
There was a problem hiding this comment.
The original classifier issues are fixed. Two previously working callable shapes still fail dispatch on this revision: a generator that copies a coroutine marker and a partial subclass that overrides __call__.
…t runs A generator copying an inspect.markcoroutinefunction marker through functools.wraps stays synchronous, and a functools.partial subclass that overrides __call__ is classified by the override instead of its func. Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Bernd Verst (berndverst)
left a comment
There was a problem hiding this comment.
The classifier now honors generator bodies even when decorators copy a coroutine marker, and follows a partial subclass's __call__ override. I verified both fixes through activity replay and confirmed explicitly marked coroutine factories still work.

Warning
Do not merge this PR, or any other PR in the stack, on its own. The whole stack will be squash-merged once every PR in it is approved.
Layer 1/6 of the async orchestrator stack, which splits the closed #293 for review. It is based on #356.
Scope
shared.is_async_callableclassifies orchestrator callables by the code that runs on call. Coroutine functions (and, on Python 3.12+, non-generator functions marked withinspect.markcoroutinefunction()) are async. Any other Python function, including a generator function (even one that copied a coroutine marker throughfunctools.wraps) or a plaindefthat copies an async function'sfunctools.wrapsmetadata, is synchronous, so a sync adapter that returns a generator keeps generator dispatch. Partials, bound methods, and callable instances resolve throughfunc,__func__, and the__call__defined on the type (instance attributes are ignored, as in Python's call path). Afunctools.partialsubclass that overrides__call__is classified by that override. Only a callable whose own code cannot be inspected, such as a C-implemented wrapper, falls back to its__wrapped__target. Cycles are detected on the active traversal path only, so a shared target such asupdate_wrapper(partial(f), f)is not a loop.Taskare rejected withTypeError("The orchestrator generator yielded a non-Task object", plus a hint to useasync def). This now includes the first yield, so a bare firstyieldoryield [task]fails on the first dispatch. Previously a bare firstyieldleft the instance pending indefinitely, and a non-Taskfirst yield scheduled its work and then failed later withAttributeError.DurableEventLoop(durabletask/internal/asyncio_loop.py). This loop is enough to:loop.time()is derived from the replay-safe orchestration clock (naive or timezone-aware). Loop timers, includingasyncio.sleep()with a positive delay, fail with an error that points toctx.create_timer(...)until [Async stack 2/6] asyncio composition and durable timers #358 adds durable loop timers.asyncio_loop.pylists each private asyncio API the loop depends on and why.Task.__await__andTask.wait()are now concrete. Inside the durable loop, awaiting suspends on the durable task. Awaiting with no running loop or under a different event loop raisesRuntimeError.SyncOrchestrator,AsyncOrchestrator, andOrchestratoraliases.Temporary differences (resolved in later layers)
OrchestrationContext, andAsyncOrchestratoris typed against it. Layer 4/6 ([Async stack 4/6] Add cold async orchestration context #361) adds the coldAsyncOrchestrationContext, its tests, and the context API and metadata parity tests.asyncio.sleepwith a positive delay, TaskGroup ordering) and the executor and thread guards come in layer 2/6 ([Async stack 2/6] asyncio composition and durable timers #358).