question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Daphne freezes on some ws disconnects

See original GitHub issue
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!

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
jihoon796commented, Dec 7, 2020

@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!

3reactions
adrzystekcommented, Nov 11, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Channels: Get stuck after period of time - Stack Overflow
When I stop daphne server (with Ctrl+C), it takes about 5-10 minutes to completely go down. Sometime I have to run kill command....
Read more >
Channels Websocket immedialty disconnecting with ssl-django
I tried to make run the tutorial from the channels docs on my production server, using ssl. After a few hours i managed...
Read more >
Getting Started with Django Channels - Real Python
In this tutorial, we will use Django Channels to create a real-time application that updates a list of users as they log in...
Read more >
python-websockets(1) - Arch manual pages
It is possible to restart the WebSocket server automatically but this isn't necessary for this tutorial. Try connecting and disconnecting the interactive client ......
Read more >
subject:"Django channels"
Also, does anyone have any experience regarding the performance of using daphne, uvicorn or some other server?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found