Skip to content

fix(worker): store no failure for a task requeued by a time limit - #11185

Open
ChristianMurphy wants to merge 1 commit into
celery:mainfrom
ChristianMurphy:fix/time-limit-requeue-keeps-task-pending
Open

ChristianMurphy wants to merge 1 commit into
celery:mainfrom
ChristianMurphy:fix/time-limit-requeue-keeps-task-pending

Conversation

@ChristianMurphy

Copy link
Copy Markdown
Contributor

Note: Before submitting this pull request, please review our contributing
guidelines
& Anti AI spamming Guidelines. Before opening an AI based PR make sure you asked
in the discussion forum or in the related open issues first.

Description

Refs #11090. This is the requeue case that @auvipy asked me to take on in #11105. With acks_late on and task_acks_on_timeout off, a task that hits its hard time limit is requeued and runs again, but that run was still stored as FAILURE and its errbacks ran. A caller got TimeLimitExceeded for a task that then ran again and could succeed.

Request.on_timeout() now skips its failure handling when it requeues the message, as Request.on_failure() already does. Nothing is stored for that run, and no errback, task_failure signal, task-failed event, Task.on_failure or after_return runs. The "Hard time limit" log line and the task-rejected event stay.

The trade-off is a task that exceeds its limit on every run. It is requeued each time, as before, and now only the log line shows it, because no FAILURE is stored between runs. On a RabbitMQ quorum queue the message is dropped after 20 redeliveries by default, and then nothing is stored for the task. The docs say so.

This is separate from #11183, which stops the result store wait before the worker's own time limits. The two change different code paths and merge without a conflict.

The store and the errbacks on a requeue date from 5.5.0, when #9626 added the requeue. The signal, the event and the two task hooks date from 5.7.0 (#10226).

A late-ack task over its hard limit once, acks_on_timeout off main This PR
get() raises TimeLimitExceeded returns the second run's result
Errback calls 1 0
task_failure signals 1 0
Times the task ran 2 2

That is 3 of 3 runs on Redis 7.4 and 3 of 3 on Valkey 8.1, with the prefork pool. A task with acks_on_timeout on behaves the same on main and here: FAILURE, one errback call, one run.

Most of the diff is tests and docs: code +5, tests +83/−25, docs +23.

  • One unit test covers both orders. The prefork pool calls on_failure() and then on_timeout(), and the gevent pool calls only on_timeout(). It fails on main in both.
  • test_hard_time_limit_requeued_by_on_failure_is_handled_by_on_timeout asserted the old behavior, so the new test replaces it. The requeue case in test_on_hard_timeout_acks_on_failure_or_timeout now expects PENDING, not FAILURE, for the same reason.
  • Each of the six one-line changes I made to the new code fails a unit test.
  • A smoke test runs a task that exceeds its limit on the first delivery only. It passes here and fails on main, on the Redis and the RabbitMQ broker.
What I ran
  • tox -e 3.13-unit: 5783 passed, 34 skipped, 1 xfailed.
  • t/unit/worker/test_request.py with main's celery/worker/request.py: 3 fail, the new test in both orders and the changed assertion.
  • The worker runs: Celery main (acb1428d2) against this branch, kombu main (9cd1cef8), Python 3.13, prefork with 2 processes, a task with time_limit=2.
  • tox -e 3.13-smoke with the two hard time limit smoke tests: 4 passed here. On main the new one fails with TimeLimitExceeded, on both brokers.
  • Not run: the gevent pool with a worker, and Python versions other than 3.13.

Problem: with acks_late on and acks_on_timeout off, a hard time limit
requeues the message and the task runs again. Request.on_timeout()
still stored FAILURE and called the errbacks for that run. Since
5.7.0 it also sent task_failure and task-failed and called
Task.on_failure and after_return. A caller got TimeLimitExceeded for
a task that then ran again and could succeed.

Goal: a run whose message is requeued leaves no failure behind, as
Request.on_failure() already does when it requeues.

Changes:
- on_timeout() skips the failure handling when the message is
  requeued. The "Hard time limit" log line stays.
- A unit test covers the prefork order, where on_failure() runs
  first, and the gevent order, where only on_timeout() runs.
- A smoke test runs a task that exceeds its limit on the first
  delivery only.
- The docs for task_acks_on_timeout and the time limit note in the
  tasks guide describe the new behavior.

Notes: the unit test that asserted the old behavior,
test_hard_time_limit_requeued_by_on_failure_is_handled_by_on_timeout,
is replaced by the new one. The requeue case in
test_on_hard_timeout_acks_on_failure_or_timeout now expects PENDING,
not FAILURE.

Refs: celery#11090
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.82%. Comparing base (acb1428) to head (ad522a1).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11185      +/-   ##
==========================================
+ Coverage   91.81%   91.82%   +0.01%     
==========================================
  Files         153      153              
  Lines       22296    22316      +20     
  Branches     2794     2799       +5     
==========================================
+ Hits        20471    20492      +21     
+ Misses       1530     1529       -1     
  Partials      295      295              
Flag Coverage Δ
unittests 91.80% <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.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant