Skip to content

Issue with chord group task exceptions and Redis backend #5229

Open

Description

@jessecooper

Checklist

  • I have included the output of celery -A proj report in 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 master branch of Celery.

software -> celery:4.2.0 (windowlicker) kombu:4.2.1 py:2.7.15
billiard:3.5.0.3 py-amqp:2.2.2
platform -> system:Darwin arch:64bit imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:pyamqp results:redis://localhost:6379/0

task_serializer: 'pickle'
task_soft_time_limit: 3600
redis_max_connections: None
task_bucket: 'tasks'
enable_utc: True
beat_scheduler:
beat_max_loop_interval: 5
worker_redirect_stdouts: False
accept_content: ['pickle']
result_serializer: 'pickle'
broker_pool_limit: 10
db_port: '8091'
task_acks_late: False
result_expires: 86400
task_track_started: True
task_routes: {
'': { 'queue': 'priority.high'}}
str_to_bool: <function str_to_bool at 0x10744daa0>
db_user: 'i'
db_host: 'localhost'
broker_url: u'amqp://guest:********@localhost:5672//'
result_backend: u'redis://localhost:6379/0'

Steps to reproduce

testrunner.py

from celery import group, chain, chord
from tasks import add, raise_err, task_on_complete

chord_group = [
    raise_err.s(),
]

celery_chords = [
    chord(
        group(
            task for task in chord_group
        ), task_on_complete.s()
    ),
    add.s(1,2) # the problem task
]

chord_chains = chain(chord for chord in celery_chords)
async_resp = chord_chains.apply_async()

if async_resp.ready():
    print(async_resp.get())

tasks.py

from celery import Celery

app = Celery(
    'tasks',
    backend='redis://localhost',
    broker='pyamqp://guest@localhost//',
    result_expire = 3000,
    task_serializer = 'pickle',
    accept_content = ['pickle'],
    result_serializer = 'pickle'
)

@app.task(bind=True)
def add(self, x, y):
    return x + y

@app.task(bind=True)
def raise_err(self):
    if True:
        raise Exception('except')

@app.task
def task_on_complete(*args):
    for arg in args:
        print(args)

Expected behavior

Tasks exception is handled properly and the state of the task is set to failed on the backend. No crash to the celery worker.

Actual behavior

[2018-12-10 13:46:07,354: ERROR/MainProcess] Pool callback raised exception: TypeError('sequence item 1: expected string, NoneType found',)
Traceback (most recent call last):
  File "/Users/jesse.cooper/Code/Python/env/lib/python2.7/site-packages/billiard/pool.py", line 1747, in safe_apply_callback
    fun(*args, **kwargs)
  File "/Users/jesse.cooper/Code/Python/celery/worker/request.py", line 365, in on_failure
    self.id, exc, request=self, store_result=self.store_errors,
  File "/Users/jesse.cooper/Code/Python/celery/backends/base.py", line 158, in mark_as_failure
    self.on_chord_part_return(request, state, exc)
  File "/Users/jesse.cooper/Code/Python/celery/backends/redis.py", line 306, in on_chord_part_return
    ChordError('Join error: {0!r}'.format(exc)),
  File "/Users/jesse.cooper/Code/Python/celery/backends/base.py", line 215, in chord_error_from_stack
    return backend.fail_from_current_stack(callback.id, exc=exc)
  File "/Users/jesse.cooper/Code/Python/celery/backends/base.py", line 222, in fail_from_current_stack
    self.mark_as_failure(task_id, exc, ei.traceback)
  File "/Users/jesse.cooper/Code/Python/celery/backends/base.py", line 155, in mark_as_failure
    traceback=traceback, request=request)
  File "/Users/jesse.cooper/Code/Python/celery/backends/base.py", line 301, in store_result
    request=request, **kwargs)
  File "/Users/jesse.cooper/Code/Python/celery/backends/base.py", line 646, in _store_result
    self.set(self.get_key_for_task(task_id), self.encode(meta))
  File "/Users/jesse.cooper/Code/Python/celery/backends/base.py", line 552, in get_key_for_task
    self.task_keyprefix, key_t(task_id), key_t(key),
TypeError: sequence item 1: expected string, NoneType found

This seems to be an issue with the request.chord dict getting passed to the maybe_signature() method in celery/backends/redis.py on line 358.

callback = maybe_signature(request.chord, app=app)

This is then passed into Signature.from_dict(). Here there is no verification code on the dictionary so it creates a signature without an ID property. This then gets passed into self.chord_error_from_stack()

    def chord_error_from_stack(self, callback, exc=None):
        # need below import for test for some crazy reason
        from celery import group  # pylint: disable
        app = self.app
        try:
            backend = app._tasks[callback.task].backend
        except KeyError:
            backend = self
        try:
            group(
                [app.signature(errback)
                 for errback in callback.options.get('link_error') or []],
                app=app,
            ).apply_async((callback.id,))
        except Exception as eb_exc:  # pylint: disable=broad-except
            return backend.fail_from_current_stack(callback.id, exc=eb_exc)
        else:
            return backend.fail_from_current_stack(callback.id, exc=exc)

Then this method tries a few times to use callback.id to set the failed state on the backend. Since there is no callback.id property these generates and internal exception. In celery 4.1.1 this does not crash the worker but the task is never set to a failed state it is always stuck in a started state. In celery 4.2.0 (master) this causes the worker to crash but the cleanup processes sets the state of the task to failed.

A hacky work around is to add this to check if callback.id is None and if so set it to the desired task id held inside of callback.tasks[n].id

            if callback.id is None:
                callback.id = callback.tasks[1].id

Activity

  1. thedrow commented on Dec 11, 2018

    @thedrow
    Contributor

    Do you happen to have a test case that will help me reproduce the problem?

  2. jessecooper commented on Dec 12, 2018

    @jessecooper
    Author

    I have updated the steps to reproduce section with a test case you can use to reproduce the issue. @thedrow

  3. iven commented on May 6, 2019

    @iven

    Same problem to me, and I can reproduce using the test case attached. Could anyone look into this?

    Also is there a workaround without having to modify celery code? Thanks.

  4. modified the milestones: 4.4.0, 4.5 on May 6, 2019
  5. modified the milestones: 4.5, 4.4.x on Dec 16, 2019
  6. modified the milestones: 4.4.x, 5.3 on Feb 18, 2021
  7. auvipy commented on Feb 18, 2021

    @auvipy
    Member

    can anyone check the master branch again?

  8. ahopkins commented on Feb 18, 2021

    @ahopkins

    I actually am dealing with a variant of this issue right now (v5.0.3). Will try it on master and report back.

  9. barakalon commented on Jan 24, 2022

    @barakalon

    I think this (or some form of this) is still an issue: #7261

  10. wochinge commented on Sep 5, 2022

    @wochinge
    Contributor

    @ahopkins Did you get this solved in the end?

  11. ahopkins commented on Sep 5, 2022

    @ahopkins

    😬 I feel like I did as I am no longer dealing with this. But TBH I do not remember the resolution.

  12. modified the milestones: 5.3, Future on Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions