Django Channels - core.py error

265 views
Skip to first unread message

itsnate_b

unread,
Aug 31, 2018, 3:19:28 PM8/31/18
to Django users
I have the latest Django (2x), channels (2x), and redis/docker as of Aug 31st, 2018 installed. I am encountering a django channels error and have no idea why this is happening...has anyone seen this output? What is the potential cause? Feeling pretty stuck with the error below...happens when refreshing the site and an attempt is made to connect from js Websocket to the python/channels backend.


2018-08-31 15:13:04,623 - ERROR - server - Exception inside application:
  File "/.../tests/lib/python3.6/site-packages/channels/sessions.py", line 175, in __call__
    return await self.inner(receive, self.send)
  File "/.../tests/lib/python3.6/site-packages/channels/middleware.py", line 41, in coroutine_call
    await inner_instance(receive, send)
  File "/.../tests/lib/python3.6/site-packages/channels/consumer.py", line 54, in __call__
    await await_many_dispatch([receive, self.channel_receive], self.dispatch)
  File "/.../tests/lib/python3.6/site-packages/channels/utils.py", line 57, in await_many_dispatch
    await task
  File "/.../tests/lib/python3.6/site-packages/channels_redis/core.py", line 400, in receive
    assert not self.receive_lock.locked()
 
[2018/08/31 15:13:04] WebSocket DISCONNECT /ws/dashboard/ [127.0.0.1:56102]

Marius Räsener

unread,
Sep 1, 2018, 9:40:10 AM9/1/18
to Django users
Hey, my guess is to check redis integration, f.e. like mentioned here at the bottom (outside of consumers):
https://channels.readthedocs.io/en/latest/topics/channel_layers.html

next guess, since an unintended lock feels rather basic to me is to make sure you‘re still following the virtualenv best practices? No Idea if that’s worth mentioning or already clear but to be sure...

Good luck

itsnate_b

unread,
Sep 1, 2018, 1:20:16 PM9/1/18
to Django users

Hi Marius,
Thanks for the recommendation. Yes, I am using channel layers outside of consumers, however, this has not been an issue in the past. I am not clear on what you mean by "virtualenv best practices"? Yes, this is running in a venv, and it has worked in the past. I do think this issue is somehow driven by my setup..although any suggestions are welcome!

My setup:
app1/
...AsyncWebsocketConsumer (consumers.py)
...AsyncConsumer (backgroundTask.py)
..a couple other function files that call the get_channel_layer to send to a group
app2/
...same idea

itsnate_b

unread,
Sep 1, 2018, 1:23:10 PM9/1/18
to Django users
I do not think it is even getting to the point where I can accept the connection from within the consumers.py....and I'm not sure how to get a better traceback than the one I already provided.

itsnate_b

unread,
Sep 2, 2018, 1:26:29 PM9/2/18
to Django users
Turns out there was an erroneous init function in the AsyncWebsocketConsumer class. Removing it fixed the issue.

Simon Vézina

unread,
Oct 9, 2018, 9:59:33 AM10/9/18
to Django users
Hi itsnate_b,

Would you mind elaborating on your fix?

I'm getting this exact error and I'm at a complete loss.   Everything works fine in local, but on my live server, after a couple of minutes/hours of working fine, I start getting this error in the logs.

Thanks!

itsnate_b

unread,
Oct 11, 2018, 5:06:13 PM10/11/18
to Django users
Hi Simon,

Sorry to hear that you are having the same problem! Channels can be a blessing and a curse. I find it very finicky with very poor documentation & poor error handling. As for the fix: I had an AsyncWebsocketConsumer class in consumers.py within which I had a custom __init__ function. I believe I was setting the scope incorrectly within the init. I realized that I had no need to override/augment the init and removed it. The result was that the error disappeared. That was it for me. I have since updated to all the latest builds for the channels & redis infrastructure.

Hope that helps.

Cheers,
Nathan

Paul Whipp

unread,
Nov 6, 2018, 6:57:58 PM11/6/18
to Django users
I've just had trouble with exactly the same error. In my case I was building from the tutorial ChatConsumer with my own XChatConsumer and I hadn't referenced it in the routing.py. As a result the tutorial async chat consumer was receiving a scope that did not contain a room_name in its url_route kwargs. Very obscure! I think the channel gets closed by an error and then this 500 error comes up rather than anything helpful for the original error.

Simon Vézina

unread,
Nov 7, 2018, 2:44:58 PM11/7/18
to Django users
Hi,

I'm coming back with this one as well. I managed to fix my problem.  In my consumer, I was making normal Django database queries without the @database_sync_to_async decorator.

As soon as I started wrapping my code properly with this decorator, everything started to work fine. See below.

class NotificationConsumer (AsyncJsonWebsocketConsumer):
    async
def connect (self):
       
#do stuff

    async
def disconnect (self, code):
       
#do stuff

    async
def user_notification (self, event):
       
#do stuff

   
@database_sync_to_async
   
def get_notification (self, notification_pk):
       
return Notification.objects.filter(pk=notification_pk).first()

Hope this helps somebody else!
Reply all
Reply to author
Forward
0 new messages