Skip to content

Keep the worker running through a Server failover's 500 and 503 - #383

Merged
rmcdaniel merged 3 commits into
mainfrom
general/transient-5xx-retry
Nov 26, 2026
Merged

rmcdaniel merged 3 commits into
mainfrom
general/transient-5xx-retry

Conversation

@rmcdaniel

Copy link
Copy Markdown
Member

Follow-up to a finding from the Cloud HA run in durable-workflow/cloud#752 (run 2). During a database primary failover, the Server answered an activity poll with HTTP 500, and the PHP SDK worker exited. This PR checks the Python worker for the same failure and closes the gaps it found.

What already held

  • Polls: workflow, activity and query pollers retry any failure with full jitter capped at 10 s. Only the strict storage-admission refusal is raised.
  • Worker heartbeats: a failure is logged and retried at the next interval.
  • Activity lease renewal: treats every 5xx and 429 as transient.

So Worker.run() did not exit on a poll-time 500.

Gaps fixed

  1. Plain HTTP 500 was not transient. _transient_runtime_error covered only 502, 503 and 504 without a protocol body. A 500 such as {"message": "Server Error"} was not transient, which caused two problems:

    • an activity completion or failure report was dropped after the client's 3 quick attempts, even with lease time left;
    • a 500 on POST /worker/register stopped Worker.run() at startup.

    It is now transient, with the same rule as 502/503/504: no typed reason. A 5xx with a typed reason, retryable: false, 501, and every 4xx keep their current handling.

  2. Workflow-task completion gave up after about 1 s. It made 3 worker attempts × 3 transport attempts and then dropped the completion, so the task waited for its lease to expire. Transient failures are now resent with the shared backoff uniform(0, min(4, 0.25·2^(n-1))) while the task's lease_expires_at covers the next wait, or for 60 s when the lease is unknown. When the lease runs out the worker logs an error. Each resend has the same task ID, lease owner, workflow_task_attempt and commands, and the request bytes are identical, so lease fencing is unchanged. Lease and attempt refusals of a resend are still never reported as a workflow-task failure. Other retryable errors, such as a typed 5xx or a builtin TimeoutError, keep the 3-attempt budget.

  3. Retry-After was ignored everywhere. The client now parses the header, in both the delta-seconds and HTTP-date forms, into DurableWorkflowError.retry_after. Polls, registration, activity outcomes and workflow-task completions wait at least that long, capped at 60 s. TransportRetryPolicy honours it between its own attempts, capped at max_backoff_seconds.

Unchanged on purpose

  • Task-offer 409 refusals and lease fencing.
  • Storage-admission pause and stop behaviour.
  • Poll-capacity 429 handling.
  • Query-task answers: a lost answer only times out that query.
  • Workflow-task /fail reports: the Server redelivers the task at lease expiry, and replay fails it again.
  • The local-activity workflow-task heartbeat fence, which abandons the claim, not the worker.
  • User-called activity heartbeats, which still raise to user code.

Tests

  • tests/test_worker_transient_5xx_retry.py drives a fake Server (httpx.MockTransport with a real Client) through 500, then 503 with Retry-After, then success, with sleeps injected through Worker._retry_sleep:
    • Worker.run() end to end: covers the activity poll and the activity completion. It asserts that the worker is still running, the completion was delivered once, and the waits are [0.25, 2.0] (delta-seconds) and [0.25, ≤3] (HTTP-date).
    • Replayed workflow-task completion: uses the new replay-regression fixture failover-completion-resend-cold-history, which is also registered in the corpus harness. It checks three byte-identical sends and that the expected result is delivered.
    • Bounds: the poll schedule caps at 10 s, completions at 4 s, and a Retry-After: 86400 is capped at 60 s. Resending stops when the lease runs out. A typed 5xx keeps 3 attempts. A 409 is not resent.
    • Parsing: parse_retry_after handles both forms, and TransportRetryPolicy caps the header.
  • tests/test_worker_transient_runtime.py: the classification table now expects a bare 500 to be transient, and a new case checks that registration survives repeated 500s.
  • Locally, on python:3.12 (3.12.16): the targeted modules pass, along with ruff, mypy and the regression-corpus validator. The new tests also pass on 3.10.

Docs: docs/reference/worker.md has new sections, "Workflow-task completions through transient failures" and "Server failover and Retry-After", and an updated list of transient failures. CHANGELOG: entry under Unreleased → Fixed.

🤖 Generated with Claude Code

A Cloud HA run (durable-workflow/cloud#752, run 2) saw the Server answer
HTTP 500 during a database failover; the PHP SDK worker exited. The
Python pollers and heartbeats already rode that out, but three gaps
remained:

- A 500 without a protocol reason was not transient. An activity
  completion or failure report hit by one was dropped after the client's
  three quick attempts, and registration hit by one stopped Worker.run().
  It is now transient like an upstream 502/503/504. Typed 5xx reasons,
  retryable: false and 4xx refusals keep their handling.
- A workflow-task completion was given up after about a second. A
  transient failure is now resent with the shared backoff while the
  task's lease_expires_at covers the next wait (60 s when unknown). Each
  resend carries the same task, lease owner, attempt and commands, so
  lease fencing is unchanged. Other retryable failures keep the
  three-attempt budget.
- Retry-After was ignored. Errors raised for HTTP responses now carry
  the parsed value (delta-seconds or HTTP-date) as retry_after. Polls,
  registration, activity outcomes and workflow-task completions wait at
  least that long, capped at 60 s, and TransportRetryPolicy honours it
  up to max_backoff_seconds.

Retry waits go through an injectable Worker._retry_sleep. The new tests
drive a fake Server through 500, 503 with Retry-After, then success for
polling, activity completion and a replayed workflow-task completion
(new replay-regression fixture), and check the bounded waits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Server answers a database failover with
500 {"reason":"server_error",...}. Count that generic reason as transient
like an untyped 5xx, so registration, activity outcomes and workflow-task
completions retry it. Typed reasons and retryable:false stay definitive.
The fake-Server failover test now sends the Server's real body.

A Retry-After value longer than ten digits is treated as a very long wait
(callers cap it) instead of raising OverflowError or ValueError.

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

Copy link
Copy Markdown
Member Author

Independent review: PASS at fc7eb43fe94a691483d1279eca140264a917c76b. CI is green: Python 3.10–3.15, lint, integration, the regression corpus, CodeQL and Sonar.

  • The first review blocked because the Server's real failover body, 500 {"reason":"server_error",...} from Servers 3.28 and 3.29, was not transient. It now is: a 500/502/503/504 with no reason or with server_error is transient, so registration, activity outcomes and workflow-task completions retry it. retryable:false and typed reasons stay definitive. The fake-Server failover test now sends that real body.
  • An overlong Retry-After value is treated as a very long wait, which callers cap, instead of raising.
  • Polls and heartbeats keep running. Workflow-task completion resends are byte-identical and bounded by lease_expires_at. Refusals and the task-offer 409 refusals are unchanged. The 500 → 503 → success test is deterministic.

Non-blocking items to pick up later:

  • an end-to-end /register case with the server_error body;
  • ctx.heartbeat() called from activity code still raises during a long failover (documented as intended).

🤖 Generated with Claude Code

@rmcdaniel
rmcdaniel enabled auto-merge (squash) November 26, 2026 00:50
@rmcdaniel
rmcdaniel disabled auto-merge November 26, 2026 00:51
@rmcdaniel

Copy link
Copy Markdown
Member Author

Delta check: I updated the branch with a merge from main. The new head is 6e3c8447; the reviewed head was fc7eb43f. The PR's own diff against main is identical; only main's docs/sdk-reference.md change came in. I'm re-arming auto-merge at 6e3c8447.

@rmcdaniel
rmcdaniel enabled auto-merge (squash) November 26, 2026 00:51
@sonarqubecloud

Copy link
Copy Markdown

@rmcdaniel
rmcdaniel merged commit fe46b30 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