Repository navigation
[Async stack 1/6] Coroutine dispatch - #357
andystaples wants to merge 4 commits into
Conversation
e01cc4a to
1c76780
Compare
1c76780 to
e73a705
Compare
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
…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 <223556219+Copilot@users.noreply.github.com>
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
| identity = id(node) | ||
| if identity in seen: | ||
| raise ValueError("wrapper loop when classifying async callable") | ||
| seen[identity] = node |
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.
| if isinstance(result, GeneratorType): | ||
| result.close() | ||
| raise TypeError( | ||
| "An async orchestrator must return a coroutine. A synchronous wrapper " |
There was a problem hiding this comment.
A synchronous adapter can return a generator while copying the underlying async function's metadata. For example, @wraps(agent_body) on a plain def that returns run_durable(ctx, ...) used to dispatch on the generator path. It now closes the generator and fails the instance before running it. Once #361 chooses the context before invoking the wrapper, falling back after the call is too late. Please preserve synchronous dispatch for this shape unless the wrapper explicitly opts into coroutine dispatch, and add a regression that replays its activity.
| or inspect.isbuiltin(node) or inspect.ismethodwrapper(node)): | ||
| node = None | ||
| else: | ||
| node = getattr(node, "__call__", None) |
There was a problem hiding this comment.
getattr(node, "__call__") can inspect an instance attribute that Python ignores when calling the object. I reproduced this with a callable class whose __call__ is synchronous and an async __call__ attribute set on the instance: calling it returns the value, but dispatch classifies it as async and fails. Please resolve __call__ via the callable's type, preserving descriptor binding, and test a shadowed instance attribute.

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. Each callable is checked directly first: coroutine functions are async and generator functions are synchronous, even when they carryfunctools.wrapsmetadata from a function of the other kind. Partials and callable objects resolve throughfuncand__call__; only a callable that is neither falls back to the function it wraps. Cyclic__wrapped__chains are reported as errors.functools.wraps. If it returned a generator, that generator is closed.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).