Skip to content

feat(worker): add task_retry_on_worker_lost to limit requeues after worker loss - #10843

Merged
auvipy merged 11 commits into
celery:mainfrom
abdullahmujahidali:feat/retry-on-worker-lost-9082
Oct 14, 2026
Merged

auvipy merged 11 commits into
celery:mainfrom
abdullahmujahidali:feat/retry-on-worker-lost-9082

Conversation

@abdullahmujahidali

Copy link
Copy Markdown
Contributor

Description

Fixes #9082 (see also discussion #8969)

With task_reject_on_worker_lost, a task whose worker dies is requeued with no limit, since the broker doesn't count requeues.

This adds an opt-in task_retry_on_worker_lost setting. When it's on, a lost worker retries the task like Task.retry() does, so max_retries and default_retry_delay apply. When retries run out, the task is marked as failed. Default is off, so existing behaviour doesn't change.

One open question: if sending the retry fails, the task is currently marked as failed. Would falling back to requeue be better?

@codecov

codecov Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.47%. Comparing base (ccec355) to head (c5feaf3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10843      +/-   ##
==========================================
+ Coverage   90.46%   90.47%   +0.01%     
==========================================
  Files         153      153              
  Lines       21216    21244      +28     
  Branches     2580     2583       +3     
==========================================
+ Hits        19192    19221      +29     
  Misses       1710     1710              
+ Partials      314      313       -1     
Flag Coverage Δ
unittests 90.45% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@auvipy

auvipy commented Oct 11, 2026

Copy link
Copy Markdown
Member

previous partial fix #9720

@abdullahmujahidali

Copy link
Copy Markdown
Contributor Author

Yes #9720 added the request context to task_failure for lost workers. This PR handles the other part from #8969 limiting how many times the task gets sent again.

@abdullahmujahidali
abdullahmujahidali marked this pull request as ready for review October 11, 2026 12:30
@auvipy auvipy modified the milestones: 5.7.x, 5.7.0 Oct 11, 2026

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also consider smoke or integration tests for this new config settings based feature?

@abdullahmujahidali

Copy link
Copy Markdown
Contributor Author

Added a smoke test that kills the worker on every run with task_reject_on_worker_lost and task_retry_on_worker_lost enabled it checks the task fails with WorkerLostError after max_retries instead of being requeued forever.

Passes locally with both Redis and RabbitMQ.

@auvipy

auvipy commented Oct 13, 2026

Copy link
Copy Markdown
Member

@MehrazRumman @GangEunzzang please review

@auvipy

auvipy commented Oct 14, 2026

Copy link
Copy Markdown
Member

@abdullahmujahidali did you checked this comment? #8969 (reply in thread) whats your view on that?

@auvipy

auvipy commented Oct 14, 2026

Copy link
Copy Markdown
Member

@caspervdw would you mind checking and reviewing this PR?

@abdullahmujahidali

Copy link
Copy Markdown
Contributor Author

@abdullahmujahidali did you checked this comment? #8969 (reply in thread) whats your view on that?

DLX approach works but only on RabbitMQ x-death is a RabbitMQ header and it needs acks_on_failure_or_timeout and reject_on_worker_lost turned off plus a custom pre-run hook.

PR uses Celery's own retries count so it works the same on Redis and SQS too (the smoke test runs on both Redis and RabbitMQ)

They don't really combine though retries strip the x-death headers (_filter_x_death_headers) so with this setting on the DLX count would reset each time. Since it's off by default existing DLX setups aren't affected. I can add a note about that to the docs if you think it's useful

@GangEunzzang

Copy link
Copy Markdown
Contributor

Looks good overall. I ran it against main with a prefork worker on Redis, killing the child with SIGKILL inside the task.

config main this PR
setting off (7 combinations of acks_late / reject_on_worker_lost / acks_on_failure_or_timeout) — same as main
acks_late + reject_on_worker_lost + retry_on_worker_lost, max_retries=2 requeued ~76 times in 15 s, stays PENDING runs 3 times (retries 0, 1, 2), then FAILURE / WorkerLostError
same, task in a chord header chord stays PENDING header runs 3 times, chord fails with WorkerLostError
killed only on the first run SUCCESS SUCCESS (second run has retries=1)

A few things I noticed, none blocking:

  1. A retry after a lost worker doesn't call Task.on_retry, send the task_retry signal or log anything, unlike self.retry(). Only the task-retried event and the RETRY state show it.
  2. On the open question: if publishing the retry fails, the message is acked and the task marked failed, even with task_reject_on_worker_lost on, where main would requeue it. Falling through to the existing reject/requeue path when the send fails would keep the task.
  3. It only covers a pool child dying. If the whole worker is killed (e.g. the container OOM-killed), the task ran 5 times over 5 restarts with retries=0 every time, on both branches. Might be worth one line in the docs next to the OOM example.
  4. Also worth stating in the docs: a lost task now waits default_retry_delay (180 s) instead of being requeued right away, these retries share max_retries with self.retry(), and it applies without acks_late too.

The five new tests fail on main and pass here. I didn't run the smoke test.

@auvipy

auvipy commented Oct 14, 2026

Copy link
Copy Markdown
Member

@abdullahmujahidali we need to address 1-4 from the last review

@abdullahmujahidali

Copy link
Copy Markdown
Contributor Author

Thanks @GangEunzzang @auvipy for the ping

Pushed changes for all four:

  1. A retry after a lost worker now calls on_retry, sends task_retry and logs the retry, same as self.retry()
  2. If sending the retry fails it now falls back to the normal reject_on_worker_lost path so with that on the message gets requeued like on main. The task is only failed when retries actually run out.
  3. Added a note that it only covers a pool child dying, not the whole worker or container being killed.
  4. Documented the default_retry_delay wait the shared max_retries with self.retry() and that it applies without acks_late

updated the unit tests to cover these.

@auvipy

auvipy commented Oct 14, 2026

Copy link
Copy Markdown
Member

Pushed changes for all four

@abdullahmujahidali couldnt see the pushed commit yet. did you forget to push?

@abdullahmujahidali

Copy link
Copy Markdown
Contributor Author

Pushed changes for all four

@abdullahmujahidali couldnt see the pushed commit yet. did you forget to push?

Sorry I forgot just pushed

@auvipy

auvipy commented Oct 14, 2026

Copy link
Copy Markdown
Member

I still cant see the changes in latest commit

@abdullahmujahidali

abdullahmujahidali commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

I still cant see the changes in latest commit

The changes are in aa409e4, the latest commit is just a merge from main so it doesn't show them:
aa409e4

@auvipy
auvipy merged commit c09b6ff into celery:main Oct 14, 2026
470 checks passed
@auvipy auvipy added the Worker label Oct 14, 2026
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.

Avoid infinite retries for reject_on_worker_lost

3 participants