Skip to content

Advertise timeout cleanup and send the portable interruption marker - #381

Merged
rmcdaniel merged 2 commits into
mainfrom
general/sdk-python-1201-timeout-cleanup
Nov 25, 2026
Merged

rmcdaniel merged 2 commits into
mainfrom
general/sdk-python-1201-timeout-cleanup

Conversation

@rmcdaniel

@rmcdaniel rmcdaniel commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Python SDK worker side of deadline-triggered timeout cleanup, for durable-workflow/server#1201. It follows the portable contract in Workflow's docs/architecture/timeout-cleanup.md and mirrors sdk-php#481.

What changes

  • Capability. A worker with negotiated cooperative cancellation lists timeout_cleanup in capabilities and sends capability_manifest.timeout_cleanup = {supported: true, minimum_protocol_version: "1.20", version: 1}. Native worker_supports() admits this shape. A worker without cooperative cancellation advertises nothing, and its registration is unchanged. Listing timeout_cleanup without cooperative_cancellation raises ValueError, the same way the Nexus capability is checked.

  • Marker. When the delivered WorkflowTimeoutCleanupRequested escapes workflow code, fail_workflow carries failure: {interruption: "timeout_cleanup"}. That covers uncaught, re-raised, and raised again by throw_if_cancellation_requested(). The runtime remembers the exception objects it raised for a timeout request and matches the escaped exception by identity. It also requires the root context: cause: "timeout", matching request ID, no parent or scope origin, and root_workflow_run_id equal to the run. The class name alone never decides. These stay unmarked and record failed cleanup:

    • an exception the application constructs, even with a copied timeout context
    • a wrapped interruption, including a new WorkflowTimeoutCleanupRequested raised from the delivered one
    • a compensation failure
    • an ordinary or explicit cause: "cancellation" request

    Returning from cleanup still sends complete_workflow.

  • FailWorkflow gains an optional interruption field (default None), so the change is additive. The worker drops the field when cooperative cancellation is not negotiated. The failure-report limiter keeps the top-level failure key, and a test covers that.

  • New internal durable_workflow._timeout_cleanup holds the capability, the manifest entry and the marker rule. CHANGELOG (Unreleased) and docs/cooperative-cancellation-design.md describe admission and the four outcomes.

Tests

  • tests/test_timeout_cleanup.py (28 tests):
    • registration advertises the v1 entry only with cooperative cancellation; an explicit capability is listed once; timeout_cleanup without cooperative cancellation is refused
    • replay: the marker is sent for uncaught, re-raised after shielded compensation, re-raised by name, and safe-point re-raise
    • replay: no marker for compensation failure, wrapped, wrapped in the same type, constructed, ordinary cancellation, explicit cancellation cause, and re-raised ordinary cancellation
    • returning cleanup gives CompleteWorkflow; offline replay without a run ID does not mark
    • unit tests of the rule: descendant, another request, another run
    • limiter bounding keeps the marker
    • worker end to end through the claim/delivery path: 6 delivery vectors, returning cleanup, and a non-cooperative worker never sends the marker, including a defensive strip of a forced marker
  • New replay regression fixture cooperative-timeout-cleanup-interruption-marker.json. Its history contains WorkflowTimeoutCleanupStarted, then delivery, shielded cleanup and re-raise, and the expected command is fail_workflow with the marker. All test histories include WorkflowTimeoutCleanupStarted and replay unchanged.
  • Docker python:3.12-bookworm: ruff and mypy are clean, and validate-regression-corpus.py --base-ref origin/main passes. The focused suites pass (47). The full local non-integration suite gives 7972 passed and 2 failed. The two failures are GIL-blocked prepared-local timing tests, which pass on rerun; the host load was about 40. CI is green on 3.10 through 3.15.

Installed check against published native Server 3.29.0 (SQLite)

durableworkflow/server@sha256:a2f588ec34b0b3a5a1eb298ce53c0b823260f86cec7bb5c5cb3577dcc5df0127 (3.29.0), with SQLite on tmpfs and the worker running from this branch's source. server_capabilities.timeout_cleanup is still unadvertised (null), as expected.

Case Result
Start with timeout_cleanup_seconds on a queue with only a non-cooperative Python worker 422 timeout_cleanup_not_supported
Cleanup returns after a shielded timer WorkflowTimedOut, timeout_cleanup.outcome: completed
Interruption re-raised after shielded compensation (marker sent) WorkflowTimedOut, outcome: completed
Interruption uncaught (marker sent) WorkflowTimedOut, outcome: completed
Compensation raises RuntimeError WorkflowTimedOut, outcome: failed with failure_id, message ends "Timeout cleanup failed."
Wrapped interruption (raise RuntimeError(...) from interrupted) WorkflowTimedOut, outcome: failed with failure_id

Each run recorded exactly one WorkflowTimeoutCleanupStarted and failure_category: timeout. Each run used execution_timeout_seconds: 40 and timeout_cleanup_seconds: 25, one run at a time. An earlier attempt with an 8-second allowance, concurrent runs and disk SQLite on the loaded shared host hit storage_unavailable 503s, and several runs recorded expired. That is the Server enforcing the cleanup deadline, not an SDK outcome.

What this does not do

  • No release, and no version bump.
  • No client start option for timeout_cleanup_seconds.
  • Server still owns flipping ADVERTISED and the installed vectors 1 to 12.

🤖 Generated with Claude Code

A worker with cooperative cancellation now lists the timeout_cleanup
capability and sends capability_manifest.timeout_cleanup =
{supported: true, minimum_protocol_version: "1.20", version: 1}, the
shape native Server admission accepts (server#1201).

When the exception object the runtime raised for the run's root
cause: timeout request escapes workflow code, uncaught or re-raised,
fail_workflow carries failure.interruption: "timeout_cleanup".
Constructed, wrapped and compensation failures and ordinary or explicit
cancellation requests stay ordinary failures. Workers without
cooperative cancellation advertise nothing and never send the marker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant