daphne icon indicating copy to clipboard operation
daphne copied to clipboard

AttributeError: 'WebSocketProtocol' object has no attribute 'application_queue' error

Open bsr3000 opened this issue 8 years ago • 6 comments

Hello, I have mentioned this error on django-channels issue tracker, I am currently testing channels 2 for performance and reliability, currently, we use channels 1 on production and preparing to use channels 2 to production. Today, when I was load testing the application (Stack: Nginx + UWSGI + Daphne), 10 daphne instances setup, each ASGI_THREADS=8 16 cores server, Debian 9 OS, AsyncConsumers, this errors has appeared:

ERROR    [Failure instance: Traceback: <class 'KeyError'>: <WebSocketProtocol client=['127.0.0.1', 55226] path=b'/ws/'>
/venv/lib/python3.5/site-packages/twisted/internet/defer.py:459:callback
/venv/lib/python3.5/site-packages/twisted/internet/defer.py:567:_startRunCallbacks
/venv/lib/python3.5/site-packages/twisted/internet/defer.py:653:_runCallbacks
/venv/lib/python3.5/site-packages/twisted/internet/defer.py:1442:gotResult
--- <exception caught here> ---
/venv/lib/python3.5/site-packages/twisted/internet/defer.py:1386:_inlineCallbacks
/venv/lib/python3.5/site-packages/daphne/server.py:191:create_application
]
ERROR    Exception in callback AsyncioSelectorReactor.callLater.<locals>.run() at /venv/lib/python3.5/site-packages/twisted/internet/asyncioreactor.py:287
handle: <TimerHandle when=1713807.003341125 AsyncioSelectorReactor.callLater.<locals>.run() at /venv/lib/python3.5/site-packages/twisted/internet/asyncioreactor.py:287>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/events.py", line 126, in _run
    self._callback(*self._args)
  File "/venv/lib/python3.5/site-packages/twisted/internet/asyncioreactor.py", line 290, in run
    f(*args, **kwargs)
  File "/venv/lib/python3.5/site-packages/twisted/internet/tcp.py", line 289, in connectionLost
    protocol.connectionLost(reason)
  File "/venv/lib/python3.5/site-packages/autobahn/twisted/websocket.py", line 128, in connectionLost
    self._connectionLost(reason)
  File "/venv/lib/python3.5/site-packages/autobahn/websocket/protocol.py", line 2467, in _connectionLost
    WebSocketProtocol._connectionLost(self, reason)
  File "/venv/lib/python3.5/site-packages/autobahn/websocket/protocol.py", line 1096, in _connectionLost
    self._onClose(self.wasClean, WebSocketProtocol.CLOSE_STATUS_CODE_ABNORMAL_CLOSE, "connection was closed uncleanly (%s)" % self.wasNotCleanReason)
  File "/venv/lib/python3.5/site-packages/autobahn/twisted/websocket.py", line 171, in _onClose
    self.onClose(wasClean, code, reason)
  File "/venv/lib/python3.5/site-packages/daphne/ws_protocol.py", line 146, in onClose
    self.application_queue.put_nowait({
AttributeError: 'WebSocketProtocol' object has no attribute 'application_queue'

Packages

aioredis==1.0.0
asgi-redis==1.3.0
asgiref==2.2.0
async-timeout==2.0.1
autobahn==18.3.1
channels==2.1.1
channels-redis==2.1.1
daphne==2.1.1
Django==1.11
Twisted==17.9.0
txaio==2.9.0
txredisapi==1.4.4
zope.interface==4.4.3

On this setup I have got 10 new connections per second to one endpoint, without throwing that error.

Here are the results of load testing tool:

  Scenarios launched:  1200
  Scenarios completed: 1200
  Requests completed:  1200

Load test setup:

phases:
    - duration: 120
       arrivalRate: 10

Scenario phases: 1. Connects to ws, 2. Send message 3. Wait for 4 seconds 4. Disconnect

If I set the arrivalRate to any value bigger then 10, this error starts to occur AttributeError: 'WebSocketProtocol' object has no attribute 'application_queue' and I can`t normally connect to ws, while those errors are thrown to console.

Currently, it`s whole info, which I have from logs.

bsr3000 avatar Apr 26 '18 14:04 bsr3000

Also this one appears from time to time under load test

WARNING  Application instance <Task pending coro=<__call__() running at /venv/lib/python3.5/site-packages/channels/consumer.py:54> wait_for=<Future pending cb=[Task._wakeup()]>> for connection <WebSocketProtocol client=['127.0.0.1', 52270] path=b'/ws/'> took too long to shut down and was killed.

bsr3000 avatar Apr 26 '18 14:04 bsr3000

The warning you posted in the second comment is fine - it's not bad, and can be fixed in your application if you need to - but the main error looks like a race condition in our websocket handler.

andrewgodwin avatar Apr 26 '18 14:04 andrewgodwin

What can help with that warning ? set -t param on daphne start?

That race condition only appears under hard load, if I connect few clients to server that error never appears.

bsr3000 avatar Apr 26 '18 15:04 bsr3000

The warning is because you have some application code that doesn't exit. It's currently hard to find out what it is, but it's one of your consumers not finishing up right (did you write any consumers based on the low-level SyncConsumer or AsyncConsumer ones?)

Race conditions do tend to appear more under hard loads, so I'm not surprised it didn't show up before in our tests.

andrewgodwin avatar Apr 26 '18 19:04 andrewgodwin

Basically, I have inherited a new class from JsonAsyncWebsocketConsumer, cause I use ujson instead of usual json package and also overidden AsyncConsumer.dispatch() method, so I think I know where is the error

    async def dispatch(self, message):
        """
        Works out what to do with a message.
        """
        if message['type'] in ['websocket.connect', 'websocket.disconnect', 'websocket.receive', 'websocket.send']:
            await super(AsyncUJsonWebsocketConsumer, self).dispatch(message)
        else:
            await self.send_message(message)

I needed to do this, to save backward compatibility with our frontend code, so as I think, I need to add 'websocket.close' type to my type list, and now it enters self.send_message, not a class super method.

bsr3000 avatar Apr 26 '18 20:04 bsr3000

Ah yes, if you're doing that sort of override you have to ensure that you end the main coroutine when you get a close event. You can read more about it here: http://channels.readthedocs.io/en/latest/topics/consumers.html#closing-consumers

andrewgodwin avatar Apr 27 '18 16:04 andrewgodwin