channels: Daphne freezes on some ws disconnects

python 3.8
channels==2.3.1
channels-redis==2.4.1
daphne==2.4.0
Django==2.2.8

Entrypoint is daphne on alpine linux.
Consumer is quite simple:


class Consumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.user = self.scope["user"]
        self.name = self.scope["url_route"]["kwargs"]["name"]
        # this line is changed by different subclasses
        self.group_name = "%s%s" % ("chat-", self.name)

        await self.accept()
        # Join group
        await self.channel_layer.group_add(self.group_name, self.channel_name)

        if not True: # auth check
            await self.send(text_data="not authorized")
            await self.close()

    # Receive message from group
    async def m(self, event):
        message = event["m"]

        # Send message to WebSocket
        await self.send(text_data=json.dumps(message))

This is the error that comes up after a random amount of disconnects:

2019-12-15 15:57:32,107 WARNING  Application instance <Task pending name='Task-21465' coro=<SessionMiddlewareInstance.__call__() running at /usr/local/lib/python3.8/site-packages/channels/sessions.py:178> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/local/lib/python3.8/asyncio/futures.py:357, <TaskWakeupMethWrapper object at 0x7f7ebec32910>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 41380] path=b'/api/ws/chat/ddd8ce11-50d0-456a-b8d8-15a068682d1e/'> took too long to shut down and was killed.
2019-12-15 15:57:33,108 ERROR    Exception in callback AsyncioSelectorReactor.callLater.<locals>.run() at /usr/local/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287
handle: <TimerHandle when=1090864.949068441 AsyncioSelectorReactor.callLater.<locals>.run() at /usr/local/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287>
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/asyncio/events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py", line 290, in run
    f(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/daphne/server.py", line 275, in application_checker
    exception = application_instance.exception()
asyncio.exceptions.CancelledError

Subsequent requests, http and ws, aren’t served anymore. Timeout.

Any insight into this at all is greatly appreciated!

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 27 (1 by maintainers)

Most upvoted comments

@Genarito I’m not able to update Django due to work considerations, but I did finally get things to work on Django 2.2.

This no longer throws the exception:

asyncio.exceptions.CancelledError

@adrzystek here’s the dependency stack that finally worked for me:

aioredis==1.3.1
asgiref==3.2.10
channels==2.3.1
channels-redis==2.4.1
daphne==2.5.0
Django==2.2
python: 3.8.6
redis: 6.0.8 (also tested on 5.0.6)

Thanks for all your help!

I can confirm @Genarito’s words, I had the same issue while on Python 3.8.2 but it was enough to upgrade channels (in my case from 2.3.0 to 2.4.0) and channels-redis (2.4.0 -> 3.1.0) packages.

Good to read that!! 😄

While I can’t advise against upgrading Django package, I’d like to note that I myself have its 2.2 version as well.

My full stack of relevant libraries:

aioredis==1.3.1
asgiref==3.2.10
channels==2.4.0
channels-redis==3.1.0
daphne==2.3.0
Django==2.2.5
python:3.8.2
redis:5.0.8

@jihoon796 have you tried updating Django? Probably it’s the main problem

Still having this problem with 2.4.2 after trying all of the proposed solutions in the current and other similar issues. Any other ideas?