Skip to content

fix: send the task failure in hard timeout - #10226

Merged
auvipy merged 8 commits into
celery:mainfrom
ChickenBenny:fix/hard-timeout-failure-hooks
Mar 31, 2026
Merged

auvipy merged 8 commits into
celery:mainfrom
ChickenBenny:fix/hard-timeout-failure-hooks

Conversation

@ChickenBenny

@ChickenBenny ChickenBenny commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Hard time limits now invoke task.on_failure(), task.after_return(), and the task_failure signal in the parent
worker process, matching the behavior of soft time limits.

Previously, when a hard time limit fired, only backend.mark_as_failure() was called. task.on_failure(),
task.after_return(), signals.task_failure, and the task-failed monitoring event were silently skipped, making
it impossible for users to rely on standard failure hooks for cleanup or observability when a hard timeout
occurred.

Closes #10194


Root Cause

Request.on_timeout(soft=False) in celery/worker/request.py only called mark_as_failure() and handled ack/reject. The failure side-effects that the normal task tracer triggers were never wired up for the hard-timeout path.

Note: errbacks were already triggered via mark_as_failure(call_errbacks=True) before this fix.

┌───────────────────────────┬─────────────────┬──────────────────────────┬──────────────────────┐
│        Side-effect        │  Soft timeout   │  Hard timeout (before)   │ Hard timeout (after) │
├───────────────────────────┼─────────────────┼──────────────────────────┼──────────────────────┤
│ backend.mark_as_failure() │ ✅ (via tracer) │ ✅                       │ ✅                   │
├───────────────────────────┼─────────────────┼──────────────────────────┼──────────────────────┤
│ errbacks                  │ ✅              │ ✅ (via mark_as_failure) │ ✅                   │
├───────────────────────────┼─────────────────┼──────────────────────────┼──────────────────────┤
│ task.on_failure()         │ ✅              │ ❌                       │ ✅                   │
├───────────────────────────┼─────────────────┼──────────────────────────┼──────────────────────┤
│ task.after_return()       │ ✅              │ ❌                       │ ✅                   │
├───────────────────────────┼─────────────────┼──────────────────────────┼──────────────────────┤
│ signals.task_failure      │ ✅              │ ❌                       │ ✅                   │
├───────────────────────────┼─────────────────┼──────────────────────────┼──────────────────────┤
│ task-failed event         │ ✅              │ ❌                       │ ✅                   │
└───────────────────────────┴─────────────────┴──────────────────────────┴──────────────────────┘

Changes

celery/worker/request.py

After mark_as_failure(), the hard-timeout path now:

  1. Constructs an ExceptionInfo by raising and catching the TimeLimitExceeded exception (gives downstream hooks a proper traceback).
  2. Calls self.task.on_failure(exc, self.id, self.args, self.kwargs, einfo).
  3. Calls self.task.after_return(states.FAILURE, ...) if the task overrides it (guarded by task_has_custom, consistent with the tracer).
  4. Sends signals.task_failure.
  5. Emits the task-failed monitoring event using get_pickled_exception (consistent with Request.on_failure).

All additions are guarded by the existing if not state.should_terminate block so cold-shutdown behavior is unchanged.

docs/userguide/tasks.rst

  • Added a .. note:: block documenting hard vs soft timeout failure semantics with versionchanged:: 5.7.
  • Updated the custom request section to reflect that Task.on_failure now fires for hard timeouts too.

@codecov

codecov Bot commented Mar 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.25%. Comparing base (1fe2a08) to head (3213af8).
⚠️ Report is 72 commits behind head on main.

Files with missing lines Patch % Lines
celery/worker/request.py 94.44% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10226   +/-   ##
=======================================
  Coverage   88.24%   88.25%           
=======================================
  Files         153      153           
  Lines       19591    19606   +15     
  Branches     2252     2254    +2     
=======================================
+ Hits        17289    17303   +14     
  Misses       2004     2004           
- Partials      298      299    +1     
Flag Coverage Δ
unittests 88.22% <94.44%> (+<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 requested review from auvipy and Copilot March 30, 2026 17:45
@auvipy auvipy added this to the 5.7.0 milestone Mar 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Ensures hard time limits trigger the same failure side-effects as other task failures (task hooks, task_failure signal, and monitoring events), and documents the updated semantics.

Changes:

  • Update Request.on_timeout(soft=False) to invoke Task.on_failure, Task.after_return (when overridden), emit signals.task_failure, and send the task-failed event after marking the task as failed.
  • Add unit tests covering hard-timeout failure hooks, signal emission, event emission, and after_return.
  • Document hard vs soft time limit semantics and update the custom request guidance.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
celery/worker/request.py Adds failure hook/signal/event emission to the hard-timeout path.
t/unit/worker/test_request.py Adds regression tests asserting the new hard-timeout failure side-effects.
docs/userguide/tasks.rst Documents the updated hard-timeout behavior and adjusts related guidance.

Comment thread celery/worker/request.py Resolved
Comment thread celery/worker/request.py Resolved
Comment thread docs/userguide/tasks.rst Resolved
Comment thread celery/worker/request.py Resolved
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.

Soft time limit does not trigger on_failure callbacks or update the result backend

3 participants