From 513c2a52712f4eb10aff194bb190b8a7d5ff399b Mon Sep 17 00:00:00 2001 From: Andy Staples Date: Mon, 23 Nov 2026 11:53:51 -0700 Subject: [PATCH 1/6] Add cold async orchestration context 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> --- .github/workflows/durabletask.yml | 1 + docs/async-orchestrators.md | 41 ++ durabletask/async_context.py | 343 +++++++++ durabletask/task.py | 3 +- durabletask/worker.py | 17 +- tests/durabletask/test_async_context.py | 651 ++++++++++++++++++ .../durabletask/test_asyncio_orchestrators.py | 98 +-- .../test_coroutine_orchestrators.py | 118 ++-- tests/durabletask/typing/async_context.py | 86 +++ 9 files changed, 1259 insertions(+), 99 deletions(-) create mode 100644 durabletask/async_context.py create mode 100644 tests/durabletask/test_async_context.py create mode 100644 tests/durabletask/typing/async_context.py diff --git a/.github/workflows/durabletask.yml b/.github/workflows/durabletask.yml index 273f9e0a..b7272077 100644 --- a/.github/workflows/durabletask.yml +++ b/.github/workflows/durabletask.yml @@ -32,6 +32,7 @@ jobs: - name: Check asyncio, replay, and dispatch cleanup run: >- python -m pytest + tests\durabletask\test_async_context.py tests\durabletask\test_asyncio_orchestrators.py tests\durabletask\test_coroutine_orchestrators.py tests\durabletask\test_orchestration_generator_cleanup.py diff --git a/docs/async-orchestrators.md b/docs/async-orchestrators.md index b1767522..92d08c37 100644 --- a/docs/async-orchestrators.md +++ b/docs/async-orchestrators.md @@ -106,6 +106,20 @@ allocate action IDs, serialize input, or consume buffered events. Cancelling its its first step starts no durable operation. Operation inputs are not copied: mutations made before the coroutine's first step are observed, and relative timer delays start from that execution point. Keep or copy inputs accordingly. +Dropping an operation without awaiting it or wrapping it in a task schedules +nothing, and Python reports that the coroutine was never awaited. + +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 +with `asyncio.create_task(...)`: + +```python +pending = asyncio.create_task(ctx.call_activity("report", return_type=str)) +await ctx.create_timer(timedelta(minutes=5)) +summary = await pending +``` Cold coroutines are single-use, like ordinary Python coroutines. For an explicitly started operation with multiple independent waiters, retain its @@ -124,6 +138,27 @@ Cancelling either borrower does not cancel the underlying operation. Direct an awaitable durable handle is not a coroutine. Do not retry that expression assuming the eager operation was rolled back. +To identify the winner of a race, check whether an operation has completed, or +read its result later, use eager handles with `task.when_any`, or wrap cold +operations in `asyncio` tasks: + +```python +eager = ctx.as_task_context() +approval = eager.wait_for_external_event("approval", data_type=bool) +deadline = eager.create_timer(timedelta(hours=1)) +winner = await task.when_any([approval, deadline]) # The identical handle wins. +if winner is approval: + deadline.cancel() + approved = approval.get_result() +else: + report = asyncio.create_task(ctx.call_activity("report", return_type=str)) + finished = report.done() # Completion check without waiting. + summary = await report # Result, or the operation's failure. +``` + +The losing handle stays inspectable through `is_complete` and, once complete, +`get_result()`. + The worker classifies each orchestrator once, when it is registered: 1. A callable is async when the code that runs on call is a coroutine @@ -191,6 +226,12 @@ where supported by the provider. Functions compatibility contexts also provide async `task_all` / `task_any`. Generator and coroutine parents/children may call one another through durable sub-orchestrations. +For sub-orchestration results, pass `return_type=Result` to reconstruct a custom +result type. Unlike activity calls, a child orchestrator's return annotation is +not used for result reconstruction, even when passing the child function rather +than its name. Without an explicit `return_type`, the async API returns `Any`; +with the default JSON converter, a plain JSON object remains a dictionary. + The dispatch-local loop supports FIFO ready callbacks, local futures and tasks, `asyncio.create_task`, `asyncio.gather`, `asyncio.sleep`, `asyncio.TaskGroup`, and `asyncio.timeout` / `timeout_at`. diff --git a/durabletask/async_context.py b/durabletask/async_context.py new file mode 100644 index 00000000..8ba0c762 --- /dev/null +++ b/durabletask/async_context.py @@ -0,0 +1,343 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +"""Coroutine-native orchestration context backed by the durable replay engine.""" + +from __future__ import annotations + +import functools +import inspect +import logging +import sys +import weakref +from collections.abc import Callable, Coroutine, Generator +from contextlib import contextmanager +from datetime import datetime, timedelta +from typing import Any, Concatenate, ParamSpec, TypeVar, overload + +from durabletask.entities import EntityInstanceId, EntityLock + +T = TypeVar("T") +TInput = TypeVar("TInput") +TOutput = TypeVar("TOutput") +P = ParamSpec("P") +R = TypeVar("R") + + +def _cold_operation( + operation: Callable[Concatenate[AsyncOrchestrationContext, P], Coroutine[Any, Any, R]], +) -> Callable[Concatenate[AsyncOrchestrationContext, P], Coroutine[Any, Any, R]]: + """Weakly track each operation coroutine so dispatch teardown can close unstarted ones.""" + @functools.wraps(operation) + def create(self: AsyncOrchestrationContext, /, *args: P.args, **kwargs: P.kwargs) -> Coroutine[Any, Any, R]: + coroutine = operation(self, *args, **kwargs) + self._operations.add(coroutine) # pyright: ignore[reportPrivateUsage] + return coroutine + + if sys.version_info >= (3, 12): + inspect.markcoroutinefunction(create) + return create + + +def _is_unstarted(operation: Coroutine[Any, Any, Any]) -> bool: + return inspect.getcoroutinestate(operation) == inspect.CORO_CREATED + + +class AsyncOrchestrationContext: + """Context supplied automatically to ``async def`` orchestrators. + + Durable operations return single-use native coroutines. Constructing one + schedules nothing: validation, input serialization, event consumption, and + relative deadline evaluation occur when it starts. Inputs are not copied + at construction; mutations before execution are observed. + + Cancelling activity, sub-orchestration, entity, or lock waiters does not + cancel remote work. Timers and event receives own their private local + registrations and withdraw them when their coroutine exits. This cannot + undo actions already committed to history or external side effects. + """ + + def __init__(self, context: task.OrchestrationContext) -> None: + self._context = context + self._operations: weakref.WeakSet[Coroutine[Any, Any, Any]] = weakref.WeakSet() + + @contextmanager + def _closing_unstarted_operations(self) -> Generator[None, None, None]: + """Close operation coroutines that never started once dispatch teardown ends. + + An unstarted coroutine has run none of its body, so closing it schedules + nothing. Retaining them during teardown keeps the cancelled orchestrator + from finalizing them first and reporting them as never awaited. + """ + unstarted = [operation for operation in self._operations if _is_unstarted(operation)] + try: + yield + finally: + for operation in (*unstarted, *self._operations): + if _is_unstarted(operation): + operation.close() + + @property + def instance_id(self) -> str: + """Get the current orchestration instance ID.""" + return self._context.instance_id + + @property + def parent_instance_id(self) -> str | None: + """Get the parent orchestration instance ID, if any.""" + return self._context.parent_instance_id + + @property + def version(self) -> str | None: + """Get the orchestration instance version, if any.""" + return self._context.version + + @property + def current_utc_datetime(self) -> datetime: + """Get the replay-safe current UTC time.""" + return self._context.current_utc_datetime + + @property + def is_replaying(self) -> bool: + """Get whether the orchestration is replaying its history.""" + return self._context.is_replaying + + def set_custom_status(self, custom_status: Any) -> None: + """Set the orchestration's custom status.""" + self._context.set_custom_status(custom_status) + + def signal_entity( + self, entity_id: EntityInstanceId, operation_name: str, + input: Any = None, signal_time: datetime | None = None, + ) -> None: + """Send a one-way entity operation.""" + self._context.signal_entity(entity_id, operation_name, input, signal_time) + + def send_event(self, instance_id: str, event_name: str, *, data: Any | None = None) -> None: + """Send a one-way event to another orchestration.""" + self._context.send_event(instance_id, event_name, data=data) + + def continue_as_new( + self, new_input: Any, *, save_events: bool = False, new_version: str | None = None, + ) -> None: + """Continue the orchestration with new input.""" + self._context.continue_as_new(new_input, save_events=save_events, new_version=new_version) + + def new_uuid(self) -> str: + """Create a replay-safe UUID.""" + return self._context.new_uuid() + + def create_replay_safe_logger(self, logger: logging.Logger) -> task.ReplaySafeLogger: + """Create a logger that suppresses messages during replay.""" + return self._context.create_replay_safe_logger(logger) + + def as_task_context(self) -> task.OrchestrationContext: + """Return the eager task-handle context for advanced interoperability. + + Its operations schedule immediately and return reusable durable handles. + Await a handle directly or use ``handle.wait()`` for an independent + coroutine waiter whose cancellation does not cancel the underlying work. + """ + return self._context + + @overload + async def call_activity( + self, activity: task.Activity[TInput, TOutput] | str, *, + input: TInput | None = ..., + retry_policy: task.RetryPolicy | None = ..., + tags: dict[str, str] | None = ..., + return_type: type[T], + ) -> T: + ... + + @overload + async def call_activity( + self, activity: task.Activity[TInput, TOutput], *, + input: TInput | None = ..., + retry_policy: task.RetryPolicy | None = ..., + tags: dict[str, str] | None = ..., + return_type: None = ..., + ) -> TOutput: + ... + + @overload + async def call_activity( + self, activity: str, *, input: Any = ..., + retry_policy: task.RetryPolicy | None = ..., + tags: dict[str, str] | None = ..., + return_type: None = ..., + ) -> Any: + ... + + @_cold_operation + async def call_activity( + self, activity: task.Activity[TInput, TOutput] | str, *, + input: TInput | None = None, + retry_policy: task.RetryPolicy | None = None, + tags: dict[str, str] | None = None, + return_type: type | None = None, + ) -> Any: + """Start and await an activity when this coroutine executes.""" + return await self._context.call_activity( + activity, input=input, retry_policy=retry_policy, tags=tags, + return_type=return_type) + + @overload + async def call_sub_orchestrator( + self, orchestrator: task.Orchestrator[TInput, TOutput] | str, *, + input: TInput | None = ..., + instance_id: str | None = ..., + retry_policy: task.RetryPolicy | None = ..., + version: str | None = ..., + return_type: type[T], + ) -> T: + ... + + @overload + async def call_sub_orchestrator( + self, orchestrator: task.Orchestrator[TInput, TOutput], *, + input: TInput | None = ..., + instance_id: str | None = ..., + retry_policy: task.RetryPolicy | None = ..., + version: str | None = ..., + return_type: None = ..., + ) -> Any: + ... + + @overload + async def call_sub_orchestrator( + self, orchestrator: str, *, input: Any = ..., + instance_id: str | None = ..., + retry_policy: task.RetryPolicy | None = ..., + version: str | None = ..., + return_type: None = ..., + ) -> Any: + ... + + @_cold_operation + async def call_sub_orchestrator( + self, orchestrator: task.Orchestrator[TInput, TOutput] | str, *, + input: TInput | None = None, + instance_id: str | None = None, + retry_policy: task.RetryPolicy | None = None, + version: str | None = None, + return_type: type | None = None, + ) -> Any: + """Start and await a sub-orchestration when this coroutine executes. + + Supply ``return_type`` to reconstruct a custom result type. A callable's + return annotation is not used to reconstruct sub-orchestration results; + without an explicit type, the result is typed as ``Any``. + """ + return await self._context.call_sub_orchestrator( + orchestrator, input=input, instance_id=instance_id, + retry_policy=retry_policy, version=version, return_type=return_type) + + @overload + async def call_entity( + self, entity: EntityInstanceId, operation: str, input: Any = ..., *, + return_type: type[T], + ) -> T: + ... + + @overload + async def call_entity( + self, entity: EntityInstanceId, operation: str, input: Any = ..., *, + return_type: None = ..., + ) -> Any: + ... + + @_cold_operation + async def call_entity( + self, entity: EntityInstanceId, operation: str, input: Any = None, *, + return_type: type | None = None, + ) -> Any: + """Start and await an entity operation when this coroutine executes.""" + return await self._context.call_entity( + entity, operation, input, return_type=return_type) + + @_cold_operation + async def lock_entities(self, entities: list[EntityInstanceId]) -> EntityLock: + """Acquire locks, for use as ``with await ctx.lock_entities(ids):``. + + The returned synchronous context manager releases the locks. This + preserves the original context's entity locking and replay rules; + cancellation does not withdraw an already-issued lock request. An + abandoned request can still acquire locks, which remain held until + released or the orchestration ends. Retain and shield the acquisition + task if a timeout or another local cancellation might interrupt it, + then await it and exit the returned lock when it is safe to do so. + """ + return await self._context.lock_entities(entities) + + @_cold_operation + async def create_timer(self, fire_at: datetime | timedelta) -> None: + """Await a private durable timer, cancelling it when this wait exits. + + Relative deadlines are evaluated when execution starts, not when this + coroutine is constructed. Cancellation removes uncommitted timer work + where possible; it cannot retract a timer already present in history. + """ + timer = self._context.create_timer(fire_at) + try: + await timer + finally: + timer.cancel() + + @overload + async def wait_for_external_event( + self, name: str, *, timeout: timedelta | None = ..., + data_type: type[T], + ) -> T: + ... + + @overload + async def wait_for_external_event( + self, name: str, *, timeout: timedelta | None = ..., + data_type: None = ..., + ) -> Any: + ... + + @_cold_operation + async def wait_for_external_event( + self, name: str, *, timeout: timedelta | None = None, + data_type: type | None = None, + ) -> Any: + """Receive one event through a private, cancellation-owned subscription. + + A relative timeout starts when the coroutine executes and raises + ``TimeoutError`` if its durable timer wins. Buffered events win + immediately, even for zero/negative timeouts, without creating a timer. + Otherwise nonpositive timeouts fail immediately without a timer. + Pending races follow history order. On every exit, the subscription + and any outstanding timeout timer are withdrawn locally, so a later + receive cannot lose an event to this abandoned subscription. + + For a shared subscription use ``as_task_context().wait_for_external_event()`` + and borrowed ``Task.wait()`` instead. + """ + from durabletask.task import when_any + + 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: + return event.get_result() + if timeout is None: + return await event + if timeout <= timedelta(0): + raise TimeoutError(f"Timed out waiting for event {name!r}.") + timer = self._context.create_timer(timeout) + winner = await when_any([event, timer]) + if winner is timer: + raise TimeoutError(f"Timed out waiting for event {name!r}.") + return event.get_result() + finally: + event.cancel() + if timer is not None: + timer.cancel() + + +# Resolve public annotations at runtime without importing task before this +# module has defined the context that task re-exports. +from durabletask import task # noqa: E402 diff --git a/durabletask/task.py b/durabletask/task.py index ae246cf0..c8ac92e3 100644 --- a/durabletask/task.py +++ b/durabletask/task.py @@ -13,6 +13,7 @@ from datetime import UTC, datetime, timedelta from typing import Any, Generic, TypeAlias, TypeVar, cast, overload +from durabletask.async_context import AsyncOrchestrationContext as AsyncOrchestrationContext from durabletask.entities import DurableEntity, EntityInstanceId, EntityLock, EntityContext import durabletask.internal.helpers as pbh import durabletask.internal.orchestrator_service_pb2 as pb @@ -983,7 +984,7 @@ def task_id(self) -> int: ] AsyncOrchestrator: TypeAlias = Callable[ - [OrchestrationContext, TInput], Coroutine[Any, Any, TOutput], + [AsyncOrchestrationContext, TInput], Coroutine[Any, Any, TOutput], ] Orchestrator: TypeAlias = SyncOrchestrator[TInput, TOutput] | AsyncOrchestrator[TInput, TOutput] diff --git a/durabletask/worker.py b/durabletask/worker.py index 1d116def..442f7e9e 100644 --- a/durabletask/worker.py +++ b/durabletask/worker.py @@ -1673,6 +1673,7 @@ def __init__(self, self._exception_properties_provider = exception_properties_provider self._logger = logger self._async_loop: DurableEventLoop | None = None + self._async_context: task.AsyncOrchestrationContext | None = None self._async_cleanup = False def run(self, orchestrator: Generator[task.Task[Any], Any, Any] | Coroutine[Any, Any, Any]) -> None: @@ -1687,7 +1688,11 @@ def run(self, orchestrator: Generator[task.Task[Any], Any, Any] | Coroutine[Any, def close(self) -> None: if self._async_loop is not None: self._async_cleanup = True - self._async_loop.close() + if self._async_context is None: + self._async_loop.close() + return + with self._async_context._closing_unstarted_operations(): # pyright: ignore[reportPrivateUsage] + self._async_loop.close() elif self._generator is not None: self._generator.close() @@ -2761,9 +2766,15 @@ def process_event( input = self._data_converter.deserialize( event.executionStarted.input.value, input_type) - # Layer 4 passes async orchestrators a cold context instead. is_async = self._registry.is_async_orchestrator(event.executionStarted.name, fn) - result = fn(ctx, input) + if is_async: + async_fn = cast(task.AsyncOrchestrator[Any, Any], fn) + async_ctx = task.AsyncOrchestrationContext(ctx) + ctx._async_context = async_ctx # pyright: ignore[reportPrivateUsage] + result = async_fn(async_ctx, input) + else: + sync_fn = cast(task.SyncOrchestrator[Any, Any], fn) + result = sync_fn(ctx, input) if isinstance(result, CoroutineType) and not is_async: result.close() diff --git a/tests/durabletask/test_async_context.py b/tests/durabletask/test_async_context.py new file mode 100644 index 00000000..60587840 --- /dev/null +++ b/tests/durabletask/test_async_context.py @@ -0,0 +1,651 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +"""Cold coroutine and ownership semantics through the shared replay executor.""" + +import asyncio +import gc +import inspect +import json +import logging +import sys +from collections.abc import Coroutine, Generator +from dataclasses import dataclass +from datetime import timedelta +from typing import Any, get_type_hints + +import pytest + +from durabletask import entities, task, worker +from durabletask.async_context import AsyncOrchestrationContext +from durabletask.internal import helpers +from durabletask.internal import orchestrator_service_pb2 as pb +from tests.durabletask.test_coroutine_orchestrators import ( + INSTANCE_ID, + NOW, + _completion, # pyright: ignore[reportPrivateUsage] + _execute, # pyright: ignore[reportPrivateUsage] + _start, # pyright: ignore[reportPrivateUsage] +) + + +def test_additive_public_api_has_no_new_abstract_requirements() -> None: + assert "as_task_context" not in task.OrchestrationContext.__abstractmethods__ + assert "wait" not in task.Task.__abstractmethods__ + assert task.AsyncOrchestrationContext is AsyncOrchestrationContext + + +# Intentional differences between the contexts; anything else is drift. +_ASYNC_ONLY_MEMBERS = {"as_task_context"} +_EAGER_ONLY_MEMBERS: set[str] = set() +_ASYNC_OPERATIONS = { + "call_activity", "call_sub_orchestrator", "call_entity", "lock_entities", + "create_timer", "wait_for_external_event", +} +_EXTRA_ASYNC_PARAMETERS = {"wait_for_external_event": {"timeout"}} + + +def _public_members(cls: type) -> set[str]: + return {name for name in dir(cls) if not name.startswith("_")} + + +def test_async_context_public_api_matches_task_context() -> None: + async_members = _public_members(AsyncOrchestrationContext) + eager_members = _public_members(task.OrchestrationContext) + assert async_members - eager_members == _ASYNC_ONLY_MEMBERS + assert eager_members - async_members == _EAGER_ONLY_MEMBERS + + for name in sorted(async_members & eager_members): + async_member = inspect.getattr_static(AsyncOrchestrationContext, name) + eager_member = inspect.getattr_static(task.OrchestrationContext, name) + assert isinstance(async_member, property) == isinstance(eager_member, property), name + if isinstance(async_member, property): + continue + is_operation = name in _ASYNC_OPERATIONS + assert inspect.iscoroutinefunction(inspect.unwrap(async_member)) == is_operation, name + if sys.version_info >= (3, 12): + # Cold-operation tracking wraps each operation; 3.12+ can mark the wrapper. + assert inspect.iscoroutinefunction(async_member) == is_operation, name + async_parameters = set(inspect.signature(async_member).parameters) + eager_parameters = set(inspect.signature(eager_member).parameters) + assert async_parameters - eager_parameters == _EXTRA_ASYNC_PARAMETERS.get(name, set()), name + assert eager_parameters <= async_parameters, name + + +def test_async_context_metadata_matches_task_context() -> None: + observed: list[tuple[Any, Any]] = [] + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: + eager = ctx.as_task_context() + for name in ("instance_id", "parent_instance_id", "version", "current_utc_datetime", "is_replaying"): + observed.append((getattr(ctx, name), getattr(eager, name))) + await ctx.call_activity("work") + observed.append((ctx.is_replaying, eager.is_replaying)) + + old = _start() + [helpers.new_task_scheduled_event(1, "work")] + _execute(orchestrator, old, [helpers.new_task_completed_event(1, "null")]) + assert observed and all(async_value == eager_value for async_value, eager_value in observed) + assert observed[0][0] == INSTANCE_ID + assert observed[4][0] is True and observed[-1][0] is False + + +def test_public_async_context_annotations_resolve_at_runtime() -> None: + assert get_type_hints(AsyncOrchestrationContext.call_activity)["return"] is Any + assert get_type_hints(AsyncOrchestrationContext.call_sub_orchestrator)["return"] is Any + assert get_type_hints(AsyncOrchestrationContext.as_task_context)["return"] is task.OrchestrationContext + + +@pytest.mark.parametrize("child_style", ["async", "generator", "name"]) +@pytest.mark.parametrize("reconstruction", ["omitted", "none", "explicit"]) +def test_sub_orchestration_result_requires_explicit_reconstruction_type( + child_style: str, reconstruction: str, +) -> None: + @dataclass + class Result: + value: int + + async def async_child(ctx: task.AsyncOrchestrationContext, value: int) -> Result: + return Result(value) + + def generator_child(ctx: task.OrchestrationContext, value: int) -> Generator[task.Task[Any], Any, Result]: + yield ctx.call_activity("work") + return Result(value) + + child = {"async": async_child, "generator": generator_child, "name": "child"}[child_style] + child_name = child if isinstance(child, str) else task.get_name(child) + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + if reconstruction == "explicit": + result = await ctx.call_sub_orchestrator(child, input=42, return_type=Result) + assert isinstance(result, Result) + return result.value + raw: dict[str, int] + if reconstruction == "none": + raw = await ctx.call_sub_orchestrator(child, input=42, return_type=None) + else: + raw = await ctx.call_sub_orchestrator(child, input=42) + assert isinstance(raw, dict) + return raw["value"] + + start = _start() + initial = _execute(orchestrator, [], start) + assert len(initial.actions) == 1 + scheduled = initial.actions[0] + assert scheduled.createSubOrchestration.name == child_name + history = start + [helpers.new_sub_orchestration_created_event( + scheduled.id, child_name, scheduled.createSubOrchestration.instanceId)] + complete = _completion(_execute(orchestrator, history, [ + helpers.new_sub_orchestration_completed_event(scheduled.id, '{"value": 42}')])) + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_COMPLETED + assert json.loads(complete.result.value) == 42 + + +@pytest.mark.parametrize("cancel", [False, True]) +def test_all_operations_are_cold_and_prestart_cancellation_schedules_nothing(cancel: bool) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: + ops = ctx + entity = entities.EntityInstanceId("counter", "one") + invalid_input = object() + operations: list[Coroutine[Any, Any, Any]] = [ + ops.call_activity("unused", input=invalid_input), + ops.call_sub_orchestrator("unused", input=invalid_input), + ops.call_entity(entity, "unused", invalid_input), + ops.lock_entities([entity]), + ops.create_timer(timedelta(days=1)), + ops.wait_for_external_event("unused", timeout=timedelta(days=1)), + ] + assert all(inspect.iscoroutine(operation) for operation in operations) + if cancel: + pending = [asyncio.create_task(operation) for operation in operations] + for waiter in pending: + waiter.cancel() + await asyncio.gather(*pending, return_exceptions=True) + else: + for operation in operations: + operation.close() + return await ctx.call_activity("actual", input=3) + + actions = _execute(orchestrator, [], _start()).actions + assert len(actions) == 1 + assert actions[0].id == 1 + assert actions[0].scheduleTask.name == "actual" + + +@pytest.mark.parametrize("cancel", [False, True]) +def test_cold_receive_does_not_consume_buffered_event(cancel: bool) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: + await ctx.wait_for_external_event("go") + unused = ctx.wait_for_external_event("value") + if cancel: + pending = asyncio.create_task(unused) + pending.cancel() + await asyncio.gather(pending, return_exceptions=True) + else: + unused.close() + return await ctx.wait_for_external_event("value", data_type=str) + + result = _execute(orchestrator, _start(), [ + helpers.new_event_raised_event("value", '"kept"'), + helpers.new_event_raised_event("go", None), + ]) + assert json.loads(_completion(result).result.value) == "kept" + + +def test_create_task_and_task_group_accept_cold_operations() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[int]: + ops = ctx + first = asyncio.create_task(ops.call_activity("first", return_type=int)) + async with asyncio.TaskGroup() as group: + second = group.create_task(ops.call_activity("second", return_type=int)) + return [await first, second.result()] + + start = _start() + initial = _execute(orchestrator, [], start) + assert [(a.id, a.scheduleTask.name) for a in initial.actions] == [(1, "first"), (2, "second")] + old = start + [ + helpers.new_task_scheduled_event(1, "first"), + helpers.new_task_scheduled_event(2, "second"), + ] + result = _execute(orchestrator, old, [ + helpers.new_task_completed_event(2, "2"), + helpers.new_task_completed_event(1, "1"), + ]) + assert json.loads(_completion(result).result.value) == [1, 2] + + +def test_serialization_and_scheduling_follow_coroutine_execution_not_construction() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: + ops = ctx + payload = {"value": 1} + first = ops.call_activity("first", input=payload) + second = ops.call_activity("second", input=payload) + payload["value"] = 2 + try: + await second + return await first + finally: + first.close() + + actions = _execute(orchestrator, [], _start()).actions + assert len(actions) == 1 + assert actions[0].id == 1 + assert actions[0].scheduleTask.name == "second" + assert json.loads(actions[0].scheduleTask.input.value) == {"value": 2} + + +def test_cold_relative_timer_deadline_is_evaluated_when_started() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: + timer = ctx.create_timer(timedelta(hours=1)) + try: + await ctx.wait_for_external_event("go") + await timer + finally: + timer.close() + + later = NOW + timedelta(hours=2) + result = _execute(orchestrator, _start(), [ + helpers.new_orchestrator_started_event(later), + helpers.new_event_raised_event("go", None), + ]) + assert len(result.actions) == 1 + assert result.actions[0].createTimer.fireAt.ToDatetime() == later + timedelta(hours=1) + + +def test_borrowed_waiters_are_independent_reusable_and_do_not_cancel_operations() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[str]: + operation = ctx.as_task_context().call_activity("shared", return_type=str) + cancelled = asyncio.create_task(operation.wait()) + remaining = asyncio.create_task(operation.wait()) + await asyncio.sleep(0) + cancelled.cancel() + await asyncio.gather(cancelled, return_exceptions=True) + return [await remaining, await operation.wait(), await operation] + + old = _start() + [helpers.new_task_scheduled_event(1, "shared")] + result = _execute(orchestrator, old, [helpers.new_task_completed_event(1, '"ok"')]) + assert json.loads(_completion(result).result.value) == ["ok", "ok", "ok"] + + +def test_borrowed_event_cancellation_preserves_shared_subscription() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + event = ctx.as_task_context().wait_for_external_event("value", data_type=int) + first = asyncio.create_task(event.wait()) + second = asyncio.create_task(event.wait()) + await asyncio.sleep(0) + first.cancel() + await asyncio.gather(first, return_exceptions=True) + return await second + + result = _execute(orchestrator, _start(), [helpers.new_event_raised_event("value", "42")]) + assert json.loads(_completion(result).result.value) == 42 + + +def test_borrowed_cancelled_subscription_still_receives_before_owned_replacement() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[int]: + shared = ctx.as_task_context().wait_for_external_event("value", data_type=int) + pending = asyncio.create_task(shared.wait()) + await asyncio.sleep(0) + pending.cancel() + await asyncio.gather(pending, return_exceptions=True) + next_value = await ctx.wait_for_external_event("value", data_type=int) + return [await shared.wait(), next_value] + + result = _execute(orchestrator, _start(), [ + helpers.new_event_raised_event("value", "1"), + helpers.new_event_raised_event("value", "2"), + ]) + assert json.loads(_completion(result).result.value) == [1, 2] + + +def test_cold_coroutines_are_single_use_not_shared_operation_handles() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + operation = ctx.call_activity("work", return_type=int) + value = await operation + with pytest.raises(RuntimeError, match="cannot reuse"): + await operation + return value + + old = _start() + [helpers.new_task_scheduled_event(1, "work")] + result = _execute(orchestrator, old, [helpers.new_task_completed_event(1, "42")]) + assert json.loads(_completion(result).result.value) == 42 + + +def test_owned_timeout_withdraws_subscription_before_replacement() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + ops = ctx + try: + await ops.wait_for_external_event("value", timeout=timedelta(seconds=5)) + except TimeoutError: + return await ops.wait_for_external_event("value", data_type=int) + raise AssertionError("Expected timeout") + + deadline = NOW + timedelta(seconds=5) + initial = _execute(orchestrator, [], _start()) + assert initial.actions[0].id == 1 + old = _start() + [helpers.new_timer_created_event(1, deadline)] + expired = [helpers.new_orchestrator_started_event(deadline), helpers.new_timer_fired_event(1, deadline)] + assert _execute(orchestrator, old, expired).actions == [] + result = _execute(orchestrator, old + expired, [helpers.new_event_raised_event("value", "42")]) + assert json.loads(_completion(result).result.value) == 42 + + +@pytest.mark.parametrize("with_timeout", [False, True]) +def test_owned_cancellation_withdraws_subscription_and_uncommitted_timer(with_timeout: bool) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + ops = ctx + pending = asyncio.create_task(ops.wait_for_external_event( + "value", timeout=timedelta(seconds=5) if with_timeout else None)) + await asyncio.sleep(0) + pending.cancel() + await asyncio.gather(pending, return_exceptions=True) + return await ops.wait_for_external_event("value", data_type=int) + + assert _execute(orchestrator, [], _start()).actions == [] + result = _execute(orchestrator, _start(), [helpers.new_event_raised_event("value", "42")]) + assert json.loads(_completion(result).result.value) == 42 + + +@pytest.mark.parametrize("buffered", [False, True]) +@pytest.mark.parametrize("seconds", [-1, 0, 5]) +def test_buffered_events_and_immediate_timeouts(buffered: bool, seconds: int) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: + await ctx.wait_for_external_event("go") + try: + return await ctx.wait_for_external_event( + "value", timeout=timedelta(seconds=seconds), data_type=int) + except TimeoutError: + return "timeout" + + new = ([helpers.new_event_raised_event("value", "42")] if buffered else []) + [ + helpers.new_event_raised_event("go", None)] + result = _execute(orchestrator, _start(), new) + if not buffered and seconds > 0: + assert len(result.actions) == 1 + assert result.actions[0].HasField("createTimer") + else: + assert len(result.actions) == 1 + assert json.loads(_completion(result).result.value) == (42 if buffered else "timeout") + + +@pytest.mark.parametrize("event_first", [False, True]) +def test_owned_receive_race_follows_history_order(event_first: bool) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: + ops = ctx + try: + value = await ops.wait_for_external_event("value", timeout=timedelta(seconds=5), data_type=int) + return f"event:{value}" + except TimeoutError: + return f"timeout:{await ops.wait_for_external_event('value', data_type=int)}" + + deadline = NOW + timedelta(seconds=5) + old = _start() + [helpers.new_timer_created_event(1, deadline)] + arrival = helpers.new_event_raised_event("value", "42") + fired = helpers.new_timer_fired_event(1, deadline) + result = _execute(orchestrator, old, [arrival, fired] if event_first else [fired, arrival]) + assert json.loads(_completion(result).result.value) == ("event:42" if event_first else "timeout:42") + + +@pytest.mark.parametrize("approved_first", [True, False]) +def test_winner_completion_and_result_idioms(approved_first: bool) -> None: + observed: list[Any] = [] + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: + eager = ctx.as_task_context() + approval = eager.wait_for_external_event("approval", data_type=bool) + deadline = eager.create_timer(timedelta(hours=1)) + winner = await task.when_any([approval, deadline]) + observed.append((winner is approval, approval.is_complete, deadline.is_complete)) + if winner is approval: + deadline.cancel() + return f"approved:{approval.get_result()}" + + report = asyncio.create_task(ctx.call_activity("report", return_type=str)) + await asyncio.sleep(0) + observed.append(report.done()) + return f"timed-out:{await report}" + + deadline = NOW + timedelta(hours=1) + old = _start() + [helpers.new_timer_created_event(1, deadline)] + if approved_first: + new = [helpers.new_event_raised_event("approval", "true")] + for _ in range(2): + observed.clear() + complete = _completion(_execute(orchestrator, old, new)) + assert json.loads(complete.result.value) == "approved:True" + assert observed == [(True, True, False)] + return + + fired = [helpers.new_orchestrator_started_event(deadline), helpers.new_timer_fired_event(1, deadline)] + actions = _execute(orchestrator, old, fired).actions + assert [action.scheduleTask.name for action in actions] == ["report"] + assert observed == [(False, False, True), False] + old += fired + [helpers.new_task_scheduled_event(2, "report")] + for _ in range(2): + observed.clear() + complete = _completion(_execute(orchestrator, old, [helpers.new_task_completed_event(2, '"sent"')])) + assert json.loads(complete.result.value) == "timed-out:sent" + assert observed == [(False, False, True), False] + + +def test_owned_timer_cancellation_does_not_cancel_borrowed_timer() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: + borrowed = ctx.as_task_context().create_timer(timedelta(seconds=10)) + owned = asyncio.create_task(ctx.create_timer(timedelta(seconds=5))) + waiter = asyncio.create_task(borrowed.wait()) + await asyncio.sleep(0) + owned.cancel() + waiter.cancel() + await asyncio.gather(owned, waiter, return_exceptions=True) + await borrowed + + result = _execute(orchestrator, [], _start()) + assert len(result.actions) == 1 + assert result.actions[0].id == 1 + assert result.actions[0].createTimer.fireAt.ToDatetime() == NOW + timedelta(seconds=10) + + +def test_owned_timer_completes_through_history() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: + await asyncio.create_task(ctx.create_timer(timedelta(seconds=5))) + return "done" + + deadline = NOW + timedelta(seconds=5) + old = _start() + [helpers.new_timer_created_event(1, deadline)] + result = _execute(orchestrator, old, [helpers.new_timer_fired_event(1, deadline)]) + assert json.loads(_completion(result).result.value) == "done" + + +def test_owned_event_completion_cancels_only_its_uncommitted_timeout_timer() -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: + await ctx.wait_for_external_event("value", timeout=timedelta(seconds=5)) + return await ctx.call_activity("next") + + result = _execute(orchestrator, _start(), [helpers.new_event_raised_event("value", "42")]) + assert len(result.actions) == 1 + assert result.actions[0].id == 2 + assert result.actions[0].scheduleTask.name == "next" + + +def test_dispatch_disposal_of_owned_resources_preserves_response_and_has_no_cleanup_errors( + caplog: pytest.LogCaptureFixture, +) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: + ops = ctx + await asyncio.gather( + ops.create_timer(timedelta(seconds=10)), + ops.wait_for_external_event("value", timeout=timedelta(seconds=5)), + ops.wait_for_external_event("other"), + ) + + with caplog.at_level(logging.WARNING): + result = _execute(orchestrator, [], _start()) + assert [(a.id, a.createTimer.fireAt.ToDatetime()) for a in result.actions] == [ + (1, NOW + timedelta(seconds=10)), (2, NOW + timedelta(seconds=5))] + assert not caplog.records + + +@pytest.mark.filterwarnings("error::RuntimeWarning") +def test_operation_created_before_other_awaits_starts_later_without_warnings( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Teardown can finalize an unstarted operation; its warning would surface here. + unraisable: list[Any] = [] + monkeypatch.setattr(sys, "unraisablehook", unraisable.append) + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: + operation = ctx.call_activity("work", return_type=str) + await ctx.create_timer(timedelta(seconds=5)) + return await operation + + deadline = NOW + timedelta(seconds=5) + batches = [ + _start(), + [helpers.new_timer_created_event(1, deadline), helpers.new_timer_fired_event(1, deadline)], + [helpers.new_task_scheduled_event(2, "work"), helpers.new_task_completed_event(2, '"done"')], + ] + old: list[pb.HistoryEvent] = [] + observed: list[worker.ExecutionResults] = [] + for new in batches: + result = _execute(orchestrator, old, new) + assert list(result.actions) == list(_execute(orchestrator, old, new).actions) + observed.append(result) + old += new + gc.collect() + + assert [action.createTimer.fireAt.ToDatetime() for action in observed[0].actions] == [deadline] + assert [(action.id, action.scheduleTask.name) for action in observed[1].actions] == [(2, "work")] + assert json.loads(_completion(observed[2]).result.value) == "done" + assert not unraisable + + +def test_sequential_cold_operations_preserve_generator_actions() -> None: + async def coroutine(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + ops = ctx + first = await ops.call_activity("increment", input=3, return_type=int) + return await ops.call_activity("increment", input=first, return_type=int) + + def generator(ctx: task.OrchestrationContext, _: Any) -> Generator[task.Task[Any], Any, int]: + first = yield ctx.call_activity("increment", input=3, return_type=int) + return (yield ctx.call_activity("increment", input=first, return_type=int)) + + batches = [ + _start(), + [helpers.new_task_scheduled_event(1, "increment"), helpers.new_task_completed_event(1, "4")], + [helpers.new_task_scheduled_event(2, "increment"), helpers.new_task_completed_event(2, "5")], + ] + old: list[pb.HistoryEvent] = [] + result = None + for new in batches: + result = _execute(coroutine, old, new) + assert result.actions == _execute(generator, old, new).actions + old += new + assert result is not None + assert json.loads(_completion(result).result.value) == 5 + + +def test_entity_lock_and_typed_call_use_existing_replay_protocol() -> None: + entity = entities.EntityInstanceId("counter", "one") + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + ops = ctx + with await ops.lock_entities([entity]): + return await ops.call_entity(entity, "get", return_type=int) + + old = _start() + lock = _execute(orchestrator, [], old).actions[0].sendEntityMessage.entityLockRequested + new = [ + helpers.new_event_sent_event(1, str(entity), json.dumps({"id": lock.criticalSectionId})), + helpers.new_event_raised_event(lock.criticalSectionId, None), + ] + actions = _execute(orchestrator, old, new).actions + call = actions[0].sendEntityMessage.entityOperationCalled + old += new + [helpers.new_event_sent_event(2, str(entity), json.dumps({"id": call.requestId}))] + complete = _completion(_execute( + orchestrator, old, [helpers.new_event_raised_event(call.requestId, json.dumps({"result": "42"}))])) + assert json.loads(complete.result.value) == 42 + + +def test_timed_out_shielded_lock_is_released_after_acquisition() -> None: + entity = entities.EntityInstanceId("counter", "one") + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: + acquire = asyncio.create_task(ctx.lock_entities([entity])) + try: + lock = await asyncio.wait_for(asyncio.shield(acquire), timeout=2) + except TimeoutError: + lock = await acquire + with lock: + pass + return "timed out" + with lock: + return "acquired" + + start = _start() + initial = _execute(orchestrator, [], start) + assert len(initial.actions) == 2 + lock_action = next(action for action in initial.actions if action.HasField("sendEntityMessage")) + timer_action = next(action for action in initial.actions if action.HasField("createTimer")) + section = lock_action.sendEntityMessage.entityLockRequested.criticalSectionId + deadline = timer_action.createTimer.fireAt.ToDatetime() + assert deadline == NOW + timedelta(seconds=2) + history = start + [ + helpers.new_event_sent_event(lock_action.id, str(entity), json.dumps({"id": section})), + helpers.new_timer_created_event(timer_action.id, deadline), + ] + timed_out = [ + helpers.new_orchestrator_started_event(deadline), + helpers.new_timer_fired_event(timer_action.id, deadline), + ] + assert not _execute(orchestrator, history, timed_out).actions + history += timed_out + granted = [helpers.new_event_raised_event(section, None)] + result = _execute(orchestrator, history, granted) + assert result.actions == _execute(orchestrator, history, granted).actions + assert result.actions[0].sendEntityMessage.entityUnlockSent.targetInstanceId.value == str(entity) + assert json.loads(_completion(result).result.value) == "timed out" + + +@pytest.mark.parametrize("sub_orchestration", [False, True]) +def test_cold_retry_and_call_options_preserve_eager_actions(sub_orchestration: bool) -> None: + policy = task.RetryPolicy(first_retry_interval=timedelta(seconds=2), max_number_of_attempts=3) + + async def cold(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + if sub_orchestration: + return await ctx.call_sub_orchestrator( + "child", input=3, instance_id="child-id", retry_policy=policy, + version="v1", return_type=int) + return await ctx.call_activity( + "work", input=3, retry_policy=policy, tags={"tag": "value"}, return_type=int) + + def eager(ctx: task.OrchestrationContext, _: Any) -> Generator[task.Task[Any], Any, int]: + if sub_orchestration: + return (yield ctx.call_sub_orchestrator( + "child", input=3, instance_id="child-id", retry_policy=policy, + version="v1", return_type=int)) + return (yield ctx.call_activity( + "work", input=3, retry_policy=policy, tags={"tag": "value"}, return_type=int)) + + start = _start() + assert _execute(cold, [], start).actions == _execute(eager, [], start).actions + if sub_orchestration: + scheduled = helpers.new_sub_orchestration_created_event(1, "child", "child-id") + failed = helpers.new_sub_orchestration_failed_event(1, ValueError("retry me")) + completed = helpers.new_sub_orchestration_completed_event(1, "42") + else: + scheduled = helpers.new_task_scheduled_event(1, "work") + failed = helpers.new_task_failed_event(1, ValueError("retry me")) + completed = helpers.new_task_completed_event(1, "42") + old = start + [scheduled] + retry = _execute(cold, old, [failed]) + assert retry.actions == _execute(eager, old, [failed]).actions + assert len(retry.actions) == 1 + assert retry.actions[0].id == 2 + deadline = retry.actions[0].createTimer.fireAt.ToDatetime() + old += [failed, helpers.new_timer_created_event(2, deadline)] + fired = [helpers.new_orchestrator_started_event(deadline), helpers.new_timer_fired_event(2, deadline)] + restarted = _execute(cold, old, fired) + assert restarted.actions == _execute(eager, old, fired).actions + assert restarted.actions[0].id == 1 + old += fired + [scheduled] + complete = _completion(_execute(cold, old, [completed])) + assert json.loads(complete.result.value) == 42 diff --git a/tests/durabletask/test_asyncio_orchestrators.py b/tests/durabletask/test_asyncio_orchestrators.py index aebea898..a3ae2834 100644 --- a/tests/durabletask/test_asyncio_orchestrators.py +++ b/tests/durabletask/test_asyncio_orchestrators.py @@ -37,9 +37,9 @@ @pytest.mark.parametrize("fail_branch", [False, True]) def test_task_group_branches_inherit_context_and_replay_stable_actions(fail_branch: bool) -> None: - context: ContextVar[task.OrchestrationContext] = ContextVar("orchestration") + context: ContextVar[task.AsyncOrchestrationContext] = ContextVar("orchestration") - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: token = context.set(ctx) try: results: dict[str, Any] = {} @@ -89,7 +89,7 @@ def replay(old: list[pb.HistoryEvent], new: list[pb.HistoryEvent]): def test_gather_resumes_each_branch_without_waiting_for_batch() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[Any]: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[Any]: async def branch(name: str) -> Any: value: Any = await ctx.call_activity(name) await asyncio.sleep(0) @@ -115,12 +115,12 @@ async def branch(name: str) -> Any: @pytest.mark.parametrize("return_exceptions", [False, True]) def test_gather_failure_does_not_cancel_sibling(return_exceptions: bool) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: async def sibling() -> str: await ctx.call_activity("slow") return await ctx.call_activity("continued", return_type=str) - failure = asyncio.create_task(ctx.call_activity("fail", return_type=str).wait()) + failure = asyncio.create_task(ctx.call_activity("fail", return_type=str)) pending = asyncio.create_task(sibling()) try: values = await asyncio.gather( @@ -144,7 +144,7 @@ async def sibling() -> str: def test_task_group_failure_cancels_waiters_not_durable_work() -> None: cleaned: list[str] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def branch(name: str) -> None: try: await ctx.call_activity(name) @@ -165,7 +165,7 @@ async def branch(name: str) -> None: @pytest.mark.parametrize("kind", ["sleep", "wait_for", "timeout"]) def test_durable_timer_and_timeout(kind: str) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: try: if kind == "sleep": await asyncio.sleep(2) @@ -189,7 +189,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: def test_sleep_zero_fifo_without_timer() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[int]: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[int]: observed: list[int] = [] async def branch(index: int) -> None: @@ -215,7 +215,7 @@ class Payload: def __post_init__(self) -> None: references.append(ref(self)) - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: for batch in range(8): await asyncio.gather(*(ctx.call_activity("work", return_type=Payload) for _ in range(4))) await asyncio.sleep(0) @@ -251,7 +251,7 @@ class Payload: def __post_init__(self) -> None: references.append(ref(self)) - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: async def branch() -> None: payload = await ctx.call_activity("work", return_type=Payload) raise ValueError(f"Failed with payload {payload.value}") @@ -311,7 +311,7 @@ async def branch() -> None: def test_retired_tasks_do_not_reuse_cancellation_order() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: async def completed() -> None: pass @@ -345,7 +345,7 @@ def test_dispatch_cleanup_unwinds_scopes_contexts_asyncgens(under_loop: bool) -> cleaned: list[str] = [] references: list[Any] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: references.extend([ref(ctx), ref(asyncio.get_running_loop()), ref(asyncio.current_task())]) async def resource(): @@ -384,8 +384,8 @@ async def outer() -> None: def test_shared_operation_survives_local_waiter_cancellation() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: - durable = ctx.call_activity("shared", return_type=str) + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: + durable = ctx.as_task_context().call_activity("shared", return_type=str) async def wait() -> Any: return await durable @@ -403,8 +403,8 @@ async def wait() -> Any: def test_local_durable_cancellation_wakes_waiter_without_history() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: - timer = ctx.create_timer(timedelta(days=1)) + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: + timer = ctx.as_task_context().create_timer(timedelta(days=1)) async def waiter() -> None: await timer @@ -436,7 +436,7 @@ async def resource(): await asyncio.sleep(0) cleaned.append("resource") - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async with resource(): try: ctx.set_custom_status("waiting") @@ -475,7 +475,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: def test_unhandled_local_root_cancellation_fails_not_abandons() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: raise asyncio.CancelledError complete = _completion(_execute(orchestrator, [], _start())) @@ -487,7 +487,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: def test_timer_rounding_never_precedes_loop_deadline(delay: float) -> None: observed: list[float] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: loop = asyncio.get_running_loop() observed.append(loop.time() + delay) await asyncio.sleep(delay) @@ -502,7 +502,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: def test_call_at_deadline_is_exact_microsecond_ceiling(offset: float) -> None: requested: list[float] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: loop = asyncio.get_running_loop() when = loop.time() + offset requested.append(when) @@ -525,7 +525,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: def test_call_at_known_float_deadline_rounds_up_to_next_microsecond() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: loop = asyncio.get_running_loop() fired = loop.create_future() loop.call_at(loop.time() + 0.000003, fired.set_result, None) @@ -537,7 +537,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: @pytest.mark.parametrize("offset", [0.0, -1.0]) def test_call_at_due_deadline_runs_without_durable_timer(offset: float) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: loop = asyncio.get_running_loop() fired = loop.create_future() loop.call_at(loop.time() + offset, fired.set_result, "fired") @@ -549,7 +549,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: @pytest.mark.parametrize("when", [1e20, float(sys.float_info.max)]) def test_call_at_out_of_range_deadline_fails(when: float) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: asyncio.get_running_loop().call_at(when, lambda: None) complete = _completion(_execute(orchestrator, [], _start())) @@ -558,7 +558,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: def test_async_sleep_uses_shared_long_timer_chunking() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: await asyncio.sleep(7 * 86400) return "done" @@ -578,7 +578,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: @pytest.mark.parametrize("api", ["thread", "io", "foreign-loop"]) def test_unsupported_work_fails_actionably(api: str) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: if api == "thread": await asyncio.to_thread(lambda: None) elif api == "io": @@ -600,7 +600,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: def test_orchestrator_cannot_close_the_durable_loop(caught: bool, caplog: pytest.LogCaptureFixture) -> None: cleaned: list[str] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: async def background() -> None: try: await ctx.call_activity("background") @@ -639,7 +639,7 @@ class CleanupAbort(BaseException): error = CleanupAbort("cleanup failed") if unexpected else ValueError("cleanup failed") - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def resource(): try: yield 1 @@ -692,8 +692,8 @@ def test_untracked_task_finalized_after_dispatch_close_is_quiet(monkeypatch: pyt monkeypatch.setattr(sys, "unraisablehook", unraisable.append) untracked: list[asyncio.Task[Any]] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: - untracked.append(asyncio.Task(ctx.call_activity("background").wait())) + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: + untracked.append(asyncio.Task(ctx.call_activity("background"))) await ctx.call_activity("work") _execute(orchestrator, [], _start()) @@ -703,7 +703,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: def test_task_group_cancellation_durable_cleanup_has_stable_action_order() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def branch(index: int) -> None: try: await ctx.call_activity("work", input=index) @@ -732,7 +732,7 @@ async def branch(index: int) -> None: async def _shared_future_cancellation_orchestrator( - ctx: task.OrchestrationContext, _: Any, *, direct_future: bool, gather_wait: bool, + ctx: task.AsyncOrchestrationContext, _: Any, *, direct_future: bool, gather_wait: bool, ) -> str: loop = asyncio.get_running_loop() futures: list[asyncio.Future[None]] = [ @@ -740,7 +740,7 @@ async def _shared_future_cancellation_orchestrator( def on_cancelled(index: int, future: asyncio.Future[None]) -> None: assert future.cancelled() - ctx.call_activity(f"cancelled-{index}") + ctx.as_task_context().call_activity(f"cancelled-{index}") for index, future in enumerate(futures): future.add_done_callback(partial(on_cancelled, index)) @@ -840,7 +840,7 @@ def test_shared_future_cancellation_history_replays_in_other_worker_processes() def test_cancelling_last_loop_timer_cannot_hide_foreign_wait() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: handle = asyncio.get_running_loop().call_later(1, lambda: None) handle.cancel() await asyncio.Future() @@ -854,7 +854,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: @pytest.mark.parametrize("probe_pending", [False, True]) def test_background_failure_is_reported_unless_observed( observed: bool, probe_pending: bool, caplog: pytest.LogCaptureFixture) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: async def fail() -> None: raise ValueError("background failure") @@ -881,7 +881,7 @@ def test_unexpected_cleanup_failure_does_not_hide_other_task_errors( class CleanupAbort(BaseException): pass - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def branch(error: BaseException) -> None: try: await ctx.wait_for_external_event("never") @@ -907,7 +907,7 @@ def test_cleanup_callback_failure_still_drains_tasks_and_asyncgens( def fail() -> None: raise SystemExit("cleanup exit") if control_flow else ValueError("cleanup callback") - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def resource(): try: yield 1 @@ -962,7 +962,7 @@ def is_complete(durable: task.Task[Any]) -> bool: monkeypatch.setattr(task.Task, "is_complete", property(is_complete)) - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[Any]: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[Any]: async def branch(index: int) -> Any: return await ctx.call_activity("work", input=index) @@ -978,7 +978,7 @@ def test_nonquiescent_cleanup_does_not_skip_asyncgen_closure( caplog: pytest.LogCaptureFixture) -> None: cleaned: list[str] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def resource(): try: yield 1 @@ -1011,7 +1011,7 @@ class CleanupAbort(BaseException): if grouped: exit_error = BaseExceptionGroup("forced cleanup group", [exit_error, ValueError("nested")]) - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def branch(name: str, error: BaseException | None) -> None: try: await ctx.wait_for_external_event(name) @@ -1042,7 +1042,7 @@ async def branch(name: str, error: BaseException | None) -> None: @pytest.mark.parametrize("timer", [False, True]) def test_callback_stop_iteration_fails_instead_of_completing_orchestrator(timer: bool) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: def fail() -> None: raise StopIteration("callback stopped") @@ -1069,7 +1069,7 @@ def fail() -> None: @pytest.mark.parametrize("cancelled", [None, 0, 1]) def test_identical_timer_callbacks_complete_and_cancel_independently(cancelled: int | None) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> int: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> int: loop = asyncio.get_running_loop() result = loop.create_future() completed: list[None] = [] @@ -1236,7 +1236,7 @@ class CleanupAbort(BaseException): exit_error = BaseExceptionGroup("cleanup exit", [SystemExit("stop"), ValueError("nested")]) cleaned: list[str] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def branch(name: str, error: BaseException | None) -> None: try: await ctx.wait_for_external_event(name) @@ -1280,7 +1280,7 @@ def test_cleanup_keeps_process_control_over_other_control_flow( errors.reverse() cleaned: list[int] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def branch(index: int) -> None: try: await ctx.wait_for_external_event(f"never-{index}") @@ -1304,7 +1304,7 @@ class CleanupAbort(BaseException): errors: list[BaseException] = [CleanupAbort("first"), GeneratorExit("later")] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def branch(index: int) -> None: try: await ctx.wait_for_external_event(f"never-{index}") @@ -1327,7 +1327,7 @@ def firstiter(generator: Any) -> None: def finalizer(generator: Any) -> None: pass - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def resource() -> AsyncIterator[int]: yield 1 @@ -1357,7 +1357,7 @@ def test_cleanup_closes_async_generators_started_during_generator_cleanup( count: int, caplog: pytest.LogCaptureFixture) -> None: closed: list[int] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def resource(index: int) -> AsyncIterator[int]: try: yield 1 @@ -1402,7 +1402,7 @@ def emit(self, record): logging.getLogger().addHandler(Capture()) -async def orchestrator(ctx: task.OrchestrationContext, _): +async def orchestrator(ctx: task.AsyncOrchestrationContext, _): async def resource(): try: yield 1 @@ -1445,7 +1445,7 @@ async def signal_on_forced_close(): def test_exhausted_async_generators_are_not_retained_until_dispatch_end() -> None: references: list[ReferenceType[Any]] = [] - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: async def values() -> AsyncIterator[int]: yield 1 @@ -1471,7 +1471,7 @@ def test_entity_lock_disposal_during_teardown_does_not_schedule_releases( first = entities.EntityInstanceId("counter", "a") second = entities.EntityInstanceId("counter", "b") - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: with await ctx.lock_entities([first, second]): if suspend == "activity": return await ctx.call_activity("inside-lock") diff --git a/tests/durabletask/test_coroutine_orchestrators.py b/tests/durabletask/test_coroutine_orchestrators.py index 584513c5..105af22f 100644 --- a/tests/durabletask/test_coroutine_orchestrators.py +++ b/tests/durabletask/test_coroutine_orchestrators.py @@ -56,7 +56,7 @@ def _completion(result: worker.ExecutionResults) -> pb.CompleteOrchestrationActi def test_coroutine_immediate_return() -> None: - async def orchestrator(ctx: task.OrchestrationContext, value: int) -> int: + async def orchestrator(ctx: task.AsyncOrchestrationContext, value: int) -> int: return value complete = _completion(_execute(orchestrator, [], _start())) @@ -65,25 +65,29 @@ async def orchestrator(ctx: task.OrchestrationContext, value: int) -> int: @pytest.mark.parametrize("shape", ["function", "wrapped", "partial", "object", "partial-object"]) -def test_async_orchestrator_is_dispatched_for_supported_callable_shapes(shape: str) -> None: - async def plain(ctx: task.OrchestrationContext, value: int) -> int: +def test_async_context_is_selected_for_supported_callable_shapes(shape: str) -> None: + async def plain(ctx: task.AsyncOrchestrationContext, value: int) -> int: + assert isinstance(ctx, task.AsyncOrchestrationContext) return value + 1 @wraps(plain) async def wrapped(*args: Any, **kwargs: Any) -> Any: return await plain(*args, **kwargs) - async def with_prefix(prefix: int, ctx: task.OrchestrationContext, value: int) -> int: + async def with_prefix(prefix: int, ctx: task.AsyncOrchestrationContext, value: int) -> int: + assert isinstance(ctx, task.AsyncOrchestrationContext) return value + prefix class AsyncCallable: - async def __call__(self, ctx: task.OrchestrationContext, value: int) -> int: + async def __call__(self, ctx: task.AsyncOrchestrationContext, value: int) -> int: + assert isinstance(ctx, task.AsyncOrchestrationContext) return value + 1 class AsyncPartialCallable: async def __call__( - self, prefix: int, ctx: task.OrchestrationContext, value: int, + self, prefix: int, ctx: task.AsyncOrchestrationContext, value: int, ) -> int: + assert isinstance(ctx, task.AsyncOrchestrationContext) return value + prefix variants = { @@ -99,13 +103,14 @@ async def __call__( class _AsyncWrapsSync: - def base(self, ctx: task.OrchestrationContext, value: int) -> int: + def base(self, ctx: task.AsyncOrchestrationContext, value: int) -> int: + assert isinstance(ctx, task.AsyncOrchestrationContext) return value + 1 - async def asynchronous(self, ctx: task.OrchestrationContext, value: int) -> int: + async def asynchronous(self, ctx: task.AsyncOrchestrationContext, value: int) -> int: return self.base(ctx, value) - async def __call__(self, ctx: task.OrchestrationContext, value: int) -> int: + async def __call__(self, ctx: task.AsyncOrchestrationContext, value: int) -> int: return self.base(ctx, value) @@ -117,11 +122,12 @@ async def __call__(self, ctx: task.OrchestrationContext, value: int) -> int: "function", "partial", "bound-method", "callable", "partial-callable", "c-wrapper", ]) def test_async_orchestrator_is_recognized_before_its_synchronous_wrapped_function(shape: str) -> None: - def base(ctx: task.OrchestrationContext, value: int) -> int: + def base(ctx: task.AsyncOrchestrationContext, value: int) -> int: + assert isinstance(ctx, task.AsyncOrchestrationContext) return value + 1 @wraps(base) - async def asynchronous(ctx: task.OrchestrationContext, value: int) -> int: + async def asynchronous(ctx: task.AsyncOrchestrationContext, value: int) -> int: return base(ctx, value) # C-implemented wrappers expose no inspectable code, so classification @@ -196,11 +202,12 @@ def asynchronous(self, ctx: task.OrchestrationContext, value: int) -> Any: # ty @pytest.mark.skipif(sys.version_info < (3, 12), reason="inspect.markcoroutinefunction requires Python 3.12+") def test_marked_synchronous_wrapper_is_async() -> None: - async def asynchronous(ctx: task.OrchestrationContext, value: int) -> int: + async def asynchronous(ctx: task.AsyncOrchestrationContext, value: int) -> int: + assert isinstance(ctx, task.AsyncOrchestrationContext) return value + 1 @wraps(asynchronous) - def wrapper(ctx: task.OrchestrationContext, value: int) -> Any: + def wrapper(ctx: task.AsyncOrchestrationContext, value: int) -> Any: return asynchronous(ctx, value) marked = inspect.markcoroutinefunction(wrapper) # type: ignore[attr-defined] @@ -253,6 +260,7 @@ async def agent_body(ctx: task.OrchestrationContext, value: int) -> int: return value def run_durable(ctx: task.OrchestrationContext, value: int) -> Generator[task.Task[Any], Any, int]: + assert not isinstance(ctx, task.AsyncOrchestrationContext) result = yield ctx.call_activity("work", input=value, return_type=int) return result + 1 @@ -303,7 +311,7 @@ def test_synchronous_builtin_callables_are_not_classified_as_async(fn: Any) -> N def test_unmarked_synchronous_wrapper_returning_coroutine_is_rejected_and_closed() -> None: created: list[Any] = [] - async def async_orchestrator(ctx: task.OrchestrationContext, value: int) -> int: + async def async_orchestrator(ctx: task.AsyncOrchestrationContext, value: int) -> int: return value def wrapper(ctx: task.OrchestrationContext, value: int) -> Any: @@ -318,7 +326,7 @@ def wrapper(ctx: task.OrchestrationContext, value: int) -> Any: def _async_classified_wrapper(result: Callable[[], Any]) -> Any: - async def async_orchestrator(ctx: task.OrchestrationContext, value: int) -> int: + async def async_orchestrator(ctx: task.AsyncOrchestrationContext, value: int) -> int: return value # A C-implemented wrapper is classified by the async function it names. @@ -374,7 +382,7 @@ def __call__(self, ctx: task.OrchestrationContext, value: int) -> Generator[task def test_orchestrator_is_classified_once_at_registration(monkeypatch: pytest.MonkeyPatch) -> None: - async def orchestrator(ctx: task.OrchestrationContext, value: int) -> int: + async def orchestrator(ctx: task.AsyncOrchestrationContext, value: int) -> int: return await ctx.call_activity("work", input=value, return_type=int) registry = worker._Registry() # pyright: ignore[reportPrivateUsage] @@ -448,10 +456,41 @@ def test_mixed_wrapper_cycle_is_rejected_within_bounded_registration(shape: str) assert result.returncode == 0, result.stdout + result.stderr +def test_async_context_metadata_and_commands_match_sync_context() -> None: + entity = entities.EntityInstanceId("counter", "one") + + def use_context(ctx: task.OrchestrationContext | task.AsyncOrchestrationContext) -> list[Any]: + ctx.set_custom_status({"state": "running"}) + ctx.send_event("other", "ping", data=3) + ctx.signal_entity(entity, "add", 1) + return [ + ctx.instance_id, ctx.parent_instance_id, ctx.version, + ctx.current_utc_datetime.isoformat(), ctx.is_replaying, ctx.new_uuid(), + ] + + async def async_orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[Any]: + return use_context(ctx) + + def sync_orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[Any]: + return use_context(ctx) + + started = helpers.new_execution_started_event("test", INSTANCE_ID, "null", version="v1") + started.executionStarted.parentInstance.CopyFrom(pb.ParentInstanceInfo( + taskScheduledId=1, + orchestrationInstance=pb.OrchestrationInstance(instanceId="parent"))) + new = [helpers.new_orchestrator_started_event(NOW), started] + async_result = _execute(async_orchestrator, [], new) + sync_result = _execute(sync_orchestrator, [], new) + assert async_result.actions == sync_result.actions + assert async_result.encoded_custom_status == sync_result.encoded_custom_status == '{"state": "running"}' + assert json.loads(_completion(async_result).result.value)[:5] == [ + INSTANCE_ID, "parent", "v1", NOW.isoformat(), False] + + def test_await_is_concrete_and_completed_tasks_do_not_suspend() -> None: assert "__await__" not in task.Task.__abstractmethods__ - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[int]: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[int]: completed = task.CompletableTask[int]() completed.complete(7) return [await completed, await completed] @@ -459,19 +498,6 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[int]: assert json.loads(_completion(_execute(orchestrator, [], _start())).result.value) == [7, 7] -def test_task_wait_suspends_until_durable_completion() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: - return await ctx.call_activity("work", return_type=str).wait() - - old = _start() - actions = _execute(orchestrator, [], old).actions - assert [a.scheduleTask.name for a in actions] == ["work"] - old += [helpers.new_task_scheduled_event(1, "work")] - complete = _completion(_execute( - orchestrator, old, [helpers.new_task_completed_event(1, '"done"')])) - assert json.loads(complete.result.value) == "done" - - @pytest.mark.parametrize("yielded", ["none", "list"]) @pytest.mark.parametrize("after_activity", [False, True]) def test_generator_invalid_yield_recommends_async_authoring(after_activity: bool, yielded: str) -> None: @@ -513,7 +539,7 @@ async def await_task() -> int: def test_precompleted_failure_and_cancellation_are_catchable() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[str]: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[str]: failed = task.CompletableTask[int]() failed.fail("failed", ValueError("boom")) cancelled = task.TimerTask() @@ -534,10 +560,10 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[str]: def test_nested_async_helpers_replay_with_generator_action_parity() -> None: - async def helper(ctx: task.OrchestrationContext, value: int) -> int: + async def helper(ctx: task.AsyncOrchestrationContext, value: int) -> int: return await ctx.call_activity("increment", input=value, return_type=int) - async def coroutine(ctx: task.OrchestrationContext, value: int) -> int: + async def coroutine(ctx: task.AsyncOrchestrationContext, value: int) -> int: first = await helper(ctx, value) return await helper(ctx, first) @@ -563,9 +589,9 @@ def generator(ctx: task.OrchestrationContext, value: int) -> Generator[task.Task def test_fan_out_waits_for_all_and_preserves_input_order() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[str]: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[str]: pending: list[task.Task[str]] = [ - ctx.call_activity("work", input=i, return_type=str) for i in range(2)] + ctx.as_task_context().call_activity("work", input=i, return_type=str) for i in range(2)] return await task.when_all(pending) old = _start() @@ -580,7 +606,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[str]: @pytest.mark.parametrize("caught", [False, True]) def test_activity_failure_is_thrown_into_coroutine(caught: bool) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: try: await ctx.call_activity("boom") except task.TaskFailedError: @@ -602,9 +628,9 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> str: @pytest.mark.parametrize("event_wins", [False, True]) def test_when_any_timer_and_external_event(event_wins: bool) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: - event = ctx.wait_for_external_event("approval") - timer = ctx.create_timer(timedelta(seconds=1)) + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: + event = ctx.as_task_context().wait_for_external_event("approval") + timer = ctx.as_task_context().create_timer(timedelta(seconds=1)) winner = await task.when_any([event, timer]) if winner is event: timer.cancel() @@ -620,7 +646,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: def test_sub_orchestration_and_replay_flags() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[Any]: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[Any]: before = ctx.is_replaying value = await ctx.call_sub_orchestrator("child", instance_id="child-test", return_type=str) return [value, before, ctx.is_replaying] @@ -632,7 +658,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[Any]: def test_retry_timer_resumes_same_coroutine() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: return await ctx.call_activity( "retry", retry_policy=task.RetryPolicy( first_retry_interval=timedelta(seconds=1), max_number_of_attempts=2)) @@ -660,7 +686,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: @pytest.mark.parametrize("caught", [False, True]) def test_coroutine_non_retriable_failure( sub_orchestration: bool, replayed: bool, caught: bool) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: retry = task.RetryPolicy(first_retry_interval=timedelta(seconds=1), max_number_of_attempts=3) try: if sub_orchestration: @@ -701,7 +727,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: def test_entity_lock_and_call_suspend_coroutine() -> None: entity = entities.EntityInstanceId("counter", "one") - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: with await ctx.lock_entities([entity]): return await ctx.call_entity(entity, "get") @@ -723,7 +749,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: def test_continue_as_new_from_coroutine() -> None: - async def orchestrator(ctx: task.OrchestrationContext, value: int) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, value: int) -> None: await ctx.wait_for_external_event("next") ctx.continue_as_new(value + 1) @@ -734,7 +760,7 @@ async def orchestrator(ctx: task.OrchestrationContext, value: int) -> None: def test_nondeterminism_still_detected_for_coroutines() -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> Any: return await ctx.call_activity("changed") complete = _completion(_execute( @@ -745,7 +771,7 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: @pytest.mark.parametrize("after_activity", [False, True]) def test_non_durable_suspension_is_rejected(after_activity: bool) -> None: - async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: if after_activity: await ctx.call_activity("work") await asyncio.Future() diff --git a/tests/durabletask/typing/async_context.py b/tests/durabletask/typing/async_context.py new file mode 100644 index 00000000..487fe27d --- /dev/null +++ b/tests/durabletask/typing/async_context.py @@ -0,0 +1,86 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +"""The async surface returns native coroutines with inferred result types.""" + +import asyncio +from collections.abc import Coroutine, Generator +from dataclasses import dataclass +from datetime import timedelta +from typing import Any, assert_type + +from durabletask import client, task, worker +from durabletask.entities import EntityInstanceId, EntityLock + + +def increment(ctx: task.ActivityContext, value: int) -> int: + return value + 1 + + +async def child(ctx: task.AsyncOrchestrationContext, value: int) -> str: + return str(value) + + +@dataclass +class Result: + value: int + + +async def result_child(ctx: task.AsyncOrchestrationContext, value: int) -> Result: + return Result(value) + + +async def none_child(ctx: task.AsyncOrchestrationContext, value: int) -> None: + pass + + +def generator_child(ctx: task.OrchestrationContext, value: int) -> Generator[task.Task[Any], Any, str]: + result = yield ctx.call_activity("work", input=value) + return str(result) + + +def check_registration(hub_worker: worker.TaskHubGrpcWorker, hub_client: client.TaskHubGrpcClient) -> None: + hub_worker.add_orchestrator(child) + hub_worker.add_orchestrator(generator_child) + hub_client.schedule_new_orchestration(child, input=1) + hub_client.schedule_new_orchestration(generator_child, input=1) + + +async def check_cold_operations( + ctx: task.AsyncOrchestrationContext, entity: EntityInstanceId, + result_type: type[Result] | None = None, +) -> None: + ops = ctx + assert_type(ops, task.AsyncOrchestrationContext) + activity: Coroutine[Any, Any, int] = ops.call_activity(increment, input=1) + assert_type(await activity, int) + assert_type(await ops.call_activity("named", return_type=str), str) + assert_type(await ops.call_activity("named"), Any) + assert_type(await ops.call_sub_orchestrator(child, input=1), Any) + assert_type(await ops.call_sub_orchestrator(child, input=1, return_type=str), str) + assert_type(await ops.call_sub_orchestrator(generator_child, input=1), Any) + assert_type(await ops.call_sub_orchestrator(generator_child, input=1, return_type=str), str) + assert_type(await ops.call_sub_orchestrator(result_child, input=1), Any) + assert_type(await ops.call_sub_orchestrator(result_child, input=1, return_type=None), Any) + assert_type(await ops.call_sub_orchestrator(result_child, input=1, return_type=Result), Result) + assert_type(await ops.call_sub_orchestrator(result_child, input=1, return_type=result_type), Result | Any) + assert_type(await ops.call_sub_orchestrator("child", return_type=Result), Result) + assert_type(await ops.call_sub_orchestrator("child", return_type=int), int) + assert_type(await ops.call_sub_orchestrator("child"), Any) + assert_type(await ops.call_sub_orchestrator("child", return_type=None), Any) + assert_type(await ops.call_sub_orchestrator("child", return_type=result_type), Result | Any) + assert_type(await ops.call_sub_orchestrator(none_child, input=1), Any) + assert_type(await ops.call_sub_orchestrator(none_child, input=1, return_type=type(None)), None) + assert_type(await ops.call_entity(entity, "get", return_type=int), int) + assert_type(await ops.call_entity(entity, "get"), Any) + assert_type(await ops.create_timer(timedelta(seconds=1)), None) + assert_type(await ops.wait_for_external_event("event", data_type=str), str) + assert_type(await ops.wait_for_external_event("event"), Any) + assert_type(await ops.lock_entities([entity]), EntityLock) + assert_type(asyncio.create_task(ops.call_activity(increment, input=1)), asyncio.Task[int]) + async with asyncio.TaskGroup() as group: + assert_type(group.create_task(ops.wait_for_external_event("event", data_type=str)), asyncio.Task[str]) + operation = ctx.as_task_context().call_activity(increment, input=1) + assert_type(operation, task.CompletableTask[int]) + assert_type(await operation.wait(), int) + assert_type(asyncio.create_task(operation.wait()), asyncio.Task[int]) From 7e9a605a2019aac19bde6947520f9488f6d54b42 Mon Sep 17 00:00:00 2001 From: Andy Staples Date: Mon, 30 Nov 2026 16:03:31 -0700 Subject: [PATCH 2/6] Close unstarted async operations inside the dispatch cleanup boundary Unstarted operations are retained until teardown and closed on the durable loop, including when an async wrapper returns no coroutine. Event timeouts are validated before a buffered event is consumed, and the docs explain how create_task queues an operation and who owns a race's losing handle. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- docs/async-orchestrators.md | 27 ++++-- durabletask/async_context.py | 39 +++++---- durabletask/internal/asyncio_loop.py | 40 +++++---- durabletask/worker.py | 16 ++-- tests/durabletask/test_async_context.py | 86 +++++++++++++++++++ .../durabletask/test_asyncio_orchestrators.py | 11 +-- 6 files changed, 167 insertions(+), 52 deletions(-) diff --git a/docs/async-orchestrators.md b/docs/async-orchestrators.md index 92d08c37..69020dd5 100644 --- a/docs/async-orchestrators.md +++ b/docs/async-orchestrators.md @@ -107,13 +107,15 @@ its first step starts no durable operation. Operation inputs are not copied: mutations made before the coroutine's first step are observed, and relative timer delays start from that execution point. Keep or copy inputs accordingly. Dropping an operation without awaiting it or wrapping it in a task schedules -nothing, and Python reports that the coroutine was never awaited. +nothing. Dispatch teardown closes every operation that has not started yet, +so Python does not report it as never awaited. 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 -with `asyncio.create_task(...)`: +scheduled at its first await, not where it was created. To start an +operation concurrently, wrap it with `asyncio.create_task(...)`. The task is +queued rather than run inline: its durable operation is scheduled the next +time the orchestrator yields to the loop. In this example the timer is +scheduled first and the report activity second: ```python pending = asyncio.create_task(ctx.call_activity("report", return_type=str)) @@ -121,6 +123,10 @@ await ctx.create_timer(timedelta(minutes=5)) summary = await pending ``` +When the activity must be scheduled before the next statement, use +`ctx.as_task_context().call_activity(...)`, which schedules immediately and +returns a durable handle you can await later. + Cold coroutines are single-use, like ordinary Python coroutines. For an explicitly started operation with multiple independent waiters, retain its durable handle: @@ -148,16 +154,21 @@ approval = eager.wait_for_external_event("approval", data_type=bool) deadline = eager.create_timer(timedelta(hours=1)) winner = await task.when_any([approval, deadline]) # The identical handle wins. if winner is approval: - deadline.cancel() + deadline.cancel() # Release the losing timer. approved = approval.get_result() else: + approval.cancel() # Withdraw the losing wait so it cannot take a later event. report = asyncio.create_task(ctx.call_activity("report", return_type=str)) finished = report.done() # Completion check without waiting. summary = await report # Result, or the operation's failure. ``` -The losing handle stays inspectable through `is_complete` and, once complete, -`get_result()`. +The race owns its losing handle. An abandoned event wait stays first in line +and consumes the next matching event, so cancel it when the timer wins, and +cancel the timer when the event wins. For this event-with-deadline pattern, +`await ctx.wait_for_external_event(..., timeout=...)` withdraws both +automatically. A losing handle that is not cancelled stays inspectable through +`is_complete` and, once complete, `get_result()`. The worker classifies each orchestrator once, when it is registered: diff --git a/durabletask/async_context.py b/durabletask/async_context.py index 8ba0c762..8d405a70 100644 --- a/durabletask/async_context.py +++ b/durabletask/async_context.py @@ -9,9 +9,7 @@ import inspect import logging import sys -import weakref -from collections.abc import Callable, Coroutine, Generator -from contextlib import contextmanager +from collections.abc import Callable, Coroutine from datetime import datetime, timedelta from typing import Any, Concatenate, ParamSpec, TypeVar, overload @@ -27,11 +25,11 @@ def _cold_operation( operation: Callable[Concatenate[AsyncOrchestrationContext, P], Coroutine[Any, Any, R]], ) -> Callable[Concatenate[AsyncOrchestrationContext, P], Coroutine[Any, Any, R]]: - """Weakly track each operation coroutine so dispatch teardown can close unstarted ones.""" + """Track each operation coroutine so dispatch teardown can close unstarted ones.""" @functools.wraps(operation) def create(self: AsyncOrchestrationContext, /, *args: P.args, **kwargs: P.kwargs) -> Coroutine[Any, Any, R]: coroutine = operation(self, *args, **kwargs) - self._operations.add(coroutine) # pyright: ignore[reportPrivateUsage] + self._track_operation(coroutine) # pyright: ignore[reportPrivateUsage] return coroutine if sys.version_info >= (3, 12): @@ -59,21 +57,27 @@ class AsyncOrchestrationContext: def __init__(self, context: task.OrchestrationContext) -> None: self._context = context - self._operations: weakref.WeakSet[Coroutine[Any, Any, Any]] = weakref.WeakSet() + # Strong references keep an unstarted operation from being finalized, + # and reported as never awaited, before dispatch teardown closes it. + self._operations: list[Coroutine[Any, Any, Any]] = [] + self._operations_prune_at = 64 - @contextmanager - def _closing_unstarted_operations(self) -> Generator[None, None, None]: - """Close operation coroutines that never started once dispatch teardown ends. + def _track_operation(self, operation: Coroutine[Any, Any, Any]) -> None: + if len(self._operations) >= self._operations_prune_at: + # Started operations need no teardown; amortized pruning bounds retention. + self._operations = [tracked for tracked in self._operations if _is_unstarted(tracked)] + self._operations_prune_at = max(64, 2 * len(self._operations)) + self._operations.append(operation) + + def _close_unstarted_operations(self) -> None: + """Close operation coroutines that never started, during dispatch teardown. An unstarted coroutine has run none of its body, so closing it schedules - nothing. Retaining them during teardown keeps the cancelled orchestrator - from finalizing them first and reporting them as never awaited. + nothing; releasing its arguments can still run their finalizers. """ - unstarted = [operation for operation in self._operations if _is_unstarted(operation)] - try: - yield - finally: - for operation in (*unstarted, *self._operations): + while self._operations: + operations, self._operations = self._operations, [] + for operation in operations: if _is_unstarted(operation): operation.close() @@ -318,6 +322,9 @@ async def wait_for_external_event( """ from durabletask.task import when_any + # Validate before touching the event buffer so a bad call consumes nothing. + if timeout is not None and not isinstance(timeout, timedelta): # pyright: ignore[reportUnnecessaryIsInstance] + raise TypeError(f"timeout must be a timedelta or None, not {type(timeout).__name__}.") event: task.CancellableTask[Any] = self._context.wait_for_external_event(name, data_type=data_type) timer: task.TimerTask | None = None try: diff --git a/durabletask/internal/asyncio_loop.py b/durabletask/internal/asyncio_loop.py index dddefe4d..e9e5c58e 100644 --- a/durabletask/internal/asyncio_loop.py +++ b/durabletask/internal/asyncio_loop.py @@ -569,7 +569,8 @@ async def _aclose_asyncgen(self, generator: Any, closed: dict[int, Any]) -> None closed.pop(id(generator), None) self._asyncgens[id(generator)] = generator - def close(self) -> None: + def close(self, finalize: Callable[[], None] | None = None) -> None: + """Tear down the dispatch's local tasks, then run *finalize* on this loop.""" if self.is_running(): # Orchestrator code always runs inside the loop; only the worker may close it. raise RuntimeError(f"loop.close() is not supported in durable orchestrators. {_LOOP_HINT}") @@ -598,23 +599,28 @@ def close(self) -> None: self._close_asyncgens(closed_asyncgens) failure: BaseException | None = self._cleanup_failure self._forcing = True - # Forced closes run finalizers, so keep this loop and its hooks installed. + # Forced closes and finalize() release objects whose finalizers run + # user code, so keep this loop and its hooks installed. with self._activated(): - for future in tuple(self._tasks): - if not future.done(): - try: - self._force_close(cast(_DurableAsyncTask[Any], future)) - except BaseException as error: - # Deliberately broad: a finalizer can raise SystemExit, - # KeyboardInterrupt, or GeneratorExit. Record it (process-control - # exits take precedence) and keep closing the remaining tasks; - # close() re-raises the recorded failure after cleanup finishes. - self._record_cleanup_error(error, "coroutine cleanup") - failure = failure or error - failure = failure or RuntimeError( - "Async cleanup must finish without waiting for durable or external work.") - elif future not in completed_at_close: - self._record_task_failure(future) + try: + for future in tuple(self._tasks): + if not future.done(): + try: + self._force_close(cast(_DurableAsyncTask[Any], future)) + except BaseException as error: + # Deliberately broad: a finalizer can raise SystemExit, + # KeyboardInterrupt, or GeneratorExit. Record it (process-control + # exits take precedence) and keep closing the remaining tasks; + # close() re-raises the recorded failure after cleanup finishes. + self._record_cleanup_error(error, "coroutine cleanup") + failure = failure or error + failure = failure or RuntimeError( + "Async cleanup must finish without waiting for durable or external work.") + elif future not in completed_at_close: + self._record_task_failure(future) + finally: + if finalize is not None: + finalize() if self._cleanup_failure is not None: raise self._cleanup_failure if failure is not None: diff --git a/durabletask/worker.py b/durabletask/worker.py index 442f7e9e..9b9bf813 100644 --- a/durabletask/worker.py +++ b/durabletask/worker.py @@ -1686,13 +1686,17 @@ def run(self, orchestrator: Generator[task.Task[Any], Any, Any] | Coroutine[Any, self._set_next_task(orchestrator.send(None)) def close(self) -> None: - if self._async_loop is not None: + if self._async_loop is not None or self._async_context is not None: self._async_cleanup = True - if self._async_context is None: - self._async_loop.close() - return - with self._async_context._closing_unstarted_operations(): # pyright: ignore[reportPrivateUsage] - self._async_loop.close() + loop = self._async_loop + if loop is None: + # The orchestrator returned no coroutine; a loop still provides + # the cleanup boundary for operations it created. + loop = DurableEventLoop(self, is_complete=lambda: self._is_complete, logger=self._logger) + async_context = self._async_context + loop.close( + async_context._close_unstarted_operations # pyright: ignore[reportPrivateUsage] + if async_context is not None else None) elif self._generator is not None: self._generator.close() diff --git a/tests/durabletask/test_async_context.py b/tests/durabletask/test_async_context.py index 60587840..4aebb298 100644 --- a/tests/durabletask/test_async_context.py +++ b/tests/durabletask/test_async_context.py @@ -4,6 +4,7 @@ """Cold coroutine and ownership semantics through the shared replay executor.""" import asyncio +import functools import gc import inspect import json @@ -649,3 +650,88 @@ def eager(ctx: task.OrchestrationContext, _: Any) -> Generator[task.Task[Any], A old += fired + [scheduled] complete = _completion(_execute(cold, old, [completed])) assert json.loads(complete.result.value) == 42 + + +@pytest.mark.parametrize("outcome", ["raise", "value"]) +def test_operations_from_async_wrapper_without_coroutine_are_closed(outcome: str) -> None: + created: list[Coroutine[Any, Any, Any]] = [] + + async def target(ctx: task.AsyncOrchestrationContext, _: Any) -> None: + pass + + def body(ctx: task.AsyncOrchestrationContext, _: Any) -> int: + created.append(ctx.call_activity("work")) + if outcome == "raise": + raise ValueError("wrapper failed") + return 42 + + # A C-implemented wrapper is classified by the async function it names. + wrapper = functools.lru_cache(maxsize=None)(body) + wrapper.__wrapped__ = target # type: ignore[attr-defined] + complete = _completion(_execute(wrapper, [], _start())) + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_FAILED + assert inspect.getcoroutinestate(created[0]) == inspect.CORO_CLOSED + + +@pytest.mark.filterwarnings("error::RuntimeWarning") +def test_unused_cold_operation_is_closed_without_warning(monkeypatch: pytest.MonkeyPatch) -> None: + unraisable: list[Any] = [] + monkeypatch.setattr(sys, "unraisablehook", unraisable.append) + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: + pending = ctx.call_activity("never-started") # noqa: F841 + return "done" + + result = _execute(orchestrator, [], _start()) + gc.collect() + assert json.loads(_completion(result).result.value) == "done" + assert not [action for action in result.actions if action.HasField("scheduleTask")] + assert not unraisable + + +def test_unstarted_operation_finalizers_run_on_durable_loop_under_host_loop() -> None: + from durabletask.internal.asyncio_loop import DurableEventLoop + + observed: list[bool] = [] + escaped: list[str] = [] + + class Probe: + def __del__(self) -> None: + loop = asyncio.get_running_loop() + observed.append(isinstance(loop, DurableEventLoop)) + loop.call_soon(escaped.append, "callback") + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: + pending = ctx.call_activity("work", input=Probe()) # noqa: F841 + await ctx.wait_for_external_event("never") + + async def host() -> None: + assert _execute(orchestrator, [], _start()).actions == [] + await asyncio.sleep(0) + await asyncio.sleep(0) + + asyncio.run(host()) + assert observed == [True] + assert escaped == [] + + +@pytest.mark.parametrize("first", ["value", "go"]) +def test_event_timeout_is_validated_before_buffered_event_is_consumed(first: str) -> None: + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[Any]: + observed: list[Any] = [] + await ctx.wait_for_external_event("go") + try: + await ctx.wait_for_external_event("value", timeout="typo") # type: ignore[arg-type] + except TypeError as error: + observed.append(str(error)) + observed.append(await ctx.wait_for_external_event("value")) + return observed + + events = { + "value": helpers.new_event_raised_event("value", '"payload"'), + "go": helpers.new_event_raised_event("go", "null"), + } + second = "go" if first == "value" else "value" + complete = _completion(_execute(orchestrator, [], _start() + [events[first], events[second]])) + assert json.loads(complete.result.value) == [ + "timeout must be a timedelta or None, not str.", "payload"] diff --git a/tests/durabletask/test_asyncio_orchestrators.py b/tests/durabletask/test_asyncio_orchestrators.py index a3ae2834..2d44efd1 100644 --- a/tests/durabletask/test_asyncio_orchestrators.py +++ b/tests/durabletask/test_asyncio_orchestrators.py @@ -1149,16 +1149,17 @@ async def child(name: str) -> None: try: await ctx.wait_for_external_event("never") finally: - ctx.call_activity(f"{name}-finally") + eager.call_activity(f"{name}-finally") + eager = ctx.as_task_context() first = asyncio.create_task(child("first")) second = asyncio.create_task(child("second")) await ctx.call_activity("start") first.cancel() if schedule == "call_soon": - loop.call_soon(ctx.call_activity, "callback") + loop.call_soon(eager.call_activity, "callback") else: - loop.call_later(0, ctx.call_activity, "callback") + loop.call_later(0, eager.call_activity, "callback") # A later cancellation must not overtake the callback queued before it. second.cancel() for branch in (first, second): @@ -1204,7 +1205,7 @@ def test_shared_handle_waiter_removal_is_constant_time( _CountingFuture.comparisons = 0 async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> list[int]: - shared = ctx.call_activity("shared", return_type=int) + shared = ctx.as_task_context().call_activity("shared", return_type=int) abandoned = [asyncio.create_task(shared.wait()) for _ in range(count)] await asyncio.sleep(0) # Withdraw the newest waiter first, one step at a time. @@ -1747,7 +1748,7 @@ async def branch() -> None: await ctx.wait_for_external_event("never") finally: try: - ctx.wait_for_external_event("approval") + ctx.as_task_context().wait_for_external_event("approval") except RuntimeError as error: errors.append(str(error)) From c48fb2d718fd8aa20739200c918073caefae0f58 Mon Sep 17 00:00:00 2001 From: Andy Staples Date: Mon, 30 Nov 2026 16:26:30 -0700 Subject: [PATCH 3/6] Release unstarted operation arguments during teardown on Python 3.12 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- durabletask/async_context.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/durabletask/async_context.py b/durabletask/async_context.py index 8d405a70..75011073 100644 --- a/durabletask/async_context.py +++ b/durabletask/async_context.py @@ -79,7 +79,13 @@ def _close_unstarted_operations(self) -> None: operations, self._operations = self._operations, [] for operation in operations: if _is_unstarted(operation): - operation.close() + # Python 3.12's close() keeps an unstarted coroutine's + # arguments alive until it is freed. Throwing GeneratorExit + # releases them on every version and runs none of its body. + try: + operation.throw(GeneratorExit) + except GeneratorExit: + pass @property def instance_id(self) -> str: From fa3432815ccf9e0cea4ba80e952eb659619adce5 Mon Sep 17 00:00:00 2001 From: Andy Staples Date: Mon, 30 Nov 2026 16:43:13 -0700 Subject: [PATCH 4/6] Name the never-awaited operations in teardown tests explicitly Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- tests/durabletask/test_async_context.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/durabletask/test_async_context.py b/tests/durabletask/test_async_context.py index 4aebb298..6b981d6a 100644 --- a/tests/durabletask/test_async_context.py +++ b/tests/durabletask/test_async_context.py @@ -679,7 +679,8 @@ def test_unused_cold_operation_is_closed_without_warning(monkeypatch: pytest.Mon monkeypatch.setattr(sys, "unraisablehook", unraisable.append) async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: - pending = ctx.call_activity("never-started") # noqa: F841 + # Created and kept in a local, but never awaited: teardown must close it. + _pending = ctx.call_activity("never-started") # noqa: F841 return "done" result = _execute(orchestrator, [], _start()) @@ -702,7 +703,8 @@ def __del__(self) -> None: loop.call_soon(escaped.append, "callback") async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: - pending = ctx.call_activity("work", input=Probe()) # noqa: F841 + # Created and kept in a local, but never awaited: teardown must close it. + _pending = ctx.call_activity("work", input=Probe()) # noqa: F841 await ctx.wait_for_external_event("never") async def host() -> None: From ed5335ea05f589e525653a4f9cc50fc38feca23b Mon Sep 17 00:00:00 2001 From: Andy Staples Date: Mon, 30 Nov 2026 16:55:16 -0700 Subject: [PATCH 5/6] Assert that never-awaited operations in teardown tests are unstarted Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- tests/durabletask/test_async_context.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/durabletask/test_async_context.py b/tests/durabletask/test_async_context.py index 6b981d6a..c7d43cf3 100644 --- a/tests/durabletask/test_async_context.py +++ b/tests/durabletask/test_async_context.py @@ -680,7 +680,8 @@ def test_unused_cold_operation_is_closed_without_warning(monkeypatch: pytest.Mon async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> str: # Created and kept in a local, but never awaited: teardown must close it. - _pending = ctx.call_activity("never-started") # noqa: F841 + pending = ctx.call_activity("never-started") + assert inspect.getcoroutinestate(pending) == inspect.CORO_CREATED return "done" result = _execute(orchestrator, [], _start()) @@ -704,7 +705,8 @@ def __del__(self) -> None: async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: # Created and kept in a local, but never awaited: teardown must close it. - _pending = ctx.call_activity("work", input=Probe()) # noqa: F841 + pending = ctx.call_activity("work", input=Probe()) + assert inspect.getcoroutinestate(pending) == inspect.CORO_CREATED await ctx.wait_for_external_event("never") async def host() -> None: From 215f95003c8108d38016e56e0bff3b3cf7d0fb5e Mon Sep 17 00:00:00 2001 From: Andy Staples Date: Tue, 1 Dec 2026 10:48:08 -0700 Subject: [PATCH 6/6] Refuse new cold operations once the unstarted-operation sweep begins Finalizers released by the sweep can no longer allocate and track further operations, so the sweep is a single bounded pass. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- docs/async-orchestrators.md | 4 ++- durabletask/async_context.py | 30 ++++++++++++-------- tests/durabletask/test_async_context.py | 37 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/docs/async-orchestrators.md b/docs/async-orchestrators.md index 69020dd5..fc6bef93 100644 --- a/docs/async-orchestrators.md +++ b/docs/async-orchestrators.md @@ -108,7 +108,9 @@ mutations made before the coroutine's first step are observed, and relative timer delays start from that execution point. Keep or copy inputs accordingly. Dropping an operation without awaiting it or wrapping it in a task schedules nothing. Dispatch teardown closes every operation that has not started yet, -so Python does not report it as never awaited. +so Python does not report it as never awaited. Creating an operation while +that sweep runs, such as from a finalizer of a released argument, raises +`RuntimeError`. An operation can be created before other awaits and awaited later; it is scheduled at its first await, not where it was created. To start an diff --git a/durabletask/async_context.py b/durabletask/async_context.py index 75011073..112d624a 100644 --- a/durabletask/async_context.py +++ b/durabletask/async_context.py @@ -28,6 +28,10 @@ def _cold_operation( """Track each operation coroutine so dispatch teardown can close unstarted ones.""" @functools.wraps(operation) def create(self: AsyncOrchestrationContext, /, *args: P.args, **kwargs: P.kwargs) -> Coroutine[Any, Any, R]: + if self._operations_closed: # pyright: ignore[reportPrivateUsage] + # Refuse before allocating a coroutine that nothing would await or close. + raise RuntimeError( + "Dispatch cleanup cannot create durable operations; use normal orchestrator control flow.") coroutine = operation(self, *args, **kwargs) self._track_operation(coroutine) # pyright: ignore[reportPrivateUsage] return coroutine @@ -61,6 +65,7 @@ def __init__(self, context: task.OrchestrationContext) -> None: # and reported as never awaited, before dispatch teardown closes it. self._operations: list[Coroutine[Any, Any, Any]] = [] self._operations_prune_at = 64 + self._operations_closed = False def _track_operation(self, operation: Coroutine[Any, Any, Any]) -> None: if len(self._operations) >= self._operations_prune_at: @@ -73,19 +78,20 @@ def _close_unstarted_operations(self) -> None: """Close operation coroutines that never started, during dispatch teardown. An unstarted coroutine has run none of its body, so closing it schedules - nothing; releasing its arguments can still run their finalizers. + nothing; releasing its arguments can still run their finalizers. New + operations are refused first, so those finalizers cannot extend the sweep. """ - while self._operations: - operations, self._operations = self._operations, [] - for operation in operations: - if _is_unstarted(operation): - # Python 3.12's close() keeps an unstarted coroutine's - # arguments alive until it is freed. Throwing GeneratorExit - # releases them on every version and runs none of its body. - try: - operation.throw(GeneratorExit) - except GeneratorExit: - pass + self._operations_closed = True + operations, self._operations = self._operations, [] + for operation in operations: + if _is_unstarted(operation): + # Python 3.12's close() keeps an unstarted coroutine's + # arguments alive until it is freed. Throwing GeneratorExit + # releases them on every version and runs none of its body. + try: + operation.throw(GeneratorExit) + except GeneratorExit: + pass @property def instance_id(self) -> str: diff --git a/tests/durabletask/test_async_context.py b/tests/durabletask/test_async_context.py index c7d43cf3..d5c6610d 100644 --- a/tests/durabletask/test_async_context.py +++ b/tests/durabletask/test_async_context.py @@ -719,6 +719,43 @@ async def host() -> None: assert escaped == [] +@pytest.mark.filterwarnings("error::RuntimeWarning") +def test_unstarted_operation_finalizers_cannot_extend_teardown(monkeypatch: pytest.MonkeyPatch) -> None: + unraisable: list[Any] = [] + monkeypatch.setattr(sys, "unraisablehook", unraisable.append) + finalized: list[int] = [] + refused: list[str] = [] + context: list[task.AsyncOrchestrationContext] = [] + + class Link: + def __init__(self, remaining: int) -> None: + self.remaining = remaining + + def __del__(self) -> None: + finalized.append(self.remaining) + if self.remaining: + try: + context[0].call_activity("work", input=Link(self.remaining - 1)) + except RuntimeError as error: + refused.append(str(error)) + + async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> None: + context.append(ctx) + pending = ctx.call_activity("work", input=Link(7)) + assert inspect.getcoroutinestate(pending) == inspect.CORO_CREATED + await ctx.wait_for_external_event("never") + + assert _execute(orchestrator, [], _start()).actions == [] + gc.collect() + # Each finalizer's argument is still released, but the sweep refuses every + # follow-up operation before allocating it, so it closes only the original. + assert finalized == list(range(7, -1, -1)) + assert len(refused) == 7 + assert all("Dispatch cleanup cannot create durable operations" in error for error in refused) + assert context[0]._operations == [] # pyright: ignore[reportPrivateUsage] + assert not unraisable + + @pytest.mark.parametrize("first", ["value", "go"]) def test_event_timeout_is_validated_before_buffered_event_is_consumed(first: str) -> None: async def orchestrator(ctx: task.AsyncOrchestrationContext, _: Any) -> list[Any]: