Repository: getsentry/sentry-python

Issue:
Patched task factory in AsyncioIntegration loses task result.
### How do you use Sentry?

Sentry Saas (sentry.io)

### Version

1.14.0

### Steps to Reproduce

```python
import asyncio
import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration

async def add(a, b):
    return a + b

async def main():
    sentry_sdk.init('dsn', integrations=[AsyncioIntegration()])
    result = await asyncio.create_task(add(1, 2))
    assert result == 3, result

asyncio.run(main())
```

### Expected Result

No `AssertionError`.

### Actual Result

```python
    assert result == 3, result
AssertionError: None
```

Patched task factory always loses task result.


Discussion:
Pretty sure my problem is related or there is something wrong with the `AsyncioIntegration` patch. Came accross your issue while trying to add Sentry to a project with some `aiohttp` requests. Here is some reproducible  sample code:

Python 3.10.9
aiohttp==3.7.4
sentry_sdk==1.15.0

```python
import asyncio

import aiohttp
import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration


async def main():
    sentry_sdk.init(
        dsn="<dsn>",
        integrations=[AsyncioIntegration()],
    )

    async with aiohttp.ClientSession() as client:
        await client.get("https://google.com")

asyncio.run(main())
```

The above code results in `TypeError: 'NoneType' object is not iterable` coming from the aiohttp library. 

When the `AsyncioIntegration()` is omitted (i.e. `integrations=[]`), sample code works fine.
Hey @tkukushkin  and @dvdblk for the great issue and the example projects for reproducing this. I am having a look at it.

Resolving patch:
diff --git a/sentry_sdk/integrations/asyncio.py b/sentry_sdk/integrations/asyncio.py
--- a/sentry_sdk/integrations/asyncio.py
+++ b/sentry_sdk/integrations/asyncio.py
@@ -32,15 +32,19 @@ def _sentry_task_factory(loop, coro):
             # type: (Any, Any) -> Any
 
             async def _coro_creating_hub_and_span():
-                # type: () -> None
+                # type: () -> Any
                 hub = Hub(Hub.current)
+                result = None
+
                 with hub:
                     with hub.start_span(op=OP.FUNCTION, description=coro.__qualname__):
                         try:
-                            await coro
+                            result = await coro
                         except Exception:
                             reraise(*_capture_exception(hub))
 
+                return result
+
             # Trying to use user set task factory (if there is one)
             if orig_task_factory:
                 return orig_task_factory(loop, _coro_creating_hub_and_span())  # type: ignore


Test patch:
diff --git a/tests/integrations/asyncio/test_asyncio.py b/tests/integrations/asyncio/test_asyncio.py
--- a/tests/integrations/asyncio/test_asyncio.py
+++ b/tests/integrations/asyncio/test_asyncio.py
@@ -155,3 +155,19 @@ async def test_exception(
     assert error_event["exception"]["values"][0]["value"] == "division by zero"
     assert error_event["exception"]["values"][0]["mechanism"]["handled"] is False
     assert error_event["exception"]["values"][0]["mechanism"]["type"] == "asyncio"
+
+
+@minimum_python_36
+@pytest.mark.asyncio
+async def test_task_result(sentry_init):
+    sentry_init(
+        integrations=[
+            AsyncioIntegration(),
+        ],
+    )
+
+    async def add(a, b):
+        return a + b
+
+    result = await asyncio.create_task(add(1, 2))
+    assert result == 3, result