Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ ADDED
synchronous and asynchronous orchestration scheduling, including legacy
`OrchestrationStatus.CANCELED`. Omission preserves backend defaults; existing
protobuf policies remain supported. See [ID reuse](docs/features.md#orchestration-id-reuse).
- Added `async def` orchestrators in preview; their API and replay behavior may
change before general availability. They receive an `AsyncOrchestrationContext`
whose durable operations are cold coroutines, support durable asyncio timers
and ordered composition such as `asyncio.gather`, `asyncio.TaskGroup`, and
`asyncio.timeout`, and offer event waits with owned cancellation and timeouts.
`as_task_context()` and `Task.wait()` interoperate with eager durable task
handles. Pass `return_type` to type and reconstruct async sub-orchestration
results. `asyncio.wait` and `asyncio.as_completed` are not supported.

> [!WARNING]
> Async cleanup can run at every dispatch, including suspension; it is not
> durable workflow compensation. Converting running orchestrations can alter
> replay-visible action order, and recorded async histories depend on the
> Python minor version: before upgrading it, drain in-flight instances or keep
> them on workers pinned to the original version. See the
> [async contract](docs/async-orchestrators.md) for supported operations and
> replay and upgrade guidance.

CHANGED

- A generator orchestrator whose first `yield` is not a `durabletask.Task`, such
as a bare `yield` or `yield [task]`, now fails on its first dispatch with a
`TypeError` ("The orchestrator generator yielded a non-Task object").
Previously a bare first `yield` left the instance pending indefinitely, and a
non-Task first yield scheduled its work and then failed later with an
`AttributeError`.

FIXED

Expand Down
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,57 @@ defaults; an explicit empty deduplication list permits replacement in every
stable status, subject to backend support. See the
[policy documentation](./docs/features.md#orchestration-id-reuse).

### Async Orchestrators

> [!NOTE]
> Async orchestrators are in preview. Their API and replay behavior may change
> before general availability.

All three packages support `async def` orchestrators and nested async helpers.
Existing generator orchestrators continue to work.

```python
import asyncio

from durabletask import task


async def sequence(ctx: task.AsyncOrchestrationContext, value: int) -> list[int]:
first = await ctx.call_activity("plus_one", input=value, return_type=int)
async with asyncio.TaskGroup() as group:
left = group.create_task(ctx.call_activity("plus_one", input=first, return_type=int))
right = group.create_task(ctx.call_activity("plus_one", input=value, return_type=int))
return [left.result(), right.result()]
```

Register the function with `TaskHubGrpcWorker`, `DurableTaskSchedulerWorker`,
or the Azure Functions `orchestration_trigger` decorator as usual. All three
packages use this two-argument shape for async orchestrators. Async
orchestrators automatically receive cold coroutine operations accepted by
`create_task` and task groups.
Generator orchestrators keep their eager, reusable durable handles. For
interoperability, `ctx.as_task_context()` exposes the eager context and
`handle.wait()` creates an independently cancellable borrower.

> [!WARNING]
> This is replay-driven await syntax, not general-purpose asyncio execution.
> Compose durable work with `asyncio.gather`, local tasks/task groups, and
> durable `asyncio.sleep` / timeouts within the
> [supported contract](docs/async-orchestrators.md).
> Perform network, storage, and other nondeterministic I/O in activities.
> Completed foreign awaitables and synchronous side effects cannot reliably be
> detected by the replay engine.
> Python `finally` blocks and async context-manager exits can run at ordinary
> dispatch suspension; they are not workflow-lifetime compensation scopes.

An async helper is not a durable sub-orchestration: use
`await ctx.call_sub_orchestrator(...)` for a separate orchestration instance.
An `async def` function containing `yield` is an async generator, not a supported
orchestrator. Converting an existing orchestrator must preserve durable action
order and payloads for replay compatibility.
See the contract for cancellation, per-dispatch async cleanup, unsupported
ordering-sensitive APIs and framework adapters.

### Task Hub Query Filters

Set `OrchestrationQuery.task_hub_names` to filter orchestration queries by one or
Expand Down
11 changes: 10 additions & 1 deletion azure-functions-durable/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ See [ID reuse](../docs/features.md#orchestration-id-reuse). Core release and
dependency minimum updates are coordinated in the dedicated release PR.
- A best-effort background check warns when a Function App's requirements allow
a future major version, which may introduce replay-breaking changes.

- Added `async def` orchestrators in preview, with the core two-argument shape,
`async def orchestrator(ctx: task.AsyncOrchestrationContext, input)`, and
`df.call_http()` for durable HTTP requests from durabletask orchestration
contexts. The async orchestrator API and replay behavior may change before
general availability. `async def` orchestrators that cannot be called with
`(ctx, input)`, including those with fewer than two positional parameters, are
not supported and raise `TypeError` when registered.
Requires a `durabletask` release with async orchestrator support. See the
[async contract](../docs/async-orchestrators.md) for dispatch cleanup and
replay/upgrade precautions.
- Added opt-in `HttpRetryOptions` on `context.call_http()` for durable initial
request retries with configurable timing and status selection. Empty or omitted
status lists select all non-2xx responses; exhausted retries fail the call.
Expand Down
84 changes: 80 additions & 4 deletions azure-functions-durable/MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,16 @@ The single-argument orchestrator receives the compatibility
Use this state to validate the infrastructure upgrade before changing
application APIs.

Single-argument orchestrators must be generator or regular functions. To write
an `async def` orchestrator, use the native two-argument shape described next;
the `orchestration_trigger` decorator raises `TypeError` for a single-argument
`async def` orchestrator.

## Migrate to native orchestration APIs

Native orchestrators accept the durabletask context and input as separate
arguments:
arguments. Generator functions receive `task.OrchestrationContext`;
`async def` functions receive `task.AsyncOrchestrationContext`:

```python
from typing import Any
Expand Down Expand Up @@ -148,16 +154,86 @@ def say_hello(name: str) -> str:
| `context.call_activity_with_retry(...)` | `ctx.call_activity(..., retry_policy=policy)` |
| `context.call_sub_orchestrator(name, input_)` | `ctx.call_sub_orchestrator(name, input=input_)` |
| `call_sub_orchestrator_with_retry` | `ctx.call_sub_orchestrator(..., retry_policy=...)` |
| `context.task_all(tasks)` | `task.when_all(tasks)` |
| `context.task_any(tasks)` | `task.when_any(tasks)` |
| `context.task_all(tasks)` | `task.when_all(tasks)` (eager `task.Task` handles only) |
| `context.task_any(tasks)` | `task.when_any(tasks)` (eager `task.Task` handles only) |
| `context.new_guid()` | `ctx.new_uuid()` |
| `context.call_http(method, uri, ...)` | `df.call_http(ctx, method, uri, ...)` |
| `df.EntityId(name, key)` | `entities.EntityInstanceId(name, key)` |

Methods including `create_timer`, `wait_for_external_event`,
`continue_as_new`, `set_custom_status`, `call_entity`, and `signal_entity`
retain their names. Activity, sub-orchestration, and entity calls use keyword
`input` instead of the 1.x `input_` or `operationInput` names.
`continue_as_new` uses `new_input`.
`continue_as_new` uses `new_input`. `df.call_http` takes the request options
as keyword arguments.

### Compose async operations

The grouping mappings above apply to generator orchestrators and explicit eager
durable handles, not the cold coroutines returned by
`task.AsyncOrchestrationContext`. Do not pass `ctx.call_activity(...)` coroutines
directly to `task.when_all` or `task.when_any`.

For fan-out in an async orchestrator, use ordered `asyncio.gather` or
`asyncio.TaskGroup`:

```python
import asyncio

import azure.durable_functions as df
from durabletask import task

app = df.DFApp()


@app.orchestration_trigger(context_name="context")
async def greetings(ctx: task.AsyncOrchestrationContext, names: list[str]) -> list[str]:
return list(await asyncio.gather(*(
ctx.call_activity("say_hello", input=name, return_type=str)
for name in names
)))
```

These are not interchangeable with the generator `task.when_all` join, which
waits for every task before raising the first observed failure: default
`gather` reports the first failure without cancelling siblings, while task
groups cancel and drain local siblings on failure. Local cancellation does not
stop already-issued remote work.

For races, obtain eager handles through `ctx.as_task_context()` and pass them to
`task.when_any`, which returns the identical winning handle:

```python
from datetime import timedelta
from typing import Any

import azure.durable_functions as df
from durabletask import task

app = df.DFApp()


@app.orchestration_trigger(context_name="context")
async def approval(ctx: task.AsyncOrchestrationContext, _: Any) -> bool:
handles = ctx.as_task_context()
approved = handles.wait_for_external_event("approval", data_type=bool)
deadline = handles.create_timer(timedelta(hours=1))
winner = await task.when_any([approved, deadline])
if winner is approved:
deadline.cancel() # Release the losing timer.
return approved.get_result()
approved.cancel() # Withdraw the losing wait so it cannot take a later event.
return False
```

`task.when_any` does not cancel the losing handle. Cancel an abandoned event
wait, or it stays first in line and consumes the next matching event, and
cancel an abandoned timer when the event wins. For an event with a deadline,
`await ctx.wait_for_external_event(..., timeout=...)` withdraws both
automatically. Other losing operations, such as activities, keep running;
retain and join them when their completion matters. See the
[async authoring contract](../docs/async-orchestrators.md) before changing
joins or cancellation behavior in running orchestrations.

### Retry policy mapping

Expand Down
97 changes: 94 additions & 3 deletions azure-functions-durable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,98 @@ These compatibility paths remain supported.
> Changing typing modes can change values returned during orchestration replay.
> Validate existing payloads and drain affected orchestrations before switching.

## Async orchestrators

> [!NOTE]
> Async orchestrators are in preview. Their API and replay behavior may change
> before general availability.

Async orchestrators use the same two-argument shape as the core SDK and Durable
Task Scheduler provider. They receive `task.AsyncOrchestrationContext` and the
orchestration input:

```python
import asyncio
from datetime import timedelta
from typing import Any

import azure.durable_functions as df
from durabletask import task

app = df.DFApp()


@app.orchestration_trigger(context_name="context")
async def fan_out(ctx: task.AsyncOrchestrationContext, names: list[str]) -> list[str]:
return list(await asyncio.gather(*(
ctx.call_activity("say_hello", input=name, return_type=str)
for name in names
)))


@app.orchestration_trigger(context_name="context")
async def approval(ctx: task.AsyncOrchestrationContext, _: Any) -> bool:
handles = ctx.as_task_context()
approved = handles.wait_for_external_event("approval", data_type=bool)
deadline = handles.create_timer(timedelta(hours=1))
winner = await task.when_any([approved, deadline])
if winner is approved:
deadline.cancel() # Release the losing timer.
return approved.get_result()
approved.cancel() # Withdraw the losing wait so it cannot take a later event.
return False
```

Durable operations on the async context are cold coroutines. Compose branches
with `asyncio.gather` or `asyncio.TaskGroup`; `asyncio.sleep` and timeouts use
durable timers. For races, schedule eager handles through
`ctx.as_task_context()` and pass them to `task.when_any`, which returns the
identical winning handle. `task.when_any` does not cancel the losing handle:
cancel an abandoned event wait so it cannot consume the next matching event,
and cancel an abandoned timer. For an event with a deadline,
`await ctx.wait_for_external_event(..., timeout=...)` withdraws both
automatically. `handle.wait()` creates an independently cancellable
waiter for an eager handle. Generator orchestrators remain supported, including
mixed-style sub-orchestrations.

Single-argument `async def` orchestrators are not supported. The
`orchestration_trigger` decorator raises `TypeError` for them, and for any
`async def` orchestrator that cannot be called with `(ctx, input)`, such as one
with additional required parameters. The single-argument
`df.DurableOrchestrationContext` remains available for generator orchestrators.

The [shared async contract](../docs/async-orchestrators.md) defines the supported
API subset, cancellation, per-dispatch `finally`/async-generator cleanup,
replay/migration rules, and framework adapter responsibilities. Arbitrary I/O,
threads, subprocesses, `asyncio.wait`/`as_completed`, and automatic recording of
ordinary async helpers are not supported. Put nondeterministic work in ordinary
activities, not orchestrators.

## Durable HTTP calls

`df.call_http` makes durable HTTP requests from durabletask orchestration
contexts. With `task.AsyncOrchestrationContext`, it returns a cold coroutine to
`await`. With a generator's `task.OrchestrationContext`, it schedules the
request immediately and returns a task to `yield`. The result is a
`df.DurableHttpResponse`:

```python
@app.orchestration_trigger(context_name="context")
async def check_status(ctx: task.AsyncOrchestrationContext, url: str) -> int:
response = await df.call_http(ctx, "GET", url)
return response.status_code
```

`df.call_http` accepts the same `content`, `headers`, `token_source`,
`is_raw_str`, and `retry_options` options as `context.call_http()` on
`df.DurableOrchestrationContext`, as keyword arguments. Both follow `202
Accepted` responses with a `Location` header until the operation completes and
return unselected non-2xx responses rather than raising.

## Durable HTTP failure retries

Opt in to durable retries for the **initial** `context.call_http()` request with
Opt in to durable retries for the **initial** `context.call_http()` or
`df.call_http()` request with
`HttpRetryOptions`. It uses the core activity retry engine and durable timers,
not an in-process HTTP retry loop:

Expand Down Expand Up @@ -153,6 +242,7 @@ rather than returning the last selected response. Transport and other activity
failures also use the policy regardless of the status list.

The policy and status list are snapshotted when the request is scheduled.
For async orchestrators, scheduling occurs when the coroutine starts.
Omitting `retry_options` preserves the existing request payload and behavior.
Automatic 202/Location polling remains separate: subsequent GET polls inherit
neither the failure retry policy nor its status classification. Poll HTTP
Expand Down Expand Up @@ -297,8 +387,9 @@ agree on the store and reference encoding; Functions references are JSON strings
> storage error can therefore become a terminal orchestration failure.

Registered orchestration and entity handlers await the store's async methods
before and after execution. Orchestrators remain synchronous generators, and
orchestration replay and entity code run on execution threads with their
before and after execution. Orchestrators may be synchronous generators or
native `async def` functions; both use synchronous replay execution.
Orchestration replay and entity code run on execution threads with their
invocation logging context preserved. Their payload downloads and uploads do
not occupy those threads, and serialization does not access storage during
replay. Custom stores must implement genuinely nonblocking async methods to
Expand Down
Loading