Neither on_failure nor after_return are called when propagate is True
Checklist
- I have read the relevant section in the contribution guide on reporting bugs.
- I have checked the issues list for similar or identical bug reports.
- I have checked the pull requests list for existing proposed fixes.
- I have checked the commit log to find out if the bug was already fixed in the master branch.
- I have included all related issues and possible duplicate issues in this issue (If there are none, check this box anyway).
Mandatory Debugging Information
- I have included the output of
celery -A proj reportin the issue. (if you are not able to do this, then at least specify the Celery version affected). - I have verified that the issue exists against the
masterbranch of Celery. - I have included the contents of
pip freezein the issue. - I have included all the versions of all the external dependencies required to reproduce this bug.
Related Issues and Possible Duplicates
Related Issues
- #2132
- #3728
Environment & Settings
Celery version: 4.4.0rc2
Steps to Reproduce
Define after_return or on_failure in a custom Task subclass. Then use this subclass as the base class of a task.
Then define a task and do mytask.apply(..., throw=True) on it. The after_return and on_failure handlers aren't called.
Alternatively a normal mytask.apply_async(...) call also elicits this behavior when celery_always_eager and celery_eager_propagates_exceptions are true.
The problem is here (in celery.app.trace):
def on_error(request, exc, uuid, state=FAILURE, call_errbacks=True):
if propagate:
raise
I = Info(state, exc)
R = I.handle_error_state(
task, request, eager=eager, call_errbacks=call_errbacks,
)
return I, R, I.state, I.retval
def trace_task(uuid, args, kwargs, request=None):
...
# -*- TRACE -*-
try:
R = retval = fun(*args, **kwargs)
state = SUCCESS
except Exception as exc:
I, R, state, retval = on_error(task_request, exc, uuid)
except BaseException:
raise
...
# -* POST *-
if state not in IGNORE_STATES:
if task_after_return:
task_after_return(
state, retval, uuid, args, kwargs, None,
)Note that in on_error (which is called if the task function raises an Exception) if propagate is true then the exception is always raised before on_failure or after_return are called. Specifically on_failure would be called in on_error after raising the exception and after_return would be called after on_error handles the error raised from the task function.
I'm not sure if this is the intended behavior. If it is then it's unclear from the documentation:
on_failure(exc, task_id, args, kwargs, einfo)[source]
Error handler.
This is run by the worker when the task fails. after_return(status, retval, task_id, args, kwargs, einfo)[source]
Handler called after the task returns.This issue exists in master at the time that this bug was filed and appears to have been in Celery for at least the last 4 years.
Source: celery/celery