Integration for asyncio has a invalid signature for loop create_task function -- _sentry_task_factory #1993
Description
How do you use Sentry?
Sentry Saas (sentry.io)
Version
1.19.1
Steps to Reproduce
I've found out this while playing with aiorun lib and sentry_sdk asyncio integration.
This relates to python 3.11.X versions since it introduces new signature for create_task loop method.
Docs prior to py3.10 https://docs.python.org/3.10/library/asyncio-task.html#asyncio.create_task
asyncio.create_task(coro, *, name=None)
Docs for 3.11.X https://docs.python.org/3.10/library/asyncio-task.html#asyncio.create_task
asyncio.create_task(coro, *, name=None, context=None)
Sentry-sdk asyncio source code part:
https://github.com/getsentry/sentry-python/blob/1.19.1/sentry_sdk/integrations/asyncio.py#L31
def _sentry_task_factory(loop, coro):
# type: (Any, Any) -> AnyThis should be replaced by
Expected Result
_sentry_task_factory signature should be replaced by signature bellow
def _sentry_task_factory(loop, coro, *, name=None, context=None):
Actual Result
File "/home/andrei/.cache/pypoetry/virtualenvs/candles-cache-Yse3-3EP-py3.11/lib/python3.11/site-packages/aiorun.py", line 393, in run
loop.run_until_complete(loop.shutdown_asyncgens())
File "uvloop/loop.pyx", line 1487, in uvloop.loop.Loop.run_until_complete
File "/home/andrei/.pyenv/versions/3.11.2/lib/python3.11/asyncio/tasks.py", line 649, in ensure_future
return _ensure_future(coro_or_future, loop=loop)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/andrei/.pyenv/versions/3.11.2/lib/python3.11/asyncio/tasks.py", line 670, in _ensure_future
return loop.create_task(coro_or_future)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "uvloop/loop.pyx", line 1431, in uvloop.loop.Loop.create_task
TypeError: patch_asyncio.<locals>._sentry_task_factory() got an unexpected keyword argument 'context'
sys:1: RuntimeWarning: coroutine 'Loop.shutdown_asyncgens' was never awaited
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity
antonpirker commentedon Apr 11, 2023
Hey @andrewshkovskii !
Thanks for bringing this up. Yes, our Python 3.11 support is not yet perfect. So we will look into this. (but we have a lot on our plates right now, so dont hold your breath)
But if you (or anyone else) what to give it a try, we are always happy to accept PRs
github-actions commentedon May 3, 2023
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it
Status: BacklogorStatus: In Progress, I will leave it alone ... forever!"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀
szokeasaurusrex commentedon Aug 14, 2023
Hi @andrewshkovskii, can you please provide the exact steps/code to reproduce the error message that you observed? I am having trouble reproducing it
monosans commentedon Aug 15, 2023
szokeasaurusrex commentedon Aug 16, 2023
Hi @monosans and @andrewshkovskii, it looks like the reason your error is occurring is because you are calling the
sentry_sdk.initfrom within theasync mainfunction instead of calling it as soon as possible when your application starts, as we recommend in our documentation.Is there any reason why you need to be initializing the Sentry SDK from within the
async mainfunction? If not, I would suggest rewriting your code as follows, since it appears to stop your error from occurring:Thank you for brining our attention to the changed method signatures, though. Even though it does not seem to be the cause of your error, we should probably still fix our code to match the new method signatures from Python 3.11.
monosans commentedon Aug 16, 2023
Thanks, that helped.
But here's the interesting thing. This issue only occurs when using uvloop and only on Python 3.11. If you don't use uvloop or use Python below 3.11, everything works, even if you initialize sentry in an async function.
szokeasaurusrex commentedon Aug 16, 2023
I did some more investigation into this issue, and it appears that I was incorrect earlier; your error is indeed caused by the method signature that we use in the SDK not matching the new signature for Python 3.11. The reason that moving the
sentry_sdk.initcall stopped the error from occurring is because moving this call out of theasyncfunction actually prevents the SDK from instrumentingasyncioproperly. So none of the relevant SDK code ever gets called, meaning that we don't observe your error, but also the SDK will fail to report information that we would expect to gather from theasynciointegration. This is clearly a bug, since we should be instrumentingasyncioeven when Sentry is being initialized outside anasyncfunction (especially because this is how our documentation states that the SDK should be initialized), and I will be creating an issue to document the bug so we can fix it.So, for now, I would recommend that you move the Sentry SDK initialization call back into the
asyncfunction to ensure thatasynciogets instrumented properly and all the events you are supposed to observe get captured. We will let you know once we have a fix for this issue.While investigating, I discovered that the error also occurs without uvloop when you call
asyncio.create_taskwith thecontextvariable set to a value other thanNone.