Skip to content

Integration for asyncio has a invalid signature for loop create_task function -- _sentry_task_factory #1993

Closed

Description

@andrewshkovskii

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) -> Any

This 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

Activity

  1. antonpirker commented on Apr 11, 2023

    @antonpirker
    Contributor

    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

  2. github-actions commented on May 3, 2023

    @github-actions
    Contributor

    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: Backlog or Status: In Progress, I will leave it alone ... forever!


    "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

  3. szokeasaurusrex commented on Aug 14, 2023

    @szokeasaurusrex
    Member

    Hi @andrewshkovskii, can you please provide the exact steps/code to reproduce the error message that you observed? I am having trouble reproducing it

  4. monosans commented on Aug 15, 2023

    @monosans

    Hi @andrewshkovskii, can you please provide the exact steps/code to reproduce the error message that you observed? I am having trouble reproducing it

    import asyncio
    
    import sentry_sdk
    import uvloop
    from sentry_sdk.integrations.asyncio import AsyncioIntegration
    
    
    async def main():
        sentry_sdk.init(integrations=(AsyncioIntegration(),))
    
    
    if __name__ == "__main__":
        uvloop.install()
        asyncio.run(main())
  5. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Aug 15, 2023
  6. szokeasaurusrex commented on Aug 16, 2023

    @szokeasaurusrex
    Member

    Hi @monosans and @andrewshkovskii, it looks like the reason your error is occurring is because you are calling the sentry_sdk.init from within the async main function 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 main function? If not, I would suggest rewriting your code as follows, since it appears to stop your error from occurring:

    import asyncio
    
    import sentry_sdk
    import uvloop
    from sentry_sdk.integrations.asyncio import AsyncioIntegration
    
    sentry_sdk.init(integrations=(AsyncioIntegration(),))
    
    async def main():
        pass
    
    
    if __name__ == "__main__":
        uvloop.install()
        asyncio.run(main())

    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.

  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀on Aug 16, 2023
  8. monosans commented on Aug 16, 2023

    @monosans

    the reason your error is occurring is because you are calling the sentry_sdk.init from within the async main function instead of calling it as soon as possible

    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.

  9. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Aug 16, 2023
  10. szokeasaurusrex commented on Aug 16, 2023

    @szokeasaurusrex
    Member

    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.init call stopped the error from occurring is because moving this call out of the async function actually prevents the SDK from instrumenting asyncio properly. 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 the asyncio integration. This is clearly a bug, since we should be instrumenting asyncio even when Sentry is being initialized outside an async function (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 async function to ensure that asyncio gets 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.

    If you don't use uvloop or use Python below 3.11, everything works, even if you initialize sentry in an async function.

    While investigating, I discovered that the error also occurs without uvloop when you call asyncio.create_task with the context variable set to a value other than None.

  11. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀on Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions