You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Soft time limit does not trigger on_failure callbacks or update the result backend #10194
In celery/worker/request.py, the on_timeout handler treats soft and hard timeouts asymmetrically:
defon_timeout(self, soft, timeout):
ifsoft:
warn('Soft time limit (%ss) exceeded for %s[%s]', timeout, self.name, self.id)
# --- nothing else ---else:
# hard timeout path:self.acknowledge()
self._log_error(...)
self.task.backend.mark_as_failure(...) # result backend updatedsignals.task_failure.send(...) # signal firedifself._does_info:
info('Task %s[%s] timed out after %ss', ...)
When a soft time limit fires, Celery only logs a warning. The task's on_failure method is not called, the result backend is not updated, and link_error / errback chains are not invoked.
This is problematic when:
A task uses on_failure to release locks, close connections, or send alerts
A workflow uses link_error chains that must run when a task doesn't complete
A developer who sets soft_time_limit expecting "give the task a chance to clean up, then treat it as failed" will find that the failure-handling path is never triggered unless they explicitly catch SoftTimeLimitExceeded themselves.
Questions:
Is this asymmetry intentional — i.e., is a soft timeout explicitly not a failure?
If so, should the docs more prominently state that soft timeouts don't update the backend or invoke on_failure?
Would it be appropriate to add an option (e.g. soft_timeout_is_failure=True) so callers can opt into full failure semantics on soft timeout?
This is not a bug — it's a misunderstanding of the dual-process architecture.
When a soft time limit fires:
Parent process: on_timeout(soft=True) logs a warning (notification only)
Child process: SoftTimeLimitExceeded is raised → caught by trace_task → handle_failure() → mark_as_failure, task.on_failure(), task_failure signal, and errbacks are all invoked
Parent process: on_success(failed=True) → on_failure(return_ok=True) → task-failed event emitted
Hard timeout is different: billiard sends SIGKILL to the child, killing it instantly. The parent must handle cleanup because the child process is dead.
Possible enhancement (not a fix): Add a task-soft-time-limit event in on_timeout(soft=True) for finer-grained real-time monitoring. But the current failure-handling semantics are already correct.
Documentation improvement: Clarify in on_timeout docstring and user guide that soft timeout failure handling happens in the child process via trace_task, not in the parent's on_timeout.
Full Path Comparison: Soft vs Hard Timeout
Soft
TimeoutHandler (parent thread)
└─ on_soft_timeout(job)
├─ job.handle_timeout(soft=True)
│ └─ Request.on_timeout(soft=True) → only warn()
└─ os.kill(child_pid, SIGUSR1)
│
▼
Child process
soft_timeout_sighandler() → raise SoftTimeLimitExceeded()
│
▼
trace_task(): except Exception as exc ← SoftTimeLimitExceeded IS-A Exception ✅
└─ TraceInfo.handle_failure()
├─ backend.mark_as_failure() ✅ backend updated
├─ task.on_failure() ✅ on_failure method called
├─ signals.task_failure.send() ✅ signal fired
└─ errbacks dispatched ✅ errbacks run
│
▼ (child sends result via queue back to parent)
Parent: on_success(failed=True)
└─ on_failure(exc_info, return_ok=True)
└─ send_event('task-failed') ✅ monitoring event sent
Hard
TimeoutHandler (parent thread)
└─ on_hard_timeout(job)
├─ job._set(success=False, ExceptionInfo(TimeLimitExceeded))
│ └─ triggers error_callback = Request.on_failure(exc_info, return_ok=False)
│ ├─ backend.mark_as_failure() ✅ backend updated
│ ├─ signals.task_failure.send() ✅ signal fired
│ └─ send_event('task-failed') ✅ monitoring event sent
│ (no task.on_failure(), no errbacks — those only run in child)
├─ job.handle_timeout(soft=False)
│ └─ Request.on_timeout(soft=False)
│ ├─ task_ready(self)
│ ├─ backend.mark_as_failure() [2nd call — idempotent]
│ └─ ack / reject per acks_late
└─ _trywaitkill(process)
├─ os.kill(child_pid, SIGTERM) ← can be caught, but child is mid-kill
├─ sleep(0.1)
└─ os.kill(child_pid, SIGKILL) ← cannot be caught, blocked, or ignored
If my analysis above aligns with the core team's design expectations, I'd be happy to open a follow-up PR to address these gaps. Here is what I plan to do:
Update Documentation (User Guide): Explicitly document the asymmetric failure semantics between soft and hard time limits. I will clarify that hard timeouts bypass task.on_failure() and errbacks, so developers know to rely on soft timeouts for guaranteed cleanup.
Enhance on_timeout(soft=False): Currently, the hard timeout path only does mark_as_failure and ack, completely skipping task.on_failure(), errbacks, and signals.task_failure. I will add the logic to trigger these missing failure paths in the parent process so that hard timeouts behave more consistently with standard task failures.
Activity
JiwaniZakir commentedon Mar 15, 2026
Looking into this — will submit a fix if I can confirm the issue.
codybarrett108-pixel commentedon Mar 17, 2026
auvipy commentedon Mar 29, 2026
@ChickenBenny
ChickenBenny commentedon Mar 29, 2026
This is not a bug — it's a misunderstanding of the dual-process architecture.
When a soft time limit fires:
Parent process:
on_timeout(soft=True)logs a warning (notification only)Child process: SoftTimeLimitExceeded is raised → caught by trace_task →
handle_failure()→mark_as_failure,task.on_failure(),task_failuresignal, and errbacks are all invokedParent process:
on_success(failed=True)→on_failure(return_ok=True)→ task-failed event emittedHard timeout is different: billiard sends SIGKILL to the child, killing it instantly. The parent must handle cleanup because the child process is dead.
Possible enhancement (not a fix): Add a task-soft-time-limit event in
on_timeout(soft=True)for finer-grained real-time monitoring. But the current failure-handling semantics are already correct.Documentation improvement: Clarify in
on_timeoutdocstring and user guide that soft timeout failure handling happens in the child process viatrace_task, not in the parent'son_timeout.Full Path Comparison: Soft vs Hard Timeout
ChickenBenny commentedon Mar 29, 2026
If my analysis above aligns with the core team's design expectations, I'd be happy to open a follow-up PR to address these gaps. Here is what I plan to do:
Update Documentation (User Guide): Explicitly document the asymmetric failure semantics between soft and hard time limits. I will clarify that hard timeouts bypass
task.on_failure()and errbacks, so developers know to rely on soft timeouts for guaranteed cleanup.Enhance
on_timeout(soft=False): Currently, the hard timeout path only does mark_as_failure and ack, completely skipping task.on_failure(), errbacks, and signals.task_failure. I will add the logic to trigger these missing failure paths in the parent process so that hard timeouts behave more consistently with standard task failures.Let me know what you think, @auvipy!
auvipy commentedon Mar 30, 2026
please feel free to come with the enhancement PR