0

Sending messages to groups using group_send() suddenly stops working after some time. The handler method of the consumer is not called anymore.

Restarting daphne is fixing the issue for some time.

Details

There is no error showing up anywhere in the logs, just the messages are not getting handled by the consumers anymore.

I'm, using the following libraries:

  • aioredis==1.2.0
  • asgiref==2.3.2
  • channels-redis==2.3.2
  • channels==2.1.6
  • daphne==2.2.4
  • django==2.1.5
  • redis==3.0.1

Code

# settings.py
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {"hosts": [("localhost", "6379")]},
    }
}
# views.py
class ReceiveEventView(APIView):
    def post(self, request, *args, **kwargs):
        # payload: {"type": "event_triggered", "group": "warning", "message": "Button pressed"}
        # or
        # payload: {"type": "event_triggered", "group": "danger", "message": "Red Button pressed"}
        payload = json.loads(request.POST.get("payload", "{}"))
        if (payload.get("type") == "event_triggered"):
            async_to_sync(channel_layer.group_send)(payload.get("group"), payload)
        return HttpResponse(status=204)
# consumers.py
class EventConsumer(WebsocketConsumer):
    def connect(self):
        if not self.scope["user"].is_authenticated:
            return
        self.accept()
        for group in get_subscriptions(self.scope["user"]):
            async_to_sync(self.channel_layer.group_add)(group, self.channel_name)

    def disconnect(self, close_code):
        if not self.scope["user"].is_authenticated:
            return
        for group in get_subscriptions(self.scope["user"]):
            async_to_sync(self.channel_layer.group_discard)(group, self.channel_name)

    def event_triggered(self, event):
        logger.debug("Consumer::event_triggered()")
        self.send(text_data=json.dumps(event))

expected and actual results

For some time Consumer::event_triggered() appears in the logging, but suddenly stops. Receiving messages from the browser via WebSocket still works. Just the transport from group_send() to the consumers is broken.

JanMalte
  • 934
  • 6
  • 17
  • Which version of Python do you use? 3.5.2? there seems to be a known bug with the combination of [this version and channels](https://github.com/django/channels_redis/issues/79) – Ken4scholars Jan 31 '19 at 08:26
  • I'm using Python 3.5.3 – JanMalte Jan 31 '19 at 15:20
  • I've compiled Python 3.6.8 and will have a look at it. Hopefully it will run without issues until tomorrow. Thanks for the hint regarding the Python version. – JanMalte Jan 31 '19 at 16:24
  • Great, I have posted it as an answer, If that works, don't forget to accept the answer – Ken4scholars Jan 31 '19 at 16:58

2 Answers2

2

There is a known bug causing a break up in connections after a while for Channels apps running in Python 3.5. Update to Python 3.6 for a possible fix

Ken4scholars
  • 6,076
  • 2
  • 21
  • 38
  • Thanks again for your help. It was indeed an issue with Python 3.5. Running my application with Python 3.6 is not showing this issue. – JanMalte Feb 03 '19 at 22:04
2

I faced this issue with Python3.8. So I fixed it by switching to Python3.6

Django channels - Client receives message sometimes and not other times till it doesn't recieve it at all

Newbie
  • 343
  • 2
  • 13
  • yes! me too, I tried also python3.9 but it has the same problem. I fixed switching to python3.6 – lorenzo Apr 13 '21 at 15:11