skywalking icon indicating copy to clipboard operation
skywalking copied to clipboard

[Bug] Python FastAPI plugin does not support websocket connections

Open theblazehen opened this issue 1 year ago • 1 comments

Search before asking

  • [X] I had searched in the issues and found no similar issues.

Apache SkyWalking Component

Python Agent (apache/skywalking-python)

What happened

There is an assertion error whenever a websocket connection gets made

What you expected to happen

Either ignore websocket connections, or support tracing them

How to reproduce

Save the following as ws_example.py:

> cat ws_example.py
from fastapi import FastAPI, WebSocket

app = FastAPI()

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        data = await websocket.receive_text()
        await websocket.send_text(f"Message text was: {data}")

And run with sw-python run uvicorn --port 8080 ws_example:app

Make a request with

> wscat -c ws://127.0.0.1:8080/ws
error: Unexpected server response: 500

And the logs in the python service will show you

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/ubuntu/venv/deepalert/lib/python3.10/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 225, in run_asgi
    result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
  File "/home/ubuntu/venv/deepalert/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/home/ubuntu/venv/deepalert/lib/python3.10/site-packages/fastapi/applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "/home/ubuntu/venv/deepalert/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/ubuntu/venv/deepalert/lib/python3.10/site-packages/starlette/middleware/errors.py", line 149, in __call__
    await self.app(scope, receive, send)
  File "/home/ubuntu/venv/deepalert/lib/python3.10/site-packages/skywalking/plugins/sw_fastapi.py", line 45, in _sw_fast_api
    req = Request(scope, receive=receive, send=send)
  File "/home/ubuntu/venv/deepalert/lib/python3.10/site-packages/starlette/requests.py", line 195, in __init__
    assert scope["type"] == "http"
AssertionError

Anything else

No response

Are you willing to submit PR?

  • [ ] Yes I am willing to submit a PR!

Code of Conduct

theblazehen avatar Sep 30 '22 10:09 theblazehen

Thanks for reporting. I see the problem.

Superskyyy avatar Sep 30 '22 16:09 Superskyyy