diff --git a/.github/workflows/durabletask.yml b/.github/workflows/durabletask.yml index 57fa5635..273f9e0a 100644 --- a/.github/workflows/durabletask.yml +++ b/.github/workflows/durabletask.yml @@ -14,6 +14,30 @@ permissions: contents: read jobs: + async-runtime-windows: + strategy: + fail-fast: false + matrix: + python-version: ["3.11", "3.12", "3.13", "3.14", "3.15"] + runs-on: windows-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: ${{ matrix.python-version }} + - name: Install runtime test dependencies + run: | + python -m pip install --upgrade "pip>=25.1" + python -m pip install --group test -e . + - name: Check asyncio, replay, and dispatch cleanup + run: >- + python -m pytest + tests\durabletask\test_asyncio_orchestrators.py + tests\durabletask\test_coroutine_orchestrators.py + tests\durabletask\test_orchestration_generator_cleanup.py + tests\durabletask\test_orchestration_dispatch_recovery.py + -q + lint-and-unit-tests: runs-on: ubuntu-latest steps: diff --git a/docs/async-orchestrators.md b/docs/async-orchestrators.md index 4845a64c..b1767522 100644 --- a/docs/async-orchestrators.md +++ b/docs/async-orchestrators.md @@ -320,13 +320,27 @@ Every dispatch reconstructs the coroutine and its local tasks from history. After the response and entity-release obligations are snapshotted, teardown cancels remaining local tasks, drains cooperative cleanup, closes tracked async generators, and disposes the loop. This includes dispatches that suspend, -continue as new, encounter a history failure, or reject a version. +continue as new, encounter a history failure, or reject a version. An async +generator whose `anext()` is still in progress is closed after that step +finishes. A coroutine that ignores cancellation is closed last, still on the +dispatch loop and in its task's context; it cannot start a new async generator +there, because no later sweep could close it. Local cleanup can await ready work such as `sleep(0)`; it must not wait for new durable or external work. -Scheduling/awaiting durable work during async teardown raises an actionable -error. Teardown must cooperate with cancellation; a non-quiescent cleanup -queue is bounded and reported, not allowed to run indefinitely. +Scheduling/awaiting durable work, or waiting for or receiving an external +event, during async teardown raises an actionable error. Teardown must +cooperate with cancellation. + +> [!WARNING] +> Async teardown is capped at about 10,000 event-loop steps per cleanup phase +> (more for dispatches with many tasks or async generators). Cleanup that is +> still running when the cap is reached is truncated: remaining callbacks are +> dropped, unfinished coroutines are closed, and a warning is logged. The +> response decided before teardown is unchanged. Each async-generator sweep's +> budget is fixed at sweep entry; tasks or generators created during that sweep +> cannot increase it. The cap bounds cooperative scheduling steps, not +> wall-clock time or code that never yields. Ordinary teardown failures (including `CancelledError`) are logged without changing the decided response or skipping other cooperative finalizers. @@ -334,13 +348,20 @@ Unexpected non-cancellation `BaseException` exits retain the existing terminal-failure/recovery policy; an existing primary failure is preserved. Teardown status/actions do not replace the response. Held entity-release obligations are captured before teardown for terminal -payload/failure handling. +payload/failure handling. Exiting an entity critical section during teardown +does not schedule additional release messages, and a `SystemExit` or +`KeyboardInterrupt` raised by any finalizer takes precedence over other +cleanup failures. Keep workflow cleanup that must be durable in **normal orchestration control flow**, before return or continue-as-new, not solely in a dispatch finalizer. No cleanup is guaranteed after process crash or forced termination. Async-generator *helpers* are closed; async-generator *orchestrators* -(`async def` containing `yield`) are rejected. +(`async def` containing `yield`) are rejected. The dispatch owns every async +generator it starts until teardown, including generators first iterated by +another generator's cleanup. To run a generator's cleanup earlier, close it +explicitly with `await gen.aclose()` or iterate it inside +`contextlib.aclosing(...)`. ## Replay and framework adapters diff --git a/durabletask/internal/asyncio_loop.py b/durabletask/internal/asyncio_loop.py index 44b218fb..dddefe4d 100644 --- a/durabletask/internal/asyncio_loop.py +++ b/durabletask/internal/asyncio_loop.py @@ -11,9 +11,11 @@ detect durable, foreign, or absent loops without raising. - ``asyncio.tasks._enter_task`` / ``asyncio.tasks._leave_task``: suspend and restore the host's current task while draining, so asyncio does not see two - tasks running on one thread. + tasks running on one thread, and mark a task current while its coroutine is + force-closed. - ``asyncio.Handle._run``: run a ready callback with asyncio's own context and - exception-handler semantics. + exception-handler semantics. ``asyncio.Handle._callback`` identifies the + task a callback steps, so cleanup failures are recorded in execution order. - ``asyncio.Task._log_traceback``: report unobserved local-task failures during dispatch cleanup without marking them as retrieved, and keep such tasks tracked until then. @@ -28,9 +30,11 @@ from __future__ import annotations import asyncio +import contextlib import contextvars import logging import math +import sys from collections import deque from collections.abc import Callable, Coroutine, Generator from datetime import UTC, datetime, timedelta @@ -45,6 +49,13 @@ _MICROSECOND = timedelta(microseconds=1) +def _is_process_control(error: BaseException | None) -> bool: + if isinstance(error, BaseExceptionGroup): + group = cast(BaseExceptionGroup[BaseException], error) + return group.subgroup((KeyboardInterrupt, SystemExit)) is not None + return isinstance(error, (KeyboardInterrupt, SystemExit)) + + class _CompletionObserver(task.CompositeTask[None]): def __init__(self, durable: task.Task[Any], callback: Callable[[], None]) -> None: self._callback = callback @@ -67,7 +78,9 @@ def __init__( context: contextvars.Context | None, ) -> None: self._sequence = sequence - super().__init__(coro, loop=loop, name=name, context=context) + # Task.get_context() is Python 3.12+; keep the context for forced closes. + self.durable_context = context if context is not None else contextvars.copy_context() + super().__init__(coro, loop=loop, name=name, context=self.durable_context) def __hash__(self) -> int: # Load-bearing for replay determinism: TaskGroup keeps its children in a @@ -169,9 +182,16 @@ def __init__( self._observers: dict[int, _CompletionObserver] = {} self._completions: deque[Callable[[], None]] = deque() self._timers: dict[_DurableTimerHandle, _CompletionObserver] = {} + self._asyncgens: dict[int, Any] = {} + self._asyncgen_prune_at = 64 self._closed = False self._closing = False + # Set once cleanup force-closes tasks; no later sweep can close new work. + self._forcing = False self._fatal: BaseException | None = None + self._cleanup_failure: BaseException | None = None + self._cleanup_truncated = False + self._reported_tasks: set[asyncio.Task[Any]] = set() self._root: asyncio.Task[Any] | None = None def _now(self) -> datetime: @@ -305,6 +325,8 @@ def _complete_timer(self, handle: _DurableTimerHandle) -> None: self._ready.append(handle) async def wait_durable(self, durable: task.Task[T]) -> T: + if self._closing: + raise RuntimeError("Dispatch cleanup cannot await durable work.") if durable.is_complete: return durable.get_result() future = self.create_future() @@ -354,7 +376,8 @@ def resume(self) -> None: raise task.TaskCancelledError( "The orchestrator coroutine was cancelled. Catch local cancellation " "inside the orchestrator; terminate the durable instance through the client.") - # result() propagates the orchestrator's failure to the executor. + # result() deliberately propagates control-flow BaseExceptions to the + # existing executor recovery boundary, not a success-shaped fallback. result = self._root.result() raise StopIteration(result) if not self._has_durable_work(): @@ -365,13 +388,29 @@ def resume(self) -> None: def _has_durable_work(self) -> bool: return any(not durable.is_complete for durable, _ in self._waits.values()) or bool(self._timers) - def _drain(self) -> None: + @contextlib.contextmanager + def _activated(self) -> Generator[None, None, None]: + """Install this loop, no current host task, and its async-generator hooks.""" previous_loop = asyncio._get_running_loop() previous_task = asyncio.current_task(previous_loop) if previous_loop is not None else None + previous_hooks = sys.get_asyncgen_hooks() if previous_task is not None and previous_loop is not None: asyncio.tasks._leave_task(previous_loop, previous_task) asyncio._set_running_loop(self) + sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter, finalizer=self._asyncgen_finalizer) try: + yield + finally: + sys.set_asyncgen_hooks(*previous_hooks) + asyncio._set_running_loop(previous_loop) + if previous_task is not None and previous_loop is not None: + asyncio.tasks._enter_task(previous_loop, previous_task) + + def _drain(self, cleanup_limit: int | None = None) -> int: + with self._activated(): + steps = 0 + if cleanup_limit is None: + cleanup_limit = self._cleanup_budget() while self._ready: # Each step's cancellation wakeups form their own batch. self._cancel_batch = None @@ -381,23 +420,72 @@ def _drain(self) -> None: continue handle = item if not handle.cancelled(): - handle._run() + owner = getattr(handle._callback, "__self__", None) + raised: BaseException | None = None + try: + handle._run() + except BaseException as error: + if not self._closing: + raise + # Defer all exits until other cleanup tasks have drained. + raised = error + self._record_cleanup_error(error) + if self._closing and isinstance(owner, _DurableAsyncTask): + self._record_task_failure(cast(_DurableAsyncTask[Any], owner), raised) if self._fatal is not None: error, self._fatal = self._fatal, None if not self._closing: raise error - self._logger.warning( - "%s: Async cleanup callback failed.", self.ctx.instance_id, exc_info=error) + self._record_cleanup_error(error) + if not self._closing and self._is_complete(): + break + if not self._closing and self._root is not None and self._root.done(): + break if not self._closing: - if self._is_complete(): - break - if self._root is not None and self._root.done(): - break self._deliver_completions() + steps += 1 + if self._closing and steps >= cleanup_limit: + # Reaching the budget on the step that empties the queue is not truncation. + if self._ready: + self._report_unquiesced_cleanup() + break + return steps + + def _record_task_failure(self, future: asyncio.Task[Any], raised: BaseException | None = None) -> None: + """Record a task that failed during cleanup when its step finishes, in execution order.""" + if not future.done() or future in self._reported_tasks: + return + self._reported_tasks.add(future) + if not future.cancelled(): + error = future.exception() + if error is not None and error is not raised: + self._record_cleanup_error(error, "task cleanup") + + def _force_close(self, future: _DurableAsyncTask[Any]) -> None: + """Close a task that ignored cancellation, as that task and in its context.""" + asyncio.tasks._enter_task(self, future) + try: + future.durable_context.run(future.get_coro().close) finally: - asyncio._set_running_loop(previous_loop) - if previous_task is not None and previous_loop is not None: - asyncio.tasks._enter_task(previous_loop, previous_task) + asyncio.tasks._leave_task(self, future) + + def _asyncgen_firstiter(self, generator: Any) -> None: + if self._forcing: + # Refuse before the generator opens a resource that nothing could close. + raise RuntimeError( + "Async generators cannot start while dispatch cleanup force-closes " + "tasks that ignored cancellation.") + if len(self._asyncgens) >= self._asyncgen_prune_at: + # Exhausted generators have no cleanup left; amortized pruning bounds retention. + self._asyncgens = { + key: value for key, value in self._asyncgens.items() if value.ag_frame is not None} + self._asyncgen_prune_at = max(64, 2 * len(self._asyncgens)) + self._asyncgens[id(generator)] = generator + + def _asyncgen_finalizer(self, generator: Any) -> None: + # A task created during forced close would never run. + if not self._closed and not self._forcing: + self.create_task(generator.aclose()) def call_exception_handler(self, context: dict[str, Any]) -> None: error = context.get("exception") @@ -407,26 +495,128 @@ def call_exception_handler(self, context: dict[str, Any]) -> None: error = failure self._fatal = error if isinstance(error, BaseException) else RuntimeError(context["message"]) + def _cleanup_budget(self) -> int: + # Cooperative cleanup is capped in scheduling steps, not wall-clock time. + return max(10000, 4 * (len(self._tasks) + len(self._asyncgens))) + + def _report_unquiesced_cleanup(self) -> None: + self._ready.clear() + self._cancel_batch = None + if not self._cleanup_truncated: + self._cleanup_truncated = True + self._record_cleanup_error(RuntimeError( + "Async cleanup did not quiesce; cleanup must cooperate with cancellation.")) + + def _record_cleanup_error(self, error: BaseException, source: str = "cleanup callback") -> None: + self._logger.warning("%s: Async %s failed.", self.ctx.instance_id, source, exc_info=error) + if not isinstance(error, (Exception, asyncio.CancelledError)): + if _is_process_control(self._cleanup_failure): + return + # Keep the first failure unless a process-control exit supersedes it. + if self._cleanup_failure is None or _is_process_control(error): + self._cleanup_failure = error + + def _close_asyncgens(self, closed: dict[int, Any]) -> None: + # Closing one generator may first-iterate another; sweep until no new generators appear. + # Scheduled generators leave the registry, so each pass inspects only new discoveries, + # but stay referenced in `closed` so their ids cannot be reused during cleanup. + steps = 0 + # Cleanup-created tasks and generators must not replenish the sweep's budget. + limit = self._cleanup_budget() + # aclose() fails while an anext() is in progress, so running generators wait here. + deferred: dict[int, Any] = {} + + def ready(candidates: dict[int, Any]) -> list[Any]: + found: list[Any] = [] + for key, generator in candidates.items(): + if closed.get(key) is generator: + continue + if generator.ag_running: + deferred[key] = generator + else: + deferred.pop(key, None) + found.append(generator) + return found + + while True: + discovered, self._asyncgens = self._asyncgens, {} + pending = ready(discovered) + if not pending and deferred and steps < limit: + # Retry running generators only once no new generator is ready, and + # charge the retry to the budget so it cannot repeat without bound. + steps += len(deferred) + pending = ready(dict(deferred)) + if not pending or steps >= limit: + # Leave unscheduled generators registered for a later sweep. + self._asyncgens.update(deferred) + if pending: + self._asyncgens.update((id(generator), generator) for generator in pending) + self._report_unquiesced_cleanup() + return + for generator in pending: + closed[id(generator)] = generator + self.create_task(self._aclose_asyncgen(generator, closed)) + # Each drain spends from this sweep's remaining budget. + steps += max(1, self._drain(limit - steps)) + + async def _aclose_asyncgen(self, generator: Any, closed: dict[int, Any]) -> None: + try: + await generator.aclose() + except RuntimeError: + if not generator.ag_running: + raise + # Another task resumed the generator first; retry once it stops. + closed.pop(id(generator), None) + self._asyncgens[id(generator)] = generator + def close(self) -> None: - """Cancel unfinished coroutines so their cleanup runs before the dispatch ends.""" + 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}") if self._closed: return self._closing = True try: + completed_at_close = {future for future in self._tasks if future.done()} + # Failures of tasks that finished before cleanup happened first. + for future in completed_at_close: + if not future.cancelled() and getattr(future, "_log_traceback", False): + self._reported_tasks.add(future) + error = future.exception() + if error is not None: + self._record_cleanup_error(error, "background task") for future in tuple(self._tasks): if not future.done(): future.cancel() self._drain() - failure: BaseException | None = None + closed_asyncgens: dict[int, Any] = {} + self._close_asyncgens(closed_asyncgens) for future in tuple(self._tasks): if not future.done(): - future.get_coro().close() - failure = failure or RuntimeError( - "Async cleanup must finish without waiting for durable or external work.") - elif not future.cancelled() and getattr(future, "_log_traceback", False): - self._logger.warning( - "%s: Async background task failed.", self.ctx.instance_id, - exc_info=future.exception()) + future.cancel() + self._drain() + 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. + 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) + if self._cleanup_failure is not None: + raise self._cleanup_failure if failure is not None: raise failure finally: @@ -440,6 +630,8 @@ def close(self) -> None: self._waits.clear() self._timers.clear() self._tasks.clear() + self._reported_tasks.clear() + self._asyncgens.clear() _IO_HINT = "External I/O is unsupported in orchestrators; move it to an ordinary activity." diff --git a/durabletask/internal/exceptions.py b/durabletask/internal/exceptions.py index 549a54f1..cfc787f4 100644 --- a/durabletask/internal/exceptions.py +++ b/durabletask/internal/exceptions.py @@ -1,9 +1,20 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. +import asyncio +from typing import cast + import durabletask.internal.orchestrator_service_pb2 as pb +def is_control_flow_exception(error: BaseException) -> bool: + control_flow_types = (asyncio.CancelledError, GeneratorExit, KeyboardInterrupt, SystemExit) + if isinstance(error, BaseExceptionGroup): + group = cast(BaseExceptionGroup[BaseException], error) + return group.subgroup(control_flow_types) is not None + return isinstance(error, control_flow_types) + + class VersionFailureException(Exception): def __init__(self, error_details: pb.TaskFailureDetails) -> None: super().__init__() diff --git a/durabletask/worker.py b/durabletask/worker.py index dc4e5f05..1d116def 100644 --- a/durabletask/worker.py +++ b/durabletask/worker.py @@ -63,14 +63,6 @@ _WorkItem = tuple[Callable[..., Any], Callable[..., Any], tuple[Any, ...], dict[str, Any]] -def _is_control_flow_exception(error: BaseException) -> bool: - control_flow_types = (asyncio.CancelledError, GeneratorExit, KeyboardInterrupt, SystemExit) - if isinstance(error, BaseExceptionGroup): - group = cast(BaseExceptionGroup[BaseException], error) - return group.subgroup(control_flow_types) is not None - return isinstance(error, control_flow_types) - - class ConcurrencyOptions: """Configuration options for controlling concurrency of different work item types and the thread pool size. @@ -1681,6 +1673,7 @@ def __init__(self, self._exception_properties_provider = exception_properties_provider self._logger = logger self._async_loop: DurableEventLoop | None = None + self._async_cleanup = False def run(self, orchestrator: Generator[task.Task[Any], Any, Any] | Coroutine[Any, Any, Any]) -> None: if isinstance(orchestrator, Coroutine): @@ -1693,6 +1686,7 @@ 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() elif self._generator is not None: self._generator.close() @@ -1860,9 +1854,15 @@ def get_actions(self) -> list[pb.OrchestratorAction]: return current_actions def next_sequence_number(self) -> int: + self._check_durable_work_allowed() self._sequence_number += 1 return self._sequence_number + def _check_durable_work_allowed(self) -> None: + """Reject durable scheduling, event waits, and event consumption during async teardown.""" + if self._async_cleanup: + raise RuntimeError("Dispatch cleanup cannot schedule durable work; use normal orchestrator control flow.") + @property def instance_id(self) -> str: return self._instance_id @@ -2249,6 +2249,10 @@ def _exit_critical_section(self) -> None: # _exit_critical_section both from the EntityLock and the continue_as_new logic. We must keep both calls in # case the user code crashes after calling continue_as_new but before the EntityLock object is exited. return + if self._async_cleanup: + # Lock disposal during local teardown is not a durable release; the + # pre-teardown response already carries any release this dispatch owns. + return for entity_unlock_message in self._entity_context.emit_lock_release_messages(): task_id = self.next_sequence_number() action = pb.OrchestratorAction(id=task_id, sendEntityMessage=entity_unlock_message) @@ -2271,6 +2275,8 @@ def wait_for_external_event(self, name: str, *, # event with the given name so that we can resume the generator when it # arrives. If there are multiple events with the same name, we return # them in the order they were received. + # Teardown must neither consume a buffered event nor register a new wait. + self._check_durable_work_allowed() external_event_task: task.CancellableTask[Any] = task.CancellableTask(expected_type=data_type) event_name = name.casefold() event_list = self._received_events.get(event_name, None) @@ -2504,7 +2510,7 @@ def execute( ctx.set_failed(ex) except BaseException as ex: - if _is_control_flow_exception(ex): + if pe.is_control_flow_exception(ex): raise self._logger.error( f"{instance_id}: Orchestrator {orchestration_name} exited unexpectedly.", @@ -2538,7 +2544,7 @@ def execute( result = ExecutionResults( actions=actions, encoded_custom_status=ctx._encoded_custom_status, # pyright: ignore[reportPrivateUsage] orchestration_trace_context=ctx._orchestration_trace_context, # pyright: ignore[reportPrivateUsage] - # Snapshot before generator teardown can exit a critical section. + # Snapshot before orchestrator teardown can exit a critical section. entity_lock_releases=list(ctx._entity_context.get_lock_release_messages()), # pyright: ignore[reportPrivateUsage] ) if unexpected_failure and result.entity_lock_releases: @@ -2571,7 +2577,7 @@ def execute( exc_info=True, ) if not dispatch_exited: - if _is_control_flow_exception(ex) or result is None: + if pe.is_control_flow_exception(ex) or result is None: raise # Keep an existing failure rather than mask its cause. if not any( diff --git a/tests/durabletask/test_asyncio_orchestrators.py b/tests/durabletask/test_asyncio_orchestrators.py index e52531d0..aebea898 100644 --- a/tests/durabletask/test_asyncio_orchestrators.py +++ b/tests/durabletask/test_asyncio_orchestrators.py @@ -10,6 +10,8 @@ import os import subprocess import sys +from collections.abc import AsyncIterator +from contextlib import asynccontextmanager from contextvars import ContextVar from dataclasses import dataclass from datetime import datetime, timedelta @@ -21,7 +23,7 @@ import pytest -from durabletask import task +from durabletask import entities, task from durabletask.internal import helpers from durabletask.internal import orchestrator_service_pb2 as pb from durabletask.internal.asyncio_loop import _UNSUPPORTED_LOOP_APIS, DurableEventLoop # pyright: ignore[reportPrivateUsage] @@ -338,6 +340,49 @@ async def branch(index: int) -> None: assert json.loads(_completion(_execute(orchestrator, old, new)).result.value) == "done" +@pytest.mark.parametrize("under_loop", [False, True]) +def test_dispatch_cleanup_unwinds_scopes_contexts_asyncgens(under_loop: bool) -> None: + cleaned: list[str] = [] + references: list[Any] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + references.extend([ref(ctx), ref(asyncio.get_running_loop()), ref(asyncio.current_task())]) + + async def resource(): + try: + yield 1 + finally: + await asyncio.sleep(0) + cleaned.append("asyncgen") + + opened = resource() + await anext(opened) + try: + async with asyncio.TaskGroup(): + await ctx.call_activity("wait") + finally: + await asyncio.sleep(0) + cleaned.append("root") + + def dispatch() -> None: + result = _execute(orchestrator, [], _start()) + assert [a.scheduleTask.name for a in result.actions] == ["wait"] + assert cleaned == ["root", "asyncgen"] + + async def outer() -> None: + before = asyncio.current_task() + dispatch() + assert asyncio.current_task() is before + await asyncio.sleep(0) + + if under_loop: + asyncio.run(outer()) + else: + dispatch() + gc.collect() + assert all(reference() is None for reference in references) + + 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) @@ -378,6 +423,57 @@ async def waiter() -> None: assert json.loads(_completion(result).result.value) == "cancelled" +@pytest.mark.parametrize("terminal", ["suspend", "terminate", "continue", "history-failure"]) +def test_async_teardown_preserves_response_and_cannot_schedule_work( + terminal: str, caplog: pytest.LogCaptureFixture) -> None: + cleaned: list[str] = [] + + @asynccontextmanager + async def resource(): + try: + yield + finally: + await asyncio.sleep(0) + cleaned.append("resource") + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async with resource(): + try: + ctx.set_custom_status("waiting") + if terminal == "continue": + ctx.continue_as_new("next") + await ctx.call_activity("wait") + finally: + try: + await asyncio.sleep(0) + cleaned.append("inner") + await ctx.call_activity("forbidden-cleanup") + finally: + await asyncio.sleep(0) + cleaned.append("outer") + + old = _start() + if terminal == "terminate": + new = [pb.HistoryEvent(executionTerminated=pb.ExecutionTerminatedEvent())] + elif terminal == "history-failure": + new = [helpers.new_task_scheduled_event(1, "wrong")] + else: + old, new = [], old + result = _execute(orchestrator, old, new) + assert cleaned == ["inner", "outer", "resource"] + assert result.encoded_custom_status == '"waiting"' + assert all(action.scheduleTask.name != "forbidden-cleanup" for action in result.actions) + if terminal == "terminate": + assert _completion(result).orchestrationStatus == pb.ORCHESTRATION_STATUS_TERMINATED + elif terminal == "history-failure": + assert _completion(result).failureDetails.errorType.endswith("NonDeterminismError") + elif terminal == "continue": + assert _completion(result).orchestrationStatus == pb.ORCHESTRATION_STATUS_CONTINUED_AS_NEW + else: + assert [action.scheduleTask.name for action in result.actions] == ["wait"] + assert "Dispatch cleanup cannot schedule durable work" in caplog.text + + def test_unhandled_local_root_cancellation_fails_not_abandons() -> None: async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: raise asyncio.CancelledError @@ -500,6 +596,76 @@ async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: "use an activity", "ordinary activity", "different loop")) +@pytest.mark.parametrize("caught", [False, True]) +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 background() -> None: + try: + await ctx.call_activity("background") + finally: + cleaned.append("background") + + pending = asyncio.create_task(background()) + await asyncio.sleep(0) + try: + asyncio.get_running_loop().close() + except RuntimeError: + if not caught: + raise + assert not pending.done() + return "closed" if asyncio.get_running_loop().is_closed() else "open" + + complete = _completion(_execute(orchestrator, [], _start())) + if caught: + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_COMPLETED + assert json.loads(complete.result.value) == "open" + else: + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_FAILED + assert complete.failureDetails.errorType == "builtins.RuntimeError" + assert complete.failureDetails.errorMessage.startswith( + "loop.close() is not supported in durable orchestrators.") + assert cleaned == ["background"] + assert "cleanup failed" not in caplog.text + + +@pytest.mark.parametrize("async_generator", [False, True]) +@pytest.mark.parametrize("unexpected", [False, True]) +def test_cleanup_failure_policy_includes_async_generator_tasks( + async_generator: bool, unexpected: bool, caplog: pytest.LogCaptureFixture) -> None: + class CleanupAbort(BaseException): + pass + + error = CleanupAbort("cleanup failed") if unexpected else ValueError("cleanup failed") + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def resource(): + try: + yield 1 + finally: + await asyncio.sleep(0) + raise error + + opened = resource() + await anext(opened) + try: + await ctx.call_activity("wait") + finally: + if not async_generator: + await opened.aclose() + + result = _execute(orchestrator, [], _start()) + if unexpected: + complete = _completion(result) + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_FAILED + assert complete.failureDetails.errorMessage == "cleanup failed" + assert complete.failureDetails.errorType.endswith("CleanupAbort") + else: + assert [action.scheduleTask.name for action in result.actions] == ["wait"] + assert "cleanup failed" in caplog.text + + @pytest.mark.parametrize("name", sorted(_UNSUPPORTED_LOOP_APIS)) def test_every_unsupported_loop_api_fails_actionably(name: str) -> None: ctx = cast(task.OrchestrationContext, SimpleNamespace(current_utc_datetime=NOW)) @@ -710,6 +876,80 @@ async def fail() -> None: assert ("background failure" in caplog.text) is not observed +def test_unexpected_cleanup_failure_does_not_hide_other_task_errors( + caplog: pytest.LogCaptureFixture) -> None: + class CleanupAbort(BaseException): + pass + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def branch(error: BaseException) -> None: + try: + await ctx.wait_for_external_event("never") + finally: + await asyncio.sleep(0) + raise error + + asyncio.create_task(branch(CleanupAbort("first cleanup failure"))) + asyncio.create_task(branch(ValueError("second cleanup failure"))) + await ctx.call_activity("wait") + + complete = _completion(_execute(orchestrator, [], _start())) + assert complete.failureDetails.errorMessage == "first cleanup failure" + assert "second cleanup failure" in caplog.text + + +@pytest.mark.parametrize("control_flow", [False, True]) +def test_cleanup_callback_failure_still_drains_tasks_and_asyncgens( + control_flow: bool, caplog: pytest.LogCaptureFixture) -> None: + cleaned: list[str] = [] + retained: list[asyncio.Task[Any]] = [] + + def fail() -> None: + raise SystemExit("cleanup exit") if control_flow else ValueError("cleanup callback") + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def resource(): + try: + yield 1 + finally: + await asyncio.sleep(0) + cleaned.append("asyncgen") + + async def sibling() -> None: + try: + await ctx.wait_for_external_event("never") + finally: + await asyncio.sleep(0) + cleaned.append("sibling") + + opened = resource() + await anext(opened) + retained.append(asyncio.create_task(sibling())) + current = asyncio.current_task() + assert current is not None + retained.append(current) + try: + await ctx.call_activity("wait") + finally: + if control_flow: + fail() + asyncio.get_running_loop().call_soon(fail) + await asyncio.sleep(0) + cleaned.append("root") + + if control_flow: + with pytest.raises(SystemExit, match="cleanup exit"): + _execute(orchestrator, [], _start()) + else: + result = _execute(orchestrator, [], _start()) + assert [action.scheduleTask.name for action in result.actions] == ["wait"] + assert "cleanup callback" in caplog.text + assert "root" in cleaned + assert "sibling" in cleaned + assert "asyncgen" in cleaned + assert all(pending.done() for pending in retained) + + @pytest.mark.parametrize("count", [128, 4096]) def test_fanout_waiter_inspections_scale_linearly( count: int, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None: @@ -734,6 +974,72 @@ async def branch(index: int) -> Any: assert not caplog.records +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 resource(): + try: + yield 1 + finally: + await asyncio.sleep(0) + cleaned.append("asyncgen") + + opened = resource() + await anext(opened) + try: + await ctx.call_activity("wait") + finally: + while True: + await asyncio.sleep(0) + + result = _execute(orchestrator, [], _start()) + assert [action.scheduleTask.name for action in result.actions] == ["wait"] + assert cleaned == ["asyncgen"] + assert "cleanup did not quiesce" in caplog.text + + +@pytest.mark.parametrize("grouped", [False, True]) +@pytest.mark.parametrize("custom_error", [False, True]) +def test_forced_cleanup_preserves_process_control_after_other_errors(grouped: bool, custom_error: bool) -> None: + class CleanupAbort(BaseException): + pass + + cleaned: list[str] = [] + exit_error: BaseException = SystemExit("forced cleanup exit") + if grouped: + exit_error = BaseExceptionGroup("forced cleanup group", [exit_error, ValueError("nested")]) + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def branch(name: str, error: BaseException | None) -> None: + try: + await ctx.wait_for_external_event(name) + finally: + try: + while True: + try: + await asyncio.Future() + except asyncio.CancelledError: + # Force teardown to close a cancellation-resistant coroutine. + pass + finally: + cleaned.append(name) + if error is not None: + raise error + + first_error = CleanupAbort("custom cleanup error") if custom_error else ValueError("ordinary cleanup error") + asyncio.create_task(branch("ordinary", first_error)) + asyncio.create_task(branch("exit", exit_error)) + asyncio.create_task(branch("last", None)) + await ctx.call_activity("wait") + + with pytest.raises(type(exit_error)) as caught: + _execute(orchestrator, [], _start()) + assert caught.value is exit_error + assert cleaned == ["ordinary", "exit", "last"] + + @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: @@ -921,3 +1227,534 @@ async def borrow(index: int) -> int: result = _execute(orchestrator, old, [helpers.new_task_completed_event(1, "10")]) assert json.loads(_completion(result).result.value) == [10, 11, 12] assert _CountingFuture.comparisons < count + + +def test_cooperative_cleanup_prioritizes_grouped_process_control() -> None: + class CleanupAbort(BaseException): + pass + + exit_error = BaseExceptionGroup("cleanup exit", [SystemExit("stop"), ValueError("nested")]) + cleaned: list[str] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def branch(name: str, error: BaseException | None) -> None: + try: + await ctx.wait_for_external_event(name) + finally: + await asyncio.sleep(0) + cleaned.append(name) + if error is not None: + raise error + + asyncio.create_task(branch("custom", CleanupAbort("first failure"))) + asyncio.create_task(branch("exit", exit_error)) + asyncio.create_task(branch("last", None)) + await ctx.call_activity("wait") + + with pytest.raises(BaseExceptionGroup) as caught: + _execute(orchestrator, [], _start()) + assert caught.value is exit_error + assert cleaned == ["custom", "exit", "last"] + + +def _cleanup_signal(kind: str) -> BaseException: + if kind == "system_exit": + return SystemExit("stop") + if kind == "keyboard_interrupt": + return KeyboardInterrupt("interrupt") + if kind == "nested_group": + return BaseExceptionGroup("outer", [BaseExceptionGroup("inner", [SystemExit("stop")])]) + if kind == "generator_exit": + return GeneratorExit("closed") + return BaseExceptionGroup("cancelled", [asyncio.CancelledError()]) + + +@pytest.mark.parametrize("control_first", [True, False]) +@pytest.mark.parametrize("other", ["generator_exit", "cancelled_group"]) +@pytest.mark.parametrize("control", ["system_exit", "keyboard_interrupt", "nested_group"]) +def test_cleanup_keeps_process_control_over_other_control_flow( + control: str, other: str, control_first: bool) -> None: + control_error = _cleanup_signal(control) + errors = [control_error, _cleanup_signal(other)] + if not control_first: + errors.reverse() + cleaned: list[int] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def branch(index: int) -> None: + try: + await ctx.wait_for_external_event(f"never-{index}") + finally: + cleaned.append(index) + raise errors[index] + + asyncio.create_task(branch(0)) + asyncio.create_task(branch(1)) + await ctx.call_activity("wait") + + with pytest.raises(type(control_error)) as caught: + _execute(orchestrator, [], _start()) + assert caught.value is control_error + assert cleaned == [0, 1] + + +def test_cleanup_keeps_first_custom_exit_over_later_generator_exit() -> None: + class CleanupAbort(BaseException): + pass + + errors: list[BaseException] = [CleanupAbort("first"), GeneratorExit("later")] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def branch(index: int) -> None: + try: + await ctx.wait_for_external_event(f"never-{index}") + finally: + raise errors[index] + + asyncio.create_task(branch(0)) + asyncio.create_task(branch(1)) + await ctx.call_activity("wait") + + complete = _completion(_execute(orchestrator, [], _start())) + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_FAILED + assert complete.failureDetails.errorType.endswith("CleanupAbort") + + +def test_dispatch_restores_host_async_generator_hooks() -> None: + def firstiter(generator: Any) -> None: + pass + + def finalizer(generator: Any) -> None: + pass + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def resource() -> AsyncIterator[int]: + yield 1 + + await anext(resource()) + await ctx.call_activity("wait") + + previous = sys.get_asyncgen_hooks() + sys.set_asyncgen_hooks(firstiter=firstiter, finalizer=finalizer) + try: + _execute(orchestrator, [], _start()) + assert sys.get_asyncgen_hooks() == (firstiter, finalizer) + finally: + sys.set_asyncgen_hooks(*previous) + + +def test_loop_close_is_idempotent(monkeypatch: pytest.MonkeyPatch) -> None: + ctx = cast(task.OrchestrationContext, SimpleNamespace(current_utc_datetime=NOW, instance_id="id")) + loop = DurableEventLoop(ctx, is_complete=lambda: False) + loop.close() + monkeypatch.setattr(loop, "_drain", lambda *args: pytest.fail("closed loop drained again")) + loop.close() + assert loop.is_closed() + + +@pytest.mark.parametrize("count", [2, 32, 6000]) +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 resource(index: int) -> AsyncIterator[int]: + try: + yield 1 + finally: + if index + 1 < count: + await anext(resource(index + 1)) + await asyncio.sleep(0) + closed.append(index) + + await anext(resource(0)) + await ctx.call_activity("wait") + + actions = _execute(orchestrator, [], _start()).actions + assert [action.scheduleTask.name for action in actions] == ["wait"] + if count == 6000: + # Each sweep's step budget is fixed at entry, so a long chain is truncated. + assert caplog.text.count("Async cleanup did not quiesce") == 1 + assert closed[:3000] == list(range(3000)) + assert len(closed) < count + else: + assert closed == list(range(count)) + assert "Async" not in caplog.text + + +@pytest.mark.parametrize("control", [None, "system_exit", "keyboard_interrupt", "nested_group"]) +def test_infinite_asyncgen_discovery_has_bounded_cleanup(control: str | None) -> None: + code = """ +import asyncio +import logging +import sys +from durabletask import task +from tests.durabletask.test_asyncio_orchestrators import _cleanup_signal +from tests.durabletask.test_coroutine_orchestrators import _execute, _start + +control = sys.argv[1] +exit_error = None if control == "none" else _cleanup_signal(control) +warnings = [] + +class Capture(logging.Handler): + def emit(self, record): + warnings.append(logging.Formatter().format(record)) + +logging.getLogger().addHandler(Capture()) + +async def orchestrator(ctx: task.OrchestrationContext, _): + async def resource(): + try: + yield 1 + finally: + await anext(resource()) + + async def signal_on_forced_close(): + try: + await ctx.wait_for_external_event("never") + finally: + try: + while True: + try: + await asyncio.Future() + except asyncio.CancelledError: + pass + finally: + raise exit_error + + if exit_error is not None: + asyncio.create_task(signal_on_forced_close()) + await anext(resource()) + await ctx.call_activity("wait") + +try: + result = _execute(orchestrator, [], _start()) +except BaseException as error: + assert exit_error is not None and error is exit_error, repr(error) +else: + assert exit_error is None + assert [action.scheduleTask.name for action in result.actions] == ["wait"] +assert any("Async cleanup did not quiesce" in message for message in warnings), warnings +""" + process = subprocess.run( + [sys.executable, "-c", code, control or "none"], + capture_output=True, text=True, timeout=30) + assert process.returncode == 0, process.stdout + process.stderr + + +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 values() -> AsyncIterator[int]: + yield 1 + + for _ in range(200): + generator = values() + references.append(ref(generator)) + async for _ in generator: + pass + del generator + gc.collect() + assert sum(reference() is not None for reference in references) < 100 + await ctx.call_activity("wait") + + actions = _execute(orchestrator, [], _start()).actions + assert len(actions) == 1 + assert actions[0].HasField("scheduleTask") + assert actions[0].scheduleTask.name == "wait" + + +@pytest.mark.parametrize("suspend", ["activity", "entity"]) +def test_entity_lock_disposal_during_teardown_does_not_schedule_releases( + suspend: str, caplog: pytest.LogCaptureFixture) -> None: + first = entities.EntityInstanceId("counter", "a") + second = entities.EntityInstanceId("counter", "b") + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Any: + with await ctx.lock_entities([first, second]): + if suspend == "activity": + return await ctx.call_activity("inside-lock") + return await ctx.call_entity(first, "get") + + old = _start() + lock = _execute(orchestrator, [], old).actions[0].sendEntityMessage.entityLockRequested + old += [helpers.new_event_sent_event(1, str(first), json.dumps({"id": lock.criticalSectionId}))] + new = [helpers.new_event_raised_event(lock.criticalSectionId, None)] + actions = _execute(orchestrator, old, new).actions + assert len(actions) == 1 + if suspend == "activity": + assert actions[0].scheduleTask.name == "inside-lock" + old += new + [helpers.new_task_scheduled_event(2, "inside-lock")] + new = [helpers.new_task_completed_event(2, json.dumps(42))] + else: + call = actions[0].sendEntityMessage.entityOperationCalled + assert call.targetInstanceId.value == str(first) + old += new + [helpers.new_event_sent_event(2, str(first), json.dumps({"id": call.requestId}))] + new = [helpers.new_event_raised_event(call.requestId, json.dumps({"result": "42"}))] + assert "cleanup" not in caplog.text + + result = _execute(orchestrator, old, new) + actions = result.actions + releases = [action.sendEntityMessage.entityUnlockSent for action in actions[:-1]] + # Entity calls inside a critical section keep the legacy shared-list release order. + expected = [first, second] if suspend == "activity" else [second, first] + assert [release.targetInstanceId.value for release in releases] == [str(e) for e in expected] + assert [action.id for action in actions] == [3, 4, 5] + assert json.loads(_completion(result).result.value) == 42 + assert "cleanup" not in caplog.text + + +@pytest.mark.parametrize("extra", [0, 1]) +def test_cleanup_reaching_budget_on_final_step_is_not_truncated( + extra: int, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None: + ran: list[int] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + loop = asyncio.get_running_loop() + + def chain(remaining: int) -> None: + ran.append(remaining) + if remaining: + loop.call_soon(chain, remaining - 1) + + async def branch() -> None: + try: + await ctx.wait_for_external_event("never") + finally: + loop.call_soon(chain, 20) + + asyncio.create_task(branch()) + await ctx.call_activity("wait") + + steps: list[int] = [] + drain = DurableEventLoop._drain # pyright: ignore[reportPrivateUsage] + + def recording_drain(self: DurableEventLoop, cleanup_limit: int | None = None) -> int: + result = drain(self, cleanup_limit) + steps.append(result) + return result + + monkeypatch.setattr(DurableEventLoop, "_drain", recording_drain) + _execute(orchestrator, [], _start()) + # The first cleanup drain's step count is exactly what the cleanup needs. + needed = steps[1] + assert len(ran) == 21 and "did not quiesce" not in caplog.text + ran.clear() + caplog.clear() + monkeypatch.setattr(DurableEventLoop, "_cleanup_budget", lambda self: needed - extra) + _execute(orchestrator, [], _start()) + assert ("did not quiesce" in caplog.text) is bool(extra) + assert (len(ran) == 21) is not bool(extra) + + +@pytest.mark.parametrize("running", [0, 200]) +def test_async_generator_sweep_inspects_each_generator_once( + running: int, monkeypatch: pytest.MonkeyPatch) -> None: + lookups = 0 + + class CountingDict(dict[int, Any]): + def get(self, key: int, default: Any = None) -> Any: + nonlocal lookups + lookups += 1 + return super().get(key, default) + + sweep = DurableEventLoop._close_asyncgens # pyright: ignore[reportPrivateUsage] + monkeypatch.setattr( + DurableEventLoop, "_close_asyncgens", lambda self, closed: sweep(self, CountingDict(closed))) + initial, chained = 400, 40 + closed: list[int] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + gate = asyncio.Event() + + async def resource(index: int) -> AsyncIterator[int]: + try: + yield index + finally: + if initial <= index < initial + chained - 1: + await anext(resource(index + 1)) + if index == initial + chained - 1: + gate.set() + closed.append(index) + + async def gated(index: int) -> AsyncIterator[int]: + try: + yield index + try: + await ctx.wait_for_external_event("never") + except asyncio.CancelledError: + # Stay running until the last link of the close chain. + await gate.wait() + yield index + finally: + closed.append(index) + + generators = [resource(index) for index in range(initial)] + for generator in generators: + await anext(generator) + for index in range(initial + chained, initial + chained + running): + generator = gated(index) + await anext(generator) + asyncio.create_task(anext(generator)) + await anext(resource(initial)) + await ctx.call_activity("wait") + + _execute(orchestrator, [], _start()) + assert sorted(closed) == list(range(initial + chained + running)) + # Running generators are not rescanned on every link of the chain. + assert lookups < 2 * (initial + chained + running) + + +def test_forced_close_cannot_start_async_generators(caplog: pytest.LogCaptureFixture) -> None: + opened: list[int] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def resource() -> AsyncIterator[int]: + opened.append(1) + yield 1 + + async def stubborn() -> None: + try: + await ctx.wait_for_external_event("never") + finally: + try: + while True: + try: + await asyncio.Future() + except asyncio.CancelledError: + continue + finally: + # Runs only on forced close, after the last generator sweep. + await anext(resource()) + + asyncio.create_task(stubborn()) + await ctx.call_activity("wait") + + actions = _execute(orchestrator, [], _start()).actions + assert [action.scheduleTask.name for action in actions] == ["wait"] + assert opened == [] + assert "Async generators cannot start while dispatch cleanup force-closes" in caplog.text + + +def test_forced_close_runs_finalizer_on_durable_loop_under_host_loop() -> None: + marker: ContextVar[str] = ContextVar("marker", default="unset") + observed: list[tuple[bool, str, bool]] = [] + escaped: list[str] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + marker.set("orchestration") + + async def stubborn() -> None: + try: + await ctx.wait_for_external_event("never") + finally: + try: + while True: + try: + await asyncio.Future() + except asyncio.CancelledError: + # Ignore cancellation so only a forced close ends this branch. + continue + finally: + loop = asyncio.get_running_loop() + observed.append(( + isinstance(loop, DurableEventLoop), marker.get(), + asyncio.current_task() is not None)) + loop.call_soon(escaped.append, "callback") + + asyncio.create_task(stubborn()) + await ctx.call_activity("wait") + + async def host() -> None: + marker.set("host") + actions = _execute(orchestrator, [], _start()).actions + assert [action.scheduleTask.name for action in actions] == ["wait"] + assert marker.get() == "host" + await asyncio.sleep(0) + await asyncio.sleep(0) + + asyncio.run(host()) + assert observed == [(True, "orchestration", True)] + assert escaped == [] + + +def test_running_async_generator_is_closed_after_its_anext_stops() -> None: + cleaned: list[str] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + release = asyncio.Event() + + async def releaser() -> AsyncIterator[int]: + try: + yield 0 + finally: + release.set() + cleaned.append("releaser") + + async def stubborn() -> AsyncIterator[int]: + try: + yield 0 + try: + await ctx.wait_for_external_event("never") + except asyncio.CancelledError: + await release.wait() + yield 1 + finally: + cleaned.append("stubborn") + + first, second = releaser(), stubborn() + await anext(first) + await anext(second) + asyncio.create_task(anext(second)) + await ctx.call_activity("wait") + + actions = _execute(orchestrator, [], _start()).actions + assert [action.scheduleTask.name for action in actions] == ["wait"] + assert cleaned == ["releaser", "stubborn"] + + +def test_cleanup_records_task_failure_before_later_callback_failure() -> None: + class CleanupAbort(BaseException): + pass + + def later() -> None: + raise GeneratorExit("later") + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def branch() -> None: + try: + await ctx.wait_for_external_event("never") + finally: + asyncio.get_running_loop().call_soon(later) + raise CleanupAbort("first") + + asyncio.create_task(branch()) + await ctx.call_activity("wait") + + complete = _completion(_execute(orchestrator, [], _start())) + assert complete.orchestrationStatus == pb.ORCHESTRATION_STATUS_FAILED + assert complete.failureDetails.errorType.endswith("CleanupAbort") + + +@pytest.mark.parametrize("buffered", [False, True]) +def test_teardown_cannot_wait_for_or_consume_external_events(buffered: bool) -> None: + errors: list[str] = [] + + async def orchestrator(ctx: task.OrchestrationContext, _: Any) -> None: + async def branch() -> None: + try: + await ctx.wait_for_external_event("never") + finally: + try: + ctx.wait_for_external_event("approval") + except RuntimeError as error: + errors.append(str(error)) + + asyncio.create_task(branch()) + await ctx.call_activity("wait") + + new = _start() + ([helpers.new_event_raised_event("approval", '"yes"')] if buffered else []) + actions = _execute(orchestrator, [], new).actions + assert [action.scheduleTask.name for action in actions] == ["wait"] + assert len(errors) == 1 and "Dispatch cleanup cannot" in errors[0]