I struggled for a few hours with this error.
Eventually, I understood what I was doing wrong.
... or -at least- not just me!
I was following the Channels documentation trying to implement some Subscription behaviour using Django.
I actually read the documentation wrong so I was trying to open a new WebSocket connection by simply instantiate it in the browser as follow:
const socket = new WebSocket("ws://localhost:8000/graphql/");
...and promptly every time I ran this function, the server logged this:
INFO: ('172.20.0.1', 45834) - "WebSocket /graphql/" 403
INFO: connection rejected (403 Forbidden)
INFO: connection closed
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/lib/python3.11/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 247, in run_asgi
result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/site-packages/channels/routing.py", line 62, in __call__
return await application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/site-packages/channels/routing.py", line 116, in __call__
return await application(
^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/site-packages/channels/consumer.py", line 94, in app
return await consumer(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/site-packages/channels/consumer.py", line 58, in __call__
await await_many_dispatch(
File "/usr/lib/python3.11/site-packages/channels/utils.py", line 50, in await_many_dispatch
await dispatch(result)
File "/usr/lib/python3.11/site-packages/strawberry/channels/handlers/base.py", line 88, in dispatch
await super().dispatch(message)
File "/usr/lib/python3.11/site-packages/channels/consumer.py", line 73, in dispatch
await handler(message)
File "/usr/lib/python3.11/site-packages/channels/generic/websocket.py", line 238, in websocket_disconnect
await self.disconnect(message["code"])
File "/usr/lib/python3.11/site-packages/strawberry/channels/handlers/ws_handler.py", line 116, in disconnect
await self._handler.handle_disconnect(code)
^^^^^^^^^^^^^
AttributeError: 'GraphQLWSConsumer' object has no attribute '_handler'
subscription SubscribeToChatRooms {
joinChatRooms(
rooms: [{ roomName: "room1" }, { roomName: "room2" }]
user: "foo"
) {
roomName
message
currentUser
}
}
Now... I was wrong... Definitely!
BUT... Is that really the proper error message to log in this case?
It -rather- seems like it crashes badly due to some bug while trying to deal with this bad behavior.
Probably, it should check if self._handler
is actually instantiated before using it or -better- it shouldn't call disconnect
before actually connecting.
strawberry/strawberry/channels/handlers/ws_handler.py
Lines 115 to 116 in a90dea2
In the future, I would prefer not to receive an unclear (and apparently serious) Sentry notification every time a bot tries to open a WebSocket connection with my server... π
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too