Repository: celery/celery

Issue:
Nested group(chain(group)) fails
<!--
Please fill this template entirely and do not erase parts of it.
We reserve the right to close without a response
bug reports which are incomplete.
-->
# Checklist
<!--
To check an item on the list replace [ ] with [x].
-->

- [x] I have read the relevant section in the
  [contribution guide](http://docs.celeryproject.org/en/latest/contributing.html#other-bugs)
  on reporting bugs.
- [x] I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22)
  for similar or identical bug reports.
- [ ] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22)
  for existing proposed fixes.
- [ ] I have checked the [commit log](https://github.com/celery/celery/commits/master)
  to find out if the bug was already fixed in the master branch.
- [x] I have included all related issues and possible duplicate issues
  in this issue (If there are none, check this box anyway).

## Mandatory Debugging Information

- [x] 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.
- [ ] I have included the contents of ``pip freeze`` in the issue.
- [ ] I have included all the versions of all the external dependencies required
  to reproduce this bug.

## Optional Debugging Information
<!--
Try some of the below if you think they are relevant.
It will help us figure out the scope of the bug and how many users it affects.
-->
- [x] I have tried reproducing the issue on more than one Python version
  and/or implementation.
- [ ] I have tried reproducing the issue on more than one message broker and/or
  result backend.
- [ ] I have tried reproducing the issue on more than one version of the message
  broker and/or result backend.
- [ ] I have tried reproducing the issue on more than one operating system.
- [ ] I have tried reproducing the issue on more than one workers pool.
- [ ] I have tried reproducing the issue with autoscaling, retries,
  ETA/Countdown & rate limits disabled.
- [ ] I have tried reproducing the issue after downgrading
  and/or upgrading Celery and its dependencies.

## Related Issues and Possible Duplicates
<!--
Please make sure to search and mention any related issues
or possible duplicates to this issue as requested by the checklist above.

This may or may not include issues in other repositories that the Celery project
maintains or other repositories that are dependencies of Celery.

If you don't know how to mention issues, please refer to Github's documentation
on the subject: https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests
-->

#### Related Issues

- None

#### Possible Duplicates

- Subsequent groups within a chain fail #3585
- Consecutive groups in chain fails #4848 
- task/group chains fails in some scenarios #5467

## Environment & Settings
<!-- Include the contents of celery --version below -->
**Celery version**: 4.3.0
<!-- Include the output of celery -A proj report below -->
<details>
<summary><b><code>celery report</code> Output:

software -> celery:4.3.0 (rhubarb) kombu:4.5.0 py:3.5.3
billiard:3.6.0.0 redis:3.2.1
platform -> system:Linux arch:64bit
kernel version:4.19.32-1-MANJARO imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:redis://redis:6379/1

broker_url: redis://redis:6379/1
result_backend: redis://redis:6379/1

</b></summary>
<p>

```
```

</p>
</details>

# Steps to Reproduce
## Minimally Reproducible Test Case
<!--
Please provide a reproducible test case.
Refer to the Reporting Bugs section in our contribution guide.

We prefer submitting test cases in the form of a PR to our integration test suite.
If you can provide one, please mention the PR number below.
If not, please attach the most minimal code example required to reproduce the issue below.
If the test case is too large, please include a link to a gist or a repository below.
-->

<details>
<p>

```python
from celery import  group, chain
from tasks import task as t

# failing sequence
task = group([ t.si(),t.si(), chain( t.si(), group([ t.si(), t.si()]))])

# working sequence
task = group([ t.si(),t.si(), chain( t.si(), group([ t.si(), t.si()]), t.si())])

async_result = task.apply_async()
result = async_result.get()


```

</p>
</details>

# Expected Behavior

Calling `get` return group result. 

# Actual Behavior
<!--
Describe in detail what actually happened.
Please include a backtrace and surround it with triple backticks (```).
In addition, include the Celery daemon logs, the broker logs,
the result backend logs and system logs below if they will help us debug
the issue.
-->

All task finish success but calling `.get()` fail with traceback:

```
Traceback (most recent call last):
  File "/my_app.py", line 111, in add_to_queue
    result = async_result.get()
  File "/root/.cache/pypoetry/virtualenvs/my_app/lib/python3.7/site-packages/celery/result.py", line 697, in get
    on_interval=on_interval,
  File "/root/.cache/pypoetry/virtualenvs/my_app/lib/python3.7/site-packages/celery/result.py", line 815, in join_native
    on_message, on_interval):
  File "/root/.cache/pypoetry/virtualenvs/my_app/lib/python3.7/site-packages/celery/backends/asynchronous.py", line 137, in iter_native
    if node._cache:
AttributeError: 'GroupResult' object has no attribute '_cache'

```


Discussion:
This can be reproduced on master and I used RabbitMQ as broker.
Because nested `group(signature, chain(group))` get results like `GroupResult(AsyncResult, GroupResult)`, but the GroupResult do not have attribute `_cache`.
The problem is, you can not simplely change the statetment to `if hasattr(node, '_cache') and node._cache`, for that will have new issue.
please provide a sample test case
@p-eli provide a test case that can be used.
First, the task file: `myapp.py`
```
from celery import Celery

app = Celery(
    'myapp',
    broker='amqp://guest@localhost//',
    backend='redis://localhost:6379/0'
)

@app.task
def t():
    print('t-t-t')


if __name__ == '__main__':
    app.start()
```
then send tasks by:
```
from celery import  group, chain
from myapp import t

# failing sequence
task = group([ t.si(),t.si(), chain( t.si(), group([ t.si(), t.si()]))])

# working sequence
task = group([ t.si(),t.si(), chain( t.si(), group([ t.si(), t.si()]), t.si())])

async_result = task.apply_async()
result = async_result.get()
```
@tothegump is this fixed?

Resolving patch:
diff --git a/celery/backends/asynchronous.py b/celery/backends/asynchronous.py
--- a/celery/backends/asynchronous.py
+++ b/celery/backends/asynchronous.py
@@ -134,7 +134,9 @@ def iter_native(self, result, no_ack=True, **kwargs):
         # into these buckets.
         bucket = deque()
         for node in results:
-            if node._cache:
+            if not hasattr(node, '_cache'):
+                bucket.append(node)
+            elif node._cache:
                 bucket.append(node)
             else:
                 self._collect_into(node, bucket)
@@ -142,7 +144,10 @@ def iter_native(self, result, no_ack=True, **kwargs):
         for _ in self._wait_for_pending(result, no_ack=no_ack, **kwargs):
             while bucket:
                 node = bucket.popleft()
-                yield node.id, node._cache
+                if not hasattr(node, '_cache'):
+                    yield node.id, node.children
+                else:
+                    yield node.id, node._cache
         while bucket:
             node = bucket.popleft()
             yield node.id, node._cache
diff --git a/celery/result.py b/celery/result.py
--- a/celery/result.py
+++ b/celery/result.py
@@ -819,9 +819,14 @@ def join_native(self, timeout=None, propagate=True,
         acc = None if callback else [None for _ in range(len(self))]
         for task_id, meta in self.iter_native(timeout, interval, no_ack,
                                               on_message, on_interval):
-            value = meta['result']
-            if propagate and meta['status'] in states.PROPAGATE_STATES:
-                raise value
+            if isinstance(meta, list):
+                value = []
+                for children_result in meta:
+                    value.append(children_result.get())
+            else:
+                value = meta['result']
+                if propagate and meta['status'] in states.PROPAGATE_STATES:
+                    raise value
             if callback:
                 callback(task_id, value)
             else:


Test patch:
diff --git a/t/integration/test_canvas.py b/t/integration/test_canvas.py
--- a/t/integration/test_canvas.py
+++ b/t/integration/test_canvas.py
@@ -120,6 +120,16 @@ def test_group_chord_group_chain(self, manager):
         assert set(redis_messages[4:]) == after_items
         redis_connection.delete('redis-echo')
 
+    @flaky
+    def test_group_result_not_has_cache(self, manager):
+        t1 = identity.si(1)
+        t2 = identity.si(2)
+        gt = group([identity.si(3), identity.si(4)])
+        ct = chain(identity.si(5), gt)
+        task = group(t1, t2, ct)
+        result = task.delay()
+        assert result.get(timeout=TIMEOUT) == [1, 2, [3, 4]]
+
     @flaky
     def test_second_order_replace(self, manager):
         from celery.five import bytes_if_py2