Skip to content

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

@jonathanberthias

Description

@jonathanberthias

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__.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions