Skip to content

[Async stack 4/6] Add cold async orchestration context - #361

Open
andystaples wants to merge 1 commit into
andystaples-async-stack-3-dispatch-cleanupfrom
andystaples-async-stack-4-async-context
Open

andystaples wants to merge 1 commit into
andystaples-async-stack-3-dispatch-cleanupfrom
andystaples-async-stack-4-async-context

Conversation

@andystaples

@andystaples andystaples commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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-awaited RuntimeWarning. 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 through Task.wait(), task.when_any winner identification, and completion checks.

The synchronous OrchestrationContext and generator orchestration behavior remain unchanged. Async sub-orchestration calls without return_type are typed as Any.

Tests

  • Adds 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.
  • Adds a public API parity test that compares AsyncOrchestrationContext with OrchestrationContext against an explicit allowlist of intentional differences (as_task_context, async-only timeout, and which members are coroutines), plus a metadata parity test for instance_id, parent_instance_id, version, current_utc_datetime, and is_replaying.
  • Moves the existing coroutine and asyncio tests from layers 1-3 to task.AsyncOrchestrationContext, using ctx.as_task_context() where they need eager handles.
  • Adds tests/durabletask/typing/async_context.py for static typing checks.

Docs and CI

  • docs/async-orchestrators.md documents sub-orchestration result typing, un-awaited operations, operations created before other awaits (with asyncio.create_task to start one immediately), and an example of the winner, completion-check, and result idioms.
  • Extends the async-runtime-windows CI job from [Async stack 3/6] Dispatch cleanup for coroutine orchestrators #360 to run test_async_context.py.

Builds on #360. Functions integration (#363) and final documentation and release notes (#364) follow. The stack splits the closed #293.

Comment thread durabletask/async_context.py Resolved
Comment thread durabletask/async_context.py Resolved
Comment thread durabletask/async_context.py Fixed
Comment thread durabletask/async_context.py Resolved
Comment thread durabletask/async_context.py Resolved
Comment thread durabletask/async_context.py Fixed
Comment thread durabletask/async_context.py Resolved
Comment thread durabletask/async_context.py Fixed
Comment thread durabletask/async_context.py Fixed
Comment thread durabletask/async_context.py Fixed
Comment thread durabletask/async_context.py Resolved
Comment thread durabletask/async_context.py Resolved
Comment thread durabletask/async_context.py Resolved
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 <[email protected]>
@andystaples
andystaples marked this pull request as ready for review November 23, 2026 19:04
Copilot AI balanced review requested due to automatic review settings November 23, 2026 19:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread durabletask/worker.py
Comment on lines +1694 to +1695
with self._async_context._closing_unstarted_operations(): # pyright: ignore[reportPrivateUsage]
self._async_loop.close()
@andystaples
andystaples force-pushed the andystaples-async-stack-4-async-context branch from 9b79875 to 72ad607 Compare November 23, 2026 19:10
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.

2 participants