Why does Celery NOT throw an Exception when the underlying task throws one

Kevin Meyer picture Kevin Meyer · Feb 4, 2013 · Viewed 19.5k times · Source

Celery doesn't seem to be handling exceptions properly.

If I have task:

def errorTest():
    raise Exception()

and then I call

r = errorTest.delay()
In [8]: r.result

In [9]: r.state
Out[9]: 'PENDING'

And it will hang like this indefinitely.

Going and checking the logs shows that the error IS getting thrown in the task (and if you want the message, ask), and I know that the backend and everything is set up properly because other tasks just work and return results correctly.

Is there something funky that I need to do to catch exceptions in Celery?

/Celery version is 3.0.13, broker is RabbitMQ running on my local machine

Answer

seddonym picture seddonym · Jun 16, 2014

If you are running Celery with the CELERY_ALWAYS_EAGER set to True, then make sure you include this line in your settings too:

CELERY_EAGER_PROPAGATES_EXCEPTIONS = True

http://docs.celeryproject.org/en/latest/configuration.html#celery-eager-propagates-exceptions