0

I am using the WebsocketConsumer consumer and when I disconnect the websocket connection it gives the below error. Also when I have already one open connection and when I try to create a connection with the same route it gets Disconnected and gives the same

Application instance <Task pending name='Task-5' coro=<StaticFilesWrapper.__call__() running at /Volumes/Data/Projects/LocoNav-LM/vt-lm-server/venv/lib/python3.8/site-packages/channels/staticfiles.py:44> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/futures.py:360, <TaskWakeupMethWrapper object at 0x1076a2580>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 54789] path=b'/ws/users/'> took too long to shut down and was killed.

My code:-

class TestMultipleUsers(WebsocketConsumer):
    def connect(self):
        logging.info('connecting....')
        self.accept()
        redis_client = RedisClient(host='localhost', port=6379, db=0)
        redis_client.subscribe_and_listen()
        redis_client.subscribe_channel('test')
        for message in redis_client.sub.listen():
            if message is not None and isinstance(message, dict):
                print(message)
                self.send(text_data="1")
    
    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        self.send(text_data=json.dumps({
            'message': message
        }))

    def disconnect(self, close_code):
        logging.info('disconnecting....')
        logging.info(close_code)

However, whenever I disconnect the web socket from Postman the disconnect method never gets called.

requirements.txt

channels==3.0.4
Django==4.0
celery==5.2.3

Here are my Logs:-

2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO    ] HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO    ] Configuring endpoint tcp:port=8000:interface=127.0.0.1
2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO    ] Listening on TCP address 127.0.0.1:8000
WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145371021312 [INFO    ] WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145387810816 [INFO    ] connecting....
WebSocket CONNECT /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145371021312 [INFO    ] WebSocket CONNECT /ws/users/ [127.0.0.1:60683]
WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:12 Process: 21283 Thread: 123145371021312 [INFO    ] WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60685]
WebSocket DISCONNECT /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:17 Process: 21283 Thread: 123145371021312 [INFO    ] WebSocket DISCONNECT /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:28 Process: 21283 Thread: 123145371021312 [WARNING ] Application instance <Task pending name='Task-7' coro=<StaticFilesWrapper.__call__() running at /Volumes/Data/Projects/LocoNav-LM/vt-lm-server/venv/lib/python3.8/site-packages/channels/staticfiles.py:44> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/futures.py:360, <TaskWakeupMethWrapper object at 0x10f495940>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 60685] path=b'/ws/users/'> took too long to shut down and was killed.
5
  • can you get out the redis_client from your class init? because you open a new connection per user Commented Feb 27, 2022 at 19:48
  • Yes, I am opening a new connection per user, so how removing the redis_client form connect will help? @Nova Commented Feb 27, 2022 at 20:08
  • this error is because you wait a long time in your connect method and when you don't pass any handshake you get that, so for testing propose to try t comment Redis process from your connect method @Subham Commented Feb 27, 2022 at 20:18
  • @Nova I removed but it didn't help. I have added some logs though. Commented Feb 27, 2022 at 20:29
  • add raise StopConsumer() to your overwritted disconnect method Commented Feb 27, 2022 at 21:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.