python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

Open neeraj-partha opened this issue 4 years ago • 17 comments

│       │                                   │       │    └ [<Task pending coro=<SinricProSocket.receiveMessage() running at /home/pi/scripts/python-sdk/sinric/_sinricprosocket.py:58> c...
│       │                                   │       └ <function wait at 0x76233d68>
│       │                                   └ <module 'asyncio' from '/usr/lib/python3.7/asyncio/__init__.py'>
│       └ <built-in function get_event_loop>
└ <module 'asyncio' from '/usr/lib/python3.7/asyncio/__init__.py'>

File "/usr/lib/python3.7/asyncio/base_events.py", line 571, in run_until_complete self.run_forever() │ └ <function BaseEventLoop.run_forever at 0x7618a030> └ <_UnixSelectorEventLoop running=True closed=False debug=False> File "/usr/lib/python3.7/asyncio/base_events.py", line 539, in run_forever self._run_once() │ └ <function BaseEventLoop._run_once at 0x7618ad68> └ <_UnixSelectorEventLoop running=True closed=False debug=False> File "/usr/lib/python3.7/asyncio/base_events.py", line 1775, in _run_once handle._run() │ └ <function Handle._run at 0x76209df8> └ <Handle <TaskWakeupMethWrapper object at 0x75df89d0>(<Future finished result=None>)> File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run self._context.run(self._callback, *self._args) │ │ │ │ │ └ <member '_args' of 'Handle' objects> │ │ │ │ └ <Handle <TaskWakeupMethWrapper object at 0x75df89d0>(<Future finished result=None>)> │ │ │ └ <member '_callback' of 'Handle' objects> │ │ └ <Handle <TaskWakeupMethWrapper object at 0x75df89d0>(<Future finished result=None>)> │ └ <member '_context' of 'Handle' objects> └ <Handle <TaskWakeupMethWrapper object at 0x75df89d0>(<Future finished result=None>)>

File "/home/pi/scripts/python-sdk/sinric/_sinricprosocket.py", line 51, in receiveMessage message = await connection.recv() │ └ <function WebSocketCommonProtocol.recv at 0x7609b108> └ <websockets.client.WebSocketClientProtocol object at 0x765730f0>

File "/home/pi/.local/lib/python3.7/site-packages/websockets/protocol.py", line 509, in recv await self.ensure_open() │ └ <function WebSocketCommonProtocol.ensure_open at 0x7609b2b8> └ <websockets.client.WebSocketClientProtocol object at 0x765730f0> File "/home/pi/.local/lib/python3.7/site-packages/websockets/protocol.py", line 803, in ensure_open raise self.connection_closed_exc() │ └ <function WebSocketCommonProtocol.connection_closed_exc at 0x7609b270> └ <websockets.client.WebSocketClientProtocol object at 0x765730f0>

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

neeraj-partha avatar May 17 '20 14:05 neeraj-partha

can you please help me to add code to re-connect automatically when disconnected.

neeraj-partha avatar May 17 '20 14:05 neeraj-partha

Might maybe problem with ISP! Try in mobile hotspot once!

thegoliathgeek avatar May 17 '20 16:05 thegoliathgeek

i'm using jio hotspot. It disconnects after a certain random amount of time. websocket connection is not consistent.

neeraj-partha avatar May 17 '20 16:05 neeraj-partha

I faced this error too. Try in different ISP.

thegoliathgeek avatar May 17 '20 16:05 thegoliathgeek

Unfortunately I don't have access to any other isp. Can we somehow add code to reconnect when this situation occur? Please help. Thank you

neeraj-partha avatar May 18 '20 10:05 neeraj-partha

Can I try removing ping interval and ping timeout as stated below

https://stackoverflow.com/questions/54101923/1006-connection-closed-abnormally-error-with-python-3-7-websockets

neeraj-partha avatar May 18 '20 10:05 neeraj-partha

Not an optional solution. If you remove ping. Maintaining a live connection will be lost.

thegoliathgeek avatar May 18 '20 10:05 thegoliathgeek

How about catching this exception and reconnect?

neeraj-partha avatar May 18 '20 10:05 neeraj-partha

Can this be caught by the DisconnectCallbackHandler? https://sinricpro.github.io/esp8266-esp32-sdk/class_sinric_pro_class.html#a654bb017ca55b448d55bb36e0346f38a I'm not sure if this works in python.
Can you tell me how to convert the code example (SinricPro.onDisconnected({ Serial.printf("Disconnected from SinricPro\r\n"); });) into python?

DevMiser avatar Aug 22 '20 17:08 DevMiser

@DevMiser sure I'll back to you on this.

thegoliathgeek avatar Aug 25 '20 10:08 thegoliathgeek

I tried the following, but it did not work:

def onDisconnected(cb): if cb == 'None': print ("Disconnected from Sinric Pro")

callbacks = { 'powerState': onPowerState, 'Disconnected': onDisconnected }

DevMiser avatar Sep 03 '20 23:09 DevMiser

https://websockets.readthedocs.io/en/stable/intro.html?highlight=disconnect#producer

This will work. So I'll update this

thegoliathgeek avatar Sep 04 '20 19:09 thegoliathgeek

Awesome. Thank you. It would be great to see this written into the Pro Switch example code.

DevMiser avatar Sep 06 '20 19:09 DevMiser

I am now using this solution to reconnect automatically after being disconnected - using a while loop. When there is an error, it moves to the next lines in the code, which prints the time of the error, and then waits 20 minutes and runs the program again (by which time hopefully the ISP interruption or other cause of the error has been resolved). Here is the modification to the applicable portion of the code in the simple example:

if name == 'main': while True:
client = SinricPro(appKey,deviceIdArr,callbacks,enable_log=False, restore_states=True, secretKey=secretKey) udp_client = SinricProUdp(callbacks,deviceIdArr,enable_trace=False)
client.handle_all(udp_client) print ("Disconnected from SinricPro at: %s" % time.ctime()) sleep(1200) print ("Reconnection attempted at: %s" % time.ctime())

You may also need to add "import time" in a line before "from time import sleep" in the simple example.

DevMiser avatar Dec 12 '20 23:12 DevMiser

https://websockets.readthedocs.io/en/stable/intro.html?highlight=disconnect#producer

This will work. So I'll update this

Hi @imdhanush, Have you updated the SDK as I am facing the same issue while using Python SDK for which I have sent you an email also.

Thanks, Dipankar

buddydip avatar Mar 01 '21 13:03 buddydip

I have stopped using Sinric Pro and switched to OpenHAB because of the above issue. I felt OpenHAB much better in features and stability and more powerful as the local installation is also provided as a package which can be setup only by simple config (installed in Raspberry Pi) and can work even without internet connection in a local network

buddydip avatar Jul 14 '21 07:07 buddydip

@buddydip Sorry for the inconvenience. This existing python-sdk has many bugs. So am trying to rewrite the whole sdk. This might take some time. Thanks

thegoliathgeek avatar Jul 14 '21 08:07 thegoliathgeek