Repository navigation
Raise a typed ActivityHeartbeatUnavailable when a heartbeat hits a failover - #385
Merged
Merged
Conversation
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>
|
Member
Author
|
Independent review: PASS at 958226c A reviewer in a fresh context reviewed this PR's own changes against main and #383. Verified
Blocking: none. Non-blocking
|
This was referenced Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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 ownctx.heartbeat()raises when the outage outlasts the client's transport attempts. The error was a plainServerError, or the rawhttpxerror for a connection or timeout failure, so activity code could not tell an outage from a refusal.Decision: raise, not retry
heartbeat()andheartbeat_sync()still raise after the client's quick transport attempts. They are not retried further, for these reasons: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.heartbeat()would take that choice away.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)indurable_workflow.errors. It is exported fromdurable_workflowand listed in__all__. Fields:retry_after: the Server'sRetry-Afterin seconds, orNone.cause(also__cause__): the underlying error.task_id.statusandbody: from the HTTP response, or0andNonewhen no response arrived.Existing
except ServerErrorhandlers still catch it.When it is raised:
ActivityContext.heartbeat()(and soheartbeat_sync()) raises it only when the worker's own_transient_runtime_errorclassifies the failure as transient. That function is imported, not copied, so the two cannot drift. The transient cases are:server_errorstorage_unavailableorbackend_unavailableretryable: trueUnchanged errors: cancellation (
ActivityCancelled), a lost lease or closed attempt (ActivityWorkerLost), 4xx refusals, typed 5xx errors andretryable: falsekeep their current exceptions.worker.pyis untouched. The classifier is imported inside theexceptblock becauseworkerimportsactivity. Leavingworker.pyalone also keeps this change out of the replay corpus guard (the validator passes withrelated_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.TransportErrorfrom a heartbeat must now catchActivityHeartbeatUnavailable. The CHANGELOG says so.Tests
tests/test_activity_heartbeat_unavailable.pyuses a realClientwith anhttpx.MockTransportfake Server. There are no real sleeps or timing dependencies:server_error: withRetry-After: 7it raises the new type withretry_after == 7.0,status,body,reason(),causeand__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, andexcept ServerErrorstill catches the error.storage_unavailable/backend_unavailable,retryable: true,ConnectErrorandReadTimeoutall raise the new type. The transport cases havestatus == 0and the httpx error ascause.retryable: falseraiseServerError, a 409stale_attemptraisesServerError, a 401 raisesUnauthorized,can_continue: falseraisesActivityWorkerLost, andcancel_requestedraisesActivityCancelled.Worker.run()runs an activity whose heartbeat gets 500server_errorwithRetry-After: 3, then 503storage_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 againstmain, andmkdocs build --strictplus 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 honoursretry_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