Skip to content

Raise a typed ActivityHeartbeatUnavailable when a heartbeat hits a failover - #385

Merged
rmcdaniel merged 2 commits into
mainfrom
general/typed-heartbeat-unavailable
Nov 26, 2026
Merged

rmcdaniel merged 2 commits into
mainfrom
general/typed-heartbeat-unavailable

Conversation

@rmcdaniel

Copy link
Copy Markdown
Member

Follow-up to #383 and durable-workflow/cloud#752. In that failover the Server answered 500 {"reason":"server_error"}, then 503, then success. #383 kept the worker running through it. One case was left on purpose: an activity's own ctx.heartbeat() raises when the outage outlasts the client's transport attempts. The error was a plain ServerError, or the raw httpx error for a connection or timeout failure, so activity code could not tell an outage from a refusal.

Decision: raise, not retry

heartbeat() and heartbeat_sync() still raise after the client's quick transport attempts. They are not retried further, for these reasons:

  • The heartbeat runs inside user activity code. A hidden retry would block the activity, and heartbeat_sync() would block its thread, for as long as the outage lasts. The activity should decide whether to keep working, wait, or give up.
  • A missed heartbeat is not lost authority. The attempt keeps its claim until its heartbeat timeout or lease runs out, and lease renewal, where the Server supports it, already rides out 5xx failures in the background. Usually the right move is to keep working and heartbeat at the next checkpoint. A retry loop inside heartbeat() would take that choice away.
  • A bounded retry inside heartbeat() would have to pick a budget smaller than the heartbeat timeout. Only the activity knows that timeout and its own pace, and its next scheduled heartbeat already acts as the retry.

Change

  • New ActivityHeartbeatUnavailable(ServerError) in durable_workflow.errors. It is exported from durable_workflow and listed in __all__. Fields:

    • retry_after: the Server's Retry-After in seconds, or None.
    • cause (also __cause__): the underlying error.
    • task_id.
    • status and body: from the HTTP response, or 0 and None when no response arrived.

    Existing except ServerError handlers still catch it.

  • When it is raised: ActivityContext.heartbeat() (and so heartbeat_sync()) raises it only when the worker's own _transient_runtime_error classifies the failure as transient. That function is imported, not copied, so the two cannot drift. The transient cases are:

    • connection and timeout errors
    • a 500, 502, 503 or 504 with no reason or with server_error
    • a 503 with storage_unavailable or backend_unavailable
    • a 5xx or 429 that says retryable: true
  • Unchanged errors: cancellation (ActivityCancelled), a lost lease or closed attempt (ActivityWorkerLost), 4xx refusals, typed 5xx errors and retryable: false keep their current exceptions.

  • worker.py is untouched. The classifier is imported inside the except block because worker imports activity. Leaving worker.py alone also keeps this change out of the replay corpus guard (the validator passes with related_change: false).

  • Not covered: with cooperative cancellation enabled, a remote activity runs in a supervised callback process. There, a failed heartbeat already abandons the claim, and the error never reaches activity code. That behaviour is unchanged and now documented.

Compatibility: code that caught httpx.TransportError from a heartbeat must now catch ActivityHeartbeatUnavailable. The CHANGELOG says so.

Tests

tests/test_activity_heartbeat_unavailable.py uses a real Client with an httpx.MockTransport fake Server. There are no real sleeps or timing dependencies:

  • 500 server_error: with Retry-After: 7 it raises the new type with retry_after == 7.0, status, body, reason(), cause and __cause__ set. Both transport attempts are used and there is no hidden extra retry. The context is not marked cancelled or lost afterwards. Without the header, retry_after is None, and except ServerError still catches the error.
  • Other transient failures: untyped 500, 502 with a text body, 504, 503 storage_unavailable / backend_unavailable, retryable: true, ConnectError and ReadTimeout all raise the new type. The transport cases have status == 0 and the httpx error as cause.
  • Definitive refusals keep their exact old types: a typed 500 and a 503 with retryable: false raise ServerError, a 409 stale_attempt raises ServerError, a 401 raises Unauthorized, can_continue: false raises ActivityWorkerLost, and cancel_requested raises ActivityCancelled.
  • End to end: Worker.run() runs an activity whose heartbeat gets 500 server_error with Retry-After: 3, then 503 storage_unavailable. The activity catches the error and backs off. The recorded waits are [3.0, 2.0]. It resends the same heartbeat details and completes, and the completion is delivered once with the expected result.

Run without the fix, 12 of these 19 tests fail.

Local checks on python:3.12 (3.12.16): ruff and mypy pass, the new and related test modules pass (260 tests), the regression-corpus validator passes against main, and mkdocs build --strict plus the docstring cross-reference check pass.

Docs

  • docs/reference/worker.md: new section "Activity heartbeats during a failover". It shows activity code catching the error and retrying with jittered backoff that honours retry_after, and says not to retry definitive refusals.
  • docs/sdk-reference.md: "Activity heartbeat refusals" now points to that section.
  • CHANGELOG.md: new entry under Unreleased → Added.

🤖 Generated with Claude Code

During a Server failover (durable-workflow/cloud#752) a user-called
activity heartbeat raised once the client's transport attempts were used
up, and the error was a plain ServerError, or the raw httpx error for a
connection or timeout failure. Activity code could not tell an outage
from a refusal.

heartbeat() and heartbeat_sync() now raise ActivityHeartbeatUnavailable
for the failures the worker treats as transient (_transient_runtime_error,
#383): connection and timeout errors, a 5xx without a reason or with
server_error, a 503 storage_unavailable or backend_unavailable, and a
retryable: true 5xx or 429. It subclasses ServerError, so existing
handlers still catch it, and carries retry_after and the underlying
cause. Cancellation, lost leases, 4xx refusals and typed 5xx errors keep
their existing errors.

The heartbeat is still not retried further: it runs inside activity code,
and a hidden retry would block the activity. The activity decides whether
to keep working or retry. It is exported from durable_workflow. The
worker reference shows activity code catching it and retrying with
backoff.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A heartbeat whose response was lost (ReadTimeout) now raises
ActivityHeartbeatUnavailable with the timeout as its cause, not the raw
httpx error. The checkpoint test still checks that the received snapshot
does not change.

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

Copy link
Copy Markdown

@rmcdaniel

Copy link
Copy Markdown
Member Author

Independent review: PASS at 958226c

A reviewer in a fresh context reviewed this PR's own changes against main and #383.

Verified

  • ActivityContext.heartbeat() (src/durable_workflow/activity.py:213-228) wraps the error only when the imported worker._transient_runtime_error returns true. Transient covers transport errors, an untyped 500/502/503/504, 503 storage_unavailable or backend_unavailable, and any 5xx or 429 with retryable: true. A typed 5xx, retryable: false, any 4xx and Unauthorized are re-raised unchanged.
  • Cancellation (ActivityCancelled) and can_continue: false (ActivityWorkerLost) are unchanged. The local-activity and cooperative-remote callbacks are not touched.
  • The class is exported in __all__ and subclasses ServerError, so except ServerError and except DurableWorkflowError still catch it. status, body and retry_after come from the cause, so the worker's own body checks still match.
  • There is no extra retry: exactly max_attempts sends.
  • Locally, 131 tests pass. 12 of the 19 new tests fail when activity.py is reverted to main. CI is green.

Blocking: none.

Non-blocking

  • The CHANGELOG mentions the compatibility change only mid-paragraph. except httpx.TransportError around heartbeat() stops catching a heartbeat transport failure. An escaped outage is now recorded as failure_type ActivityHeartbeatUnavailable instead of ServerError, ReadTimeout or ConnectError, which matters for non_retryable_error_types. The release notes should call this out under Changed.
  • The retryable: true branch is not tested on its own: there is no 429 case and no typed 503 with retryable: true.
  • The docs/reference/worker.md:255-265 example sleeps for an uncapped retry_after. Cap it.
  • docs/reference/activity.md:110-112 could name the new type.

@rmcdaniel
rmcdaniel merged commit 72d224a into main Nov 26, 2026
25 checks passed
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