Skip to content

[Async stack 3/6] Dispatch cleanup for coroutine orchestrators - #360

Open
andystaples wants to merge 1 commit into
andystaples-async-stack-2-asyncio-compositionfrom
andystaples-async-stack-3-dispatch-cleanup
Open

andystaples wants to merge 1 commit into
andystaples-async-stack-2-asyncio-compositionfrom
andystaples-async-stack-3-dispatch-cleanup

Conversation

@andystaples

@andystaples andystaples commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Do not merge this PR, or any PR in this stack, individually. The whole stack will be squash-merged only after every stack PR is approved.

Layer 3/6 of the review stack that splits the closed #293. The base is layer 2/6, #358.

Scope

Dispatch teardown for the per-dispatch durable event loop:

  • DurableEventLoop installs the async-generator hooks for each dispatch and restores the host's previous hooks afterwards. close() drains, then cancels, pending tasks and closes tracked async generators. close() is idempotent. Only the worker closes the loop: orchestrator code that calls loop.close() while the loop is running gets an actionable RuntimeError, as asyncio does for a running loop, and the dispatch's normal teardown still runs.
  • Cooperative cleanup is capped at about 10,000 loop steps per cleanup phase (more for dispatches with many tasks or generators). Each async-generator sweep spends from a budget fixed at sweep entry, so tasks or generators created during the sweep cannot extend it. Truncated cleanup is reported once as a warning without changing the snapshotted response.
  • Cleanup errors are collected per handle. The first unexpected failure is kept; a process-control exception such as KeyboardInterrupt or SystemExit takes precedence, even when grouped or raised after other errors. The existing dispatch response is preserved, and background and cleanup errors are logged.
  • wait_durable cannot schedule durable work once the loop is closing. The orchestration context also rejects sequence-number allocation during async cleanup, and exiting an entity critical section during teardown does not schedule extra release messages.
  • Control-flow exception classification moves from worker into the shared durabletask.internal.exceptions.is_control_flow_exception. Its BaseExceptionGroup handling is unchanged.

Docs

docs/async-orchestrators.md gains a warning callout describing the cleanup cap and truncation, and documents async-generator ownership during dispatch, explicit aclose() / contextlib.aclosing(...), and teardown handling of entity critical sections and process-control exits.

CI

Adds an async-runtime-windows job to .github/workflows/durabletask.yml that runs the asyncio, replay, and dispatch cleanup tests on Windows across Python 3.11-3.15. Layer 4/6 (#361) extends it with test_async_context.py.

Intermediate adaptations reversed in layer 4/6

Async orchestrators still receive the eager task.OrchestrationContext at this layer, so tests annotate contexts as task.OrchestrationContext instead of task.AsyncOrchestrationContext, and operations that will later go through ctx.as_task_context() (for example call_activity(...) and create_timer(...) that need eager handles) are called directly on ctx.

No changelog changes; release notes are added in layer 6/6 (#364).

Comment thread durabletask/internal/asyncio_loop.py Resolved
Comment thread durabletask/internal/asyncio_loop.py Resolved
@andystaples
andystaples force-pushed the andystaples-async-stack-3-dispatch-cleanup branch from fe8e8bf to 466196e Compare November 12, 2026 18:18
@andystaples
andystaples force-pushed the andystaples-async-stack-3-dispatch-cleanup branch from ed84f44 to c164cb6 Compare November 12, 2026 20:41
Rebased onto main as a single commit. Includes:

- Harden async orchestration dispatch cleanup
- Harden async dispatch cleanup ownership and signal precedence
- Bound async-generator cleanup discovery budget
- Make async cleanup budgets cumulative and close idempotent
- Reject closing the durable loop from orchestrator code

Co-authored-by: Copilot App <[email protected]>
@andystaples
andystaples marked this pull request as ready for review November 23, 2026 19:04
Copilot AI balanced review requested due to automatic review settings November 23, 2026 19:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Cleanup can falsely report truncation when queues empty at the step limit, while repeated scans of already-closed generators can cause quadratic teardown delays.

2 open issues
What changed in this PR

This layer adds dispatch teardown for coroutine orchestrators in the core SDK, cleaning up local async work while protecting the previously decided orchestration response.

Changes:

  • Adds bounded task and async-generator cleanup, host-hook restoration, and cleanup-error prioritization.
  • Prevents durable work and extra entity-release messages during teardown.
  • Documents cleanup behavior and adds regression coverage plus Windows testing across Python 3.11–3.15.
File Description
tests/​durabletask/​test_asyncio_orchestrators.py Tests cleanup limits, finalizers, error handling, hooks, and entity locks.
durabletask/​worker.py Guards durable scheduling and entity releases during async cleanup.
durabletask/​internal/​exceptions.py Centralizes control-flow exception classification.
durabletask/​internal/​asyncio_loop.py Implements bounded teardown and async-generator ownership.
docs/​async-orchestrators.md Explains cleanup limits, ownership, and failure behavior.
.github/​workflows/​durabletask.yml Adds Windows async-runtime and cleanup test coverage.

Review effort: Balanced

break
self._deliver_completions()
steps += 1
if self._closing and steps >= cleanup_limit:
Comment on lines +451 to +453
for generator in pending:
closed[id(generator)] = generator
self.create_task(generator.aclose())
@andystaples
andystaples force-pushed the andystaples-async-stack-3-dispatch-cleanup branch from d50a68f to 8dcea28 Compare November 23, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants