Skip to content

Commit

Permalink
fix: Pass back real result for single task chains
Browse files Browse the repository at this point in the history
When chains are delayed, they are first frozen as part of preparation
which causes the sub-tasks to also be frozen. Afterward, the final (0th
since we reverse the tasks/result order when freezing) result object
from the freezing process would be passed back to the caller. This
caused problems in signaling completion of groups contained in chains
because the group relies on a promise which is fulfilled by a barrier
linked to each of its applied subtasks. By constructing two
`GroupResult` objects (one during freezing, one when the chain sub-tasks
are applied), this resulted in there being two promises; only one of
which would actually be fulfilled by the group subtasks.

This change ensures that in the special case where the final task of a
chain is a group, we pass back the `GroupResult` object constructed when
the group was actually applied. The caller can then await the result
confidently!
  • Loading branch information
maybe-sybr committed Oct 13, 2020
1 parent e4c8929 commit ddb2985
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions celery/canvas.py
Expand Up @@ -662,8 +662,16 @@ def run(self, args=None, kwargs=None, group_id=None, chord=None,
first_task = tasks.pop()
options = _prepare_chain_from_options(options, tasks, use_link)

first_task.apply_async(**options)
return results[0]
real_result = first_task.apply_async(**options)
# If we only have a single task, it may be important that we pass
# the real result object rather than the one obtained via freezing.
# e.g. For `GroupResult`s, we need to pass back the result object
# which will actually have its promise fulfilled by the subtasks,
# something that will never occur for the frozen result.
if not tasks:
return real_result
else:
return results[0]

def freeze(self, _id=None, group_id=None, chord=None,
root_id=None, parent_id=None, group_index=None):
Expand Down

0 comments on commit ddb2985

Please sign in to comment.