Repository: celery/celery

Issue:
disable_sync_subtasks setting not being respected while using ResultSet
<!--
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.
- [x] 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.
- [x] 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).
- [x] 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.
-->
- [ ] 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.
- [x] 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

- #5330 

#### Possible Duplicates

- None

## 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:</b></summary>
<p>

```
software -> celery:4.3.0 (rhubarb) kombu:4.6.4 py:3.5.6
            billiard:3.6.1.0 py-amqp:2.5.1
platform -> system:Linux arch:64bit, ELF
            kernel version:4.19.71-1-lts imp:CPython
loader   -> celery.loaders.default.Loader
settings -> transport:amqp results:disabled
```

</p>
</details>

# Steps to Reproduce

## Required Dependencies
<!-- Please fill the required dependencies to reproduce this issue -->
* **Minimal Python Version**: 3.5.6
* **Minimal Celery Version**: 4.3.0
* **Minimal Kombu Version**: 4.6.4
* **Minimal Broker Version**: RabbitMQ 3.7.15
* **Minimal Result Backend Version**: 10.4.7-MariaDB
* **Minimal OS and/or Kernel Version**: Linux 4.19.71-1-lts

### Python Packages
<!-- Please fill the contents of pip freeze below -->
<details>
<summary><b><code>pip freeze</code> Output:</b></summary>
<p>

```
amqp==2.5.1
asn1crypto==0.24.0
Babel==2.7.0
bcrypt==3.1.7
billiard==3.6.1.0
celery==4.3.0
certifi==2019.9.11
cffi==1.12.3
chardet==3.0.4
cryptography==2.7
django-celery-results==1.1.2
flower==0.9.3
idna==2.8
importlib-metadata==0.22
kombu==4.6.4
more-itertools==7.2.0
mysqlclient==1.4.4
paramiko==2.6.0
pycparser==2.19
PyNaCl==1.3.0
pytz==2019.2
requests==2.22.0
requests-toolbelt==0.9.1
six==1.12.0
SQLAlchemy==1.3.8
tornado==5.1.1
urllib3==1.25.3
vine==1.3.0
websockets==7.0
zipp==0.6.0
```

</p>
</details>

### Other Dependencies
<!--
Please provide system dependencies, configuration files
and other dependency information if applicable
-->
<details>
<p>
N/A
</p>
</details>

## 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
@app.task
def add(x, y):
    return x + y

@app.task
def test():
    result_set = ResultSet([])
    add_tasks = add.starmap((i, i) for i in range(10))
    add_result = add_tasks.apply_async()
    result_set.add(add_result)
    return result_set.get(disable_sync_subtasks=False)
```

</p>
</details>

# Expected Behavior
<!-- Describe in detail what you expect to happen -->

The tasks runs successfully, with the acceptance of possible deadlock

# 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.
-->

```
Traceback (most recent call last):
  File "/home/gsfish/.pyenv/versions/scan_detect/lib/python3.5/site-packages/celery/app/trace.py", line 385, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/gsfish/.pyenv/versions/scan_detect/lib/python3.5/site-packages/celery/app/trace.py", line 648, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/gsfish/work/netease/project/scan_detect/tasks.py", line 106, in test
    return result_set.get(disable_sync_subtasks=False)
  File "/home/gsfish/.pyenv/versions/scan_detect/lib/python3.5/site-packages/celery/result.py", line 697, in get
    on_interval=on_interval,
  File "/home/gsfish/.pyenv/versions/scan_detect/lib/python3.5/site-packages/celery/result.py", line 765, in join
    interval=interval, no_ack=no_ack, on_interval=on_interval,
  File "/home/gsfish/.pyenv/versions/scan_detect/lib/python3.5/site-packages/celery/result.py", line 205, in get
    assert_will_not_block()
  File "/home/gsfish/.pyenv/versions/scan_detect/lib/python3.5/site-packages/celery/result.py", line 41, in assert_will_not_block
    raise RuntimeError(E_WOULDBLOCK)
RuntimeError: Never call result.get() within a task!
See http://docs.celeryq.org/en/latest/userguide/tasks.html#task-synchronous-subtasks
```


Discussion:

Resolving patch:
diff --git a/celery/result.py b/celery/result.py
--- a/celery/result.py
+++ b/celery/result.py
@@ -769,6 +769,7 @@ def join(self, timeout=None, propagate=True, interval=0.5,
             value = result.get(
                 timeout=remaining, propagate=propagate,
                 interval=interval, no_ack=no_ack, on_interval=on_interval,
+                disable_sync_subtasks=disable_sync_subtasks,
             )
             if callback:
                 callback(result.id, value)


Test patch:
diff --git a/t/unit/tasks/test_result.py b/t/unit/tasks/test_result.py
--- a/t/unit/tasks/test_result.py
+++ b/t/unit/tasks/test_result.py
@@ -468,6 +468,17 @@ def test_get(self):
         b.supports_native_join = True
         x.get()
         x.join_native.assert_called()
+    
+    @patch('celery.result.task_join_will_block')
+    def test_get_sync_subtask_option(self, task_join_will_block):
+        task_join_will_block.return_value = True
+        x = self.app.ResultSet([self.app.AsyncResult(str(t)) for t in [1, 2, 3]])
+        b = x.results[0].backend = Mock()
+        b.supports_native_join = False
+        with pytest.raises(RuntimeError):
+            x.get()
+        with pytest.raises(TimeoutError):
+            x.get(disable_sync_subtasks=False, timeout=0.1)
 
     def test_join_native_with_group_chain_group(self):
         """Test group(chain(group)) case, join_native can be run correctly.