Skip to content

Worker stalls on _success_was_stored when result_backend_always_retry=True and the result backend is down #10957

Closed

Description

@majidkhazaei

What's wrong

#10752 fixed self.ignore_result and added saferepr to the task-succeeded event, but it didn't address the _ensure_retryable note from @MehrazRumman. _success_was_stored still calls backend.get_state(), and the event block still calls backend.get_result(). Both go through get_task_meta() → _ensure_retryable().

on_failure runs in the worker's main process, so a blocked read there stalls the whole worker, not just one task.

Environment

  • Celery: 5.7.0rc1 on main @ e3cc7cc53 (editable install)
  • Python: 3.14.4, CPython
  • OS: Linux, 64-bit
  • Backend used in tests: cache+memory:// with a mocked _get_task_meta_for
  • Broker: memory:// (not relevant to this bug)
celery report
software -> celery:5.7.0rc1 (collider) kombu:5.7.0rc1 py:3.14.4
            billiard:4.3.1 memory:N/A
platform -> system:Linux arch:64bit, ELF
            kernel version:7.0.0-38-generic imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:memory results:cache+memory://

broker_url: 'memory://'
result_backend: 'cache+memory://'
deprecated_settings: None

Where

At e3cc7cc53:

  • celery/worker/request.py:844 → backend.get_state(self.id)
  • celery/worker/request.py:718 → self.task.backend.get_result(self.id)

Reproduced

Mocked _get_task_meta_for to raise ConnectionError, always_retry=True, exception_safe_to_retry=True (the same predicate Redis and DB backends use).

_success_was_stored:

[REPRO-1] reads=4, sleeps=3
WARNING Failed operation failing_meta. Retrying 2 more times.
WARNING Failed operation failing_meta. Retrying 1 more times.
WARNING Failed operation failing_meta. Retrying 0 more times.

The except Exception only runs after _ensure_retryable gives up. With max_retries=3 that's 3 retries. With the default max_retries=inf (from result_backend_max_retries) it never gets there. @MehrazRumman measured 50+ sleeps (~200s) in their review.

Event block in on_failure:

[REPRO-2] event path sleeps=3

Even if _success_was_stored is fixed, the event block has the same problem for the same reason.

I wrote a minimal unit test locally (alongside the existing WorkerLostError tests in t/unit/worker/test_request.py) using a mocked backend whose _get_task_meta_for raises ConnectionError. I can submit it as a PR with strict xfail markers if that's the preferred approach.

Fix

Use _get_task_meta_for instead of get_state/get_result, which skips _ensure_retryable:

try:
    return backend._get_task_meta_for(self.id)['status'] == states.SUCCESS
except Exception:
    return False
try:
    meta = self.task.backend._get_task_meta_for(self.id)
    result = meta.get('result')
except Exception:
    result = None
self.send_event('task-succeeded',
                result=saferepr(result, self.task.resultrepr_maxsize),
                runtime=None)

A down backend then falls back right away, which is what the comment in _success_was_stored says the intent is.

Notes

  • Only tested with cache+memory:// and a mocked backend. Worth double-checking on real Redis/DB with the backend actually unreachable — the retry path is the same, but a real-network failure could behave differently.

cc @MehrazRumman @auvipy @ChristianMurphy

Activity

  1. MehrazRumman commented on Oct 25, 2026

    @MehrazRumman
    Contributor

    @majidkhazaei You already suggested a fix . Why don’t you open a PR, push your code and check against existing test. Then figure out what went wrong from my side, that will be pretty cool . I will be very happy to see your solution and review your PR.

  2. added this to the 5.7.0 milestone on Oct 25, 2026
  3. majidkhazaei commented on Oct 25, 2026

    @majidkhazaei
    ContributorAuthor

    Thanks for the nudge, @MehrazRumman.

    I've just opened a PR with the fix and the tests. Here is the link: #10960

  4. ChristianMurphy commented on Oct 26, 2026

    @ChristianMurphy
    Contributor

    Reproduced with a real Redis result backend that's down. The stall needs result_backend_always_retry=True. With the default, which is off, on_failure() doesn't wait on the backend on any version I tried. The title could name that setting.

    With it on, only the acks_late and reject_on_worker_lost setup is a regression. 5.6.3 and 5.7.0rc1 requeue the task without touching the backend, and main blocks in _success_was_stored(). #10960 fixes that case.

    With default task settings, the worker also blocks on 5.6.3 and 5.7.0rc1, in mark_as_failure(). That stall is older than #10752, and #10960 doesn't change it. It may need its own issue, which the author mentioned opening on #10960.

    What I ran

    Request.on_failure() with a WorkerLostError, and the Redis result backend's port closed. result_backend_max_retries is left at its default, which has 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 (933183f) default finishes blocked in _success_was_stored()
    main (933183f) acks_late, reject_on_worker_lost finishes blocked in _success_was_stored()
    #10960 (efefb55) default finishes blocked in mark_as_failure()
    #10960 (efefb55) acks_late, reject_on_worker_lost finishes requeues

    "Finishes" means on_failure() was done in under 0.1 s. With always_retry off and default task settings, it raises the ConnectionError from mark_as_failure() on every version.

  5. changed the title [-]Worker stalls on _success_was_stored when the result backend is down[/-] [+]Worker stalls on _success_was_stored when result_backend_always_retry=True and the result backend is down[/+] on Oct 26, 2026
  6. majidkhazaei commented on Oct 26, 2026

    @majidkhazaei
    ContributorAuthor

    Thanks for the thorough cross-version verification, @ChristianMurphy. The table makes the regression-vs-pre-existing split unambiguous, and it confirms exactly what #10960 is meant to do: restore 5.6.3 behavior.

    Reading the table column-wise, #10960 (efefb55) matches 5.6.3 on both rows:

    So the PR removes the regression without introducing a new block or changing the older mark_as_failure() path.

    Actions:

    1. Updated both titles to name result_backend_always_retry=True.
    2. The mark_as_failure() stall is out of scope here. I'll open a separate issue for it (tracking the approaches discussed: bounded retry budget from the main process, deferred write, or a time-based budget instead of a fixed count).

    On "Finishes vs raises": agreed — with always_retry off, mark_as_failure() raises ConnectionError on every version, which is the expected behavior when retries are disabled. The stall only happens with always_retry=True.

    Thanks again for testing against real Redis across 5.6.3, 5.7.0rc1, main, and the PR — this is exactly the kind of verification that makes the scope clear.

  7. modified the milestones: 5.7.0, 5.7.x on Oct 31, 2026
  8. modified the milestones: 5.7.x, 5.7.0 on Oct 31, 2026
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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions