Skip to content

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

Closed

Description

@tejas-ae

In celery/worker/request.py, the on_timeout handler treats soft and hard timeouts asymmetrically:

def on_timeout(self, soft, timeout):
    if soft:
        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 updated
        signals.task_failure.send(...)           # signal fired
        if self._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:

  1. Is this asymmetry intentional — i.e., is a soft timeout explicitly not a failure?
  2. If so, should the docs more prominently state that soft timeouts don't update the backend or invoke on_failure?
  3. 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?

File: celery/worker/request.py, lines 526–548.

Activity

  1. JiwaniZakir commented on Mar 15, 2026

    @JiwaniZakir

    Looking into this — will submit a fix if I can confirm the issue.

  2. codybarrett108-pixel commented on Mar 17, 2026

    @codybarrett108-pixel
  3. auvipy commented on Mar 29, 2026

    @auvipy
    Member
  4. ChickenBenny commented on Mar 29, 2026

    @ChickenBenny
    Contributor

    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
    
  5. ChickenBenny commented on Mar 29, 2026

    @ChickenBenny
    Contributor

    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!

  6. auvipy commented on Mar 30, 2026

    @auvipy
    Member

    please feel free to come with the enhancement PR

  7. added this to the 5.7.0 milestone on Mar 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

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions