Skip to content

Starlette fails with Uvicorn when instrumented with Sentry (2.0.0) #3021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jonathanberthias opened this issue Apr 25, 2024 · 4 comments · Fixed by #3022
Closed

Starlette fails with Uvicorn when instrumented with Sentry (2.0.0) #3021

jonathanberthias opened this issue Apr 25, 2024 · 4 comments · Fixed by #3022

Comments

@jonathanberthias
Copy link

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.0.0

Steps to Reproduce

Small reproducer:

import asyncio

import sentry_sdk
from starlette.applications import Starlette
import uvicorn.config

sentry_sdk.init()

app = Starlette()

config = uvicorn.config.Config(app)
config.load()


async def run():
    await config.loaded_app({"type": "http"}, "receive", "send")


asyncio.run(run())

Using latest version of everything. Output from pip freeze:

anyio==4.3.0
certifi==2024.2.2
click==8.1.7
h11==0.14.0
idna==3.7
sentry-sdk==2.0.0
setuptools==69.1.0
sniffio==1.3.1
starlette==0.37.2
urllib3==2.2.1
uvicorn==0.29.0
wheel==0.42.0

Expected Result

The only issue should be that the scope, receive and send callables aren't valid.

Actual Result

WARNING:  ASGI app factory detected. Using it, but please consider setting the --factory flag explicitly.
Traceback (most recent call last):
  File "/srv/bug.py", line 19, in <module>
    asyncio.run(run())
  File "/usr/local/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 685, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/srv/bug.py", line 16, in run
    await config.loaded_app({"type": "http"}, "receive", "send")
  File "/usr/local/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/uvicorn/middleware/asgi2.py", line 14, in __call__
    instance = self.app(scope)
               ^^^^^^^^^^^^^^^
TypeError: 'coroutine' object is not callable

The issue seems to be that the function which replaces Starlette.__init__ isn't handled the same way as the original.

The issue disappears (or rather is completely different) when I remove sentry_sdk.__init__.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Apr 25, 2024
@sentrivana
Copy link
Contributor

Thanks @jonathanberthias! Looks like the culprit is the ensure_integration_enabled_async decorator we added in 2.0.0. If I remove it from the Starlette integration, I get the same behavior as in 1.45.0. (TBH I still couldn't run your app and got an error -- but I can repro the problem also if I take your script, remove everything after your app declaration, run it with uvicorn app:app, and try to request /.)

Preparing a PR to get rid of the decorator for the time being.

@sentrivana
Copy link
Contributor

Possibly related: #2892

@sentrivana
Copy link
Contributor

Rolling out 2.0.1 with a fix soon. Thanks again for letting us know!

@jonathanberthias
Copy link
Author

Rolling out 2.0.1 with a fix soon. Thanks again for letting us know!

Thanks a lot for the quick reaction!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants