Search code examples
python-3.xwebsocketfastapistarlette

FastAPI/Starlette websocket.close() not accepting "reason" argument


I need to disconnect an incoming WebSocket connection in case the user already has a live connection with the server, for which I am calling websocket.close() as suggested in the Starlette documentation. However when I call the following:

await websocket.close(code=1000, reason='Connection refused as session already exists for user')

The server crashes with the following stack trace saying unexpected keyword argument 'reason':

app_1     | Traceback (most recent call last):
app_1     |   File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 184, in run_asgi
app_1     |     result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
app_1     |     return await self.app(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/fastapi/applications.py", line 211, in __call__
app_1     |     await super().__call__(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 112, in __call__
app_1     |     await self.middleware_stack(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 146, in __call__
app_1     |     await self.app(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/starlette/middleware/base.py", line 22, in __call__
app_1     |     await self.app(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 76, in __call__
app_1     |     await self.app(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/starlette/exceptions.py", line 58, in __call__
app_1     |     await self.app(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 656, in __call__
app_1     |     await route.handle(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 315, in handle
app_1     |     await self.app(scope, receive, send)
app_1     |   File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 77, in app
app_1     |     await func(session)
app_1     |   File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 273, in app
app_1     |     await dependant.call(**values)
app_1     |   File "/app/code/./app/api/api_v1/ws/ws_manager.py", line 175, in websocket_endpoint
app_1     |     await manager.connect(user, websocket)
app_1     |   File "/app/code/./app/api/api_v1/ws/ws_manager.py", line 90, in connect
app_1     |     await websocket.close(code=1000, reason='Connection refused as session already exists for user')
app_1     | TypeError: WebSocket.close() got an unexpected keyword argument 'reason'

I need to let the client know that the connection was refused because of a particular reason. Please let me know what I am doing wrong here.

Thanks for your time.


Solution

  • I was running an old version of FastAPI whose close() did not support "reason" argument. Updgrading FastAPI to the latest version solved the problem as the "reason" argument was introduced recently.