Repository navigation
[Async stack 3/6] Dispatch cleanup for coroutine orchestrators #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| 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 +46,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 | ||
|
|
@@ -154,9 +162,13 @@ 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 | ||
| self._fatal: BaseException | None = None | ||
| self._cleanup_failure: BaseException | None = None | ||
| self._cleanup_truncated = False | ||
| self._root: asyncio.Task[Any] | None = None | ||
|
|
||
| def _now(self) -> datetime: | ||
|
|
@@ -272,6 +284,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() | ||
|
|
@@ -321,7 +335,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(): | ||
|
|
@@ -332,36 +347,64 @@ 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: | ||
| def _drain(self, cleanup_limit: int | None = None) -> int: | ||
| 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: | ||
| steps = 0 | ||
| if cleanup_limit is None: | ||
| cleanup_limit = self._cleanup_budget() | ||
| while self._ready or self._cancel_ready: | ||
| self._ready.extend(handle for _, _, handle in sorted(self._cancel_ready)) | ||
| self._cancel_ready.clear() | ||
| handle = self._ready.popleft() | ||
| if not handle.cancelled(): | ||
| handle._run() | ||
| try: | ||
| handle._run() | ||
| except BaseException as error: | ||
| if not self._closing: | ||
| raise | ||
| # Defer all exits until other cleanup tasks have drained. | ||
| self._record_cleanup_error(error) | ||
| 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: | ||
|
|
||
| self._report_unquiesced_cleanup() | ||
| break | ||
| return steps | ||
| 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 _asyncgen_firstiter(self, generator: Any) -> None: | ||
| 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: | ||
| if not self._closed: | ||
| self.create_task(generator.aclose()) | ||
|
|
||
| def call_exception_handler(self, context: dict[str, Any]) -> None: | ||
| error = context.get("exception") | ||
| if isinstance(error, StopIteration): | ||
|
|
@@ -370,26 +413,87 @@ 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_ready.clear() | ||
| 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. | ||
| # Closed generators stay referenced 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() | ||
| while True: | ||
| pending = [ | ||
| generator for key, generator in self._asyncgens.items() if closed.get(key) is not generator] | ||
| if not pending: | ||
| return | ||
| if steps >= limit: | ||
| self._report_unquiesced_cleanup() | ||
| return | ||
| for generator in pending: | ||
| closed[id(generator)] = generator | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Marking the generator closed before |
||
| self.create_task(generator.aclose()) | ||
|
Comment on lines
+451
to
+453
|
||
| # Each drain spends from this sweep's remaining budget. | ||
| steps += max(1, self._drain(limit - steps)) | ||
|
|
||
| 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()} | ||
| unobserved_at_close = { | ||
| future for future in completed_at_close if getattr(future, "_log_traceback", False)} | ||
| 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() | ||
| future.cancel() | ||
| self._drain() | ||
| self._close_asyncgens(closed_asyncgens) | ||
| failure: BaseException | None = self._cleanup_failure | ||
| for future in tuple(self._tasks): | ||
| if not future.done(): | ||
| try: | ||
| future.get_coro().close() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This forced |
||
| except BaseException as error: | ||
|
andystaples marked this conversation as resolved.
Resolved
|
||
| # Close siblings before propagating process-control exits. | ||
| 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 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()) | ||
| elif not future.cancelled(): | ||
| error = future.exception() | ||
| if error is not None and (future not in completed_at_close or future in unobserved_at_close): | ||
| self._record_cleanup_error( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Task exceptions are recorded only after the drains, so a later callback can displace the first unexpected cleanup failure. A cleanup task that queues a callback and raises a custom |
||
| error, "background task" if future in completed_at_close else "task cleanup") | ||
| if self._cleanup_failure is not None: | ||
| raise self._cleanup_failure | ||
| if failure is not None: | ||
| raise failure | ||
| finally: | ||
|
|
@@ -403,6 +507,7 @@ def close(self) -> None: | |
| self._waits.clear() | ||
| self._timers.clear() | ||
| self._tasks.clear() | ||
| self._asyncgens.clear() | ||
|
|
||
|
|
||
| _IO_HINT = "External I/O is unsupported in orchestrators; move it to an ordinary activity." | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.