Skip to content

Worker can still block in mark_as_failure() when result_backend_always_retry=True and the backend is down #10969

Open

Description

@majidkhazaei

Summary

As confirmed by @ChristianMurphy in #10957, with result_backend_always_retry=True and a down result backend, Request.on_failure() still blocks in mark_as_failure() → store_result() → _ensure_retryable(), because the write goes through the same infinite retry loop as the read fixed in #10960.

This is pre-existing (confirmed on 5.6.3 and 5.7.0rc1) and was not introduced by #10752. #10960 fixes the regression in _success_was_stored() for the acks_late + reject_on_worker_lost path, but deliberately leaves this write path untouched.

Reproduction (from @ChristianMurphy)

Request.on_failure() with a WorkerLostError, Redis backend port closed, result_backend_always_retry=True, result_backend_max_retries at its default (no limit). Each run waits up to 30 seconds.

Version Task settings always_retry off always_retry on
5.6.3 default finishes blocked in mark_as_failure()
5.6.3 acks_late, reject_on_worker_lost finishes requeues
5.7.0rc1 default finishes blocked in mark_as_failure()
5.7.0rc1 acks_late, reject_on_worker_lost finishes requeues
main (after #10960) default finishes blocked in mark_as_failure()
main (after #10960) acks_late, reject_on_worker_lost finishes requeues

"Finishes" means on_failure() was done in under 0.1 s. With always_retry off, mark_as_failure() raises ConnectionError instead of hanging on every version — expected when retries are disabled.

Where

  • celery/backends/base.py — mark_as_failure() calls store_result()
  • store_result() calls _ensure_retryable(self._store_result, ...) → infinite loop with max_retries=inf
  • Request.on_failure() in celery/worker/request.py calls mark_as_failure() in the worker's main process, so the whole worker stalls, not just one task.

Proposed approaches to discuss

  1. Bounded retry budget for backend calls from the worker's main process — cap retries when executing in the main process, similar to the read fix in fix: prevent worker hang in WorkerLostError when result_backend_always_retry=True #10960.
  2. Deferring the failure write — acknowledge the message first, write the failure asynchronously or after a bounded timeout.
  3. Time-based retry budget — instead of a fixed retry count (3 in fix: prevent worker hang in WorkerLostError when result_backend_always_retry=True #10960), use a wall-clock budget (e.g., 2s) so outages of different lengths are covered.

The trade-off from #10960 applies here too: a 3-retry cap is too short for a 2s outage (from @ChristianMurphy's testing in #10960), so whichever approach is chosen should probably be time-based rather than count-based.

Notes

cc @MehrazRumman @ChristianMurphy @auvipy

Activity

  1. added this to the 5.7.x milestone on Oct 31, 2026
  2. bbbbbbbbbbb114 commented on Nov 2, 2026

    @bbbbbbbbbbb114

    I'd like to work on the write-side retry budget with Codex assistance.

    On current main (57cb15f), I reproduced the synchronous on_failure -> mark_as_failure -> store_result -> _ensure_retryable loop using the real Request and Redis backend, with only backend I/O and sleeps controlled. With unlimited retries, it reached eight failed writes/eight sleeps before my test sentinel stopped it. The controls propagated the error with retries disabled or exhausted, and returned normally when the sixth write succeeded after five failures. This was a callback-level test, not a live prefork-worker integration test.

    Would a per-call monotonic retry budget be the right scope here, respecting a lower configured retry-count limit without mutating the shared backend policy or changing acknowledgement/requeue ordering? The budget would bound retry/backoff time; backend socket timeouts still need to bound each individual I/O operation.

    Before preparing a PR, could we settle the default/setting for that budget and what should happen when the failure write exhausts it (in particular, whether to propagate the existing BackendStoreError or continue failure hooks after logging the persistence failure)? Should the hard-timeout/revocation write paths be included in the same change? I'll hold the PR until this scope is agreed.

  3. majidkhazaei commented on Nov 2, 2026

    @majidkhazaei
    ContributorAuthor

    Welcome @bbbbbbbbbbb114, and thanks for the detailed repro.

    Since we used a count-based cap (min(max_retries, 3)) in #10960, I think your time-based approach makes more sense for the write-side because it bounds the actual blocking duration predictably. Using an explicit argument (e.g., time_budget=) is exactly the right way to avoid the shared-state concurrency issue.

    On exhaustion: I'd lean toward logging and continuing to failure hooks. Propagating BackendStoreError might skip errbacks, which defeats the original purpose of preserving chord/errback behavior. But this is a policy call.

    On scope: I'd suggest keeping this PR focused on mark_as_failure. on_timeout and revocation have different semantics and can be follow-ups.

    @auvipy @MehrazRumman — could you weigh in on the scope, exhaustion behavior, and default value?

    @bbbbbbbbbbb114 I'm happy to help review the PR when it's ready!

  4. bbbbbbbbbbb114 commented on Nov 2, 2026

    @bbbbbbbbbbb114

    Thanks @majidkhazaei — I agree with keeping timeout and revocation handling out of this change, and with an explicit per-call budget that respects a lower retry-count limit without changing shared backend policy.

    I rechecked on main at 26251bdf0c024401ee56a7761dfee77dd03af776. One scope detail needs care: after the first write, mark_as_failure() calls _fail_chain(), whose later writes use the same unlimited retry loop. In a controlled probe, the parent write succeeded but the next chain write reached a four-sleep sentinel before any errback ran; when that write recovered, the errback was reached. These are source-level probes, not live-worker integration tests.

    Would the intended budget cover those failure-propagation writes under one shared deadline? Bounding only the initial write would leave this path open. Chord operations also need checking before claiming the whole callback is bounded, and a retry deadline cannot interrupt an in-flight socket call.

    Logging exhausted recoverable persistence failures and continuing failure handling sounds reasonable to me. I'll hold the PR pending @auvipy / @MehrazRumman's decision on that contract and the default value/setting. Thanks for offering to review!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions