I am getting this error on my console log, and on form submit it keeps loading does not post data to the server.
/home/Python/Working/Benutzerverwaltung/env/lib/python3.6/site-packages/channels/sessions.py:183> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fab9fe51408>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 59462] path=b'/ws/stream/Sales'> took too long to shut down and was killed.
Here is my code for closing the channel.
async def disconnect(self, code):
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name,
self.channel
)
await self.close()
async def websocket_disconnect(self, event):
print("Disconnect", event)
await self.send({
"type": "websocket.close"
})
How to fix this?
This error is broadly because a coroutine is hanging around longer than it should.
This specific case
In this case,
AsyncWebsocketConsumer.websocket_disconnect()is being overridden but is not callingsuper()which means thatStopConsumer()isn't run (see channels/generic/websocket.py:228). Perhaps don't overridewebsocket_disconnectat all, since there's nothing in this example that justifies it.Also note that
async_to_syncis intended for sync consumers, but this is an async consumer. Instead use:The
await self.close()isn't required as disconnect has already occurred. Remove that line.AsyncHttpConsumer
Similarly in an
AsyncHttpConsumerthe mistake I've often made is to callawait self.send_response(...)but then forget to callreturnafterwards, so the function will continue on when you didn't expect it to.AsyncHttpConsumeralso has an open bug report about not surfacing exceptions withinhandle(). The only option currently is to adding extra print/logging lines to figure out what is/isn't running.Beware of django-debug-toolbar
Also worth noting is that adding
debug-toolbarto yourINSTALLED_APPSwill silence exceptions in your async consumers. See discussion here. Beware!