Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeping connection open freezes KLF #30

Open
werner-j opened this issue Jul 16, 2019 · 22 comments
Open

Keeping connection open freezes KLF #30

werner-j opened this issue Jul 16, 2019 · 22 comments

Comments

@werner-j
Copy link
Contributor

werner-j commented Jul 16, 2019

Hello,

I have my connection with KLF 200 open for an infinite time (it should react any time I issue a command). However, within usually 24 hours I lose the ability to talk to KLF (no reaction) and when trying to establish a connection I receive:

Connecting to KLF 200.
Traceback (most recent call last):
  File "/home/velux/klf200control/vlxcontrol/vlxcontrol.py", line 152, in <module>
    LOOP.run_until_complete(init_pyvlx_connection(LOOP))
  File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "/home/velux/klf200control/vlxcontrol/vlxcontrol.py", line 26, in init_pyvlx_connection
    await pyvlx.load_nodes()
  File "/home/velux/.local/lib/python3.7/site-packages/pyvlx/pyvlx.py", line 83, in load_nodes
    await self.nodes.load(node_id)
  File "/home/velux/.local/lib/python3.7/site-packages/pyvlx/nodes.py", line 70, in load
    await self._load_all_nodes()
  File "/home/velux/.local/lib/python3.7/site-packages/pyvlx/nodes.py", line 86, in _load_all_nodes
    await get_all_nodes_information.do_api_call()
  File "/home/velux/.local/lib/python3.7/site-packages/pyvlx/api_event.py", line 22, in do_api_call
    await self.send_frame()
  File "/home/velux/.local/lib/python3.7/site-packages/pyvlx/api_event.py", line 34, in send_frame
    await self.pyvlx.send_frame(self.request_frame())
  File "/home/velux/.local/lib/python3.7/site-packages/pyvlx/pyvlx.py", line 70, in send_frame
    await self.connect()
  File "/home/velux/.local/lib/python3.7/site-packages/pyvlx/pyvlx.py", line 45, in connect
    await self.connection.connect()
  File "/home/velux/.local/lib/python3.7/site-packages/pyvlx/connection.py", line 89, in connect
    ssl=self.create_ssl_context())
  File "/usr/lib/python3.7/asyncio/base_events.py", line 986, in create_connection
    ssl_handshake_timeout=ssl_handshake_timeout)
  File "/usr/lib/python3.7/asyncio/base_events.py", line 1014, in _create_connection_transport
    await waiter
ConnectionAbortedError: SSL handshake is taking longer than 60.0 seconds: aborting the connection

The only thing that helps is to unplug the KLF200 from the power source and replug it and have it fresh started. Any idea what the reason for this could be / any way to work around this?

@Julius2342
Copy link
Owner

Hmm, i did not experience this. What firmware version do you have?

I guess there is not so much we can do here. Looks like a problem only velux can fix.

@madzrobz
Copy link
Contributor

Werner, I had the same symptoms.

These two fixes have solved the root causes for me:
home-assistant/core#23748
#25

@Julius2342 would you be so kind to release a new version of pyvlx including these fixes?

@Julius2342
Copy link
Owner

@Julius2342
Copy link
Owner

I think we can close this issue. Please reopen if problem still exists.

@apeeters
Copy link

@Julius2342 Unfortunately I still see this issue on 0.2.12.

@Julius2342
Copy link
Owner

do you see anything in the logs?

@Julius2342 Julius2342 reopened this Apr 16, 2020
@apeeters
Copy link

Nothing but this stacktrace.

The connection seems to survive a few restarts of Home Assistant, but at some point I get the following stacktrace and the only way to recover is power cycling the KLF and restarting Home Assistant.

2020-04-19 22:14:02 WARNING (MainThread) [pyvlx] Connecting to KLF 200.
2020-04-19 22:14:12 WARNING (MainThread) [homeassistant.setup] Setup of velux is taking over 10 seconds.
2020-04-19 22:15:02 WARNING (MainThread) [pyvlx] Connecting to KLF 200.
2020-04-19 22:15:03 ERROR (MainThread) [homeassistant.setup] Error during setup of component velux
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/setup.py", line 171, in _async_setup_component
    hass, processed_config
  File "/usr/src/homeassistant/homeassistant/components/velux/__init__.py", line 31, in async_setup
    await hass.data[DATA_VELUX].async_start()
  File "/usr/src/homeassistant/homeassistant/components/velux/__init__.py", line 69, in async_start
    await self.pyvlx.load_scenes()
  File "/usr/local/lib/python3.7/site-packages/pyvlx/pyvlx.py", line 87, in load_scenes
    await self.scenes.load()
  File "/usr/local/lib/python3.7/site-packages/pyvlx/scenes.py", line 51, in load
    await get_scene_list.do_api_call()
  File "/usr/local/lib/python3.7/site-packages/pyvlx/api_event.py", line 22, in do_api_call
    await self.send_frame()
  File "/usr/local/lib/python3.7/site-packages/pyvlx/api_event.py", line 34, in send_frame
    await self.pyvlx.send_frame(self.request_frame())
  File "/usr/local/lib/python3.7/site-packages/pyvlx/pyvlx.py", line 70, in send_frame
    await self.connect()
  File "/usr/local/lib/python3.7/site-packages/pyvlx/pyvlx.py", line 45, in connect
    await self.connection.connect()
  File "/usr/local/lib/python3.7/site-packages/pyvlx/connection.py", line 89, in connect
    ssl=self.create_ssl_context())
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 989, in create_connection
    ssl_handshake_timeout=ssl_handshake_timeout)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 1017, in _create_connection_transport
    await waiter
ConnectionAbortedError: SSL handshake is taking longer than 60.0 seconds: aborting the connection

@Julius2342
Copy link
Owner

Hmm ... no idea how to add a timeout to creating a connection ...

@rohrsh
Copy link

rohrsh commented May 4, 2020

My velux connection also fails after every few reboots of Home Assistant

@Julius2342
Copy link
Owner

I had a deeper look inside the issue, the problem is that disconnecting the SSL-Connecton is not async: https://github.com/Julius2342/pyvlx/blob/master/pyvlx/connection.py#L76

It looks like the shutdown of Hass became quicker recently, so the shutting down of the SSL connections is not awaited (not in the await meaning).

Therefore the connections on the KLF200 side are not closed down correctly and after several reconnects the KLF200 refused to take new connections.

@gaggio
Copy link

gaggio commented Jun 24, 2020

Any news on this issue? Still seeing this behaviour on home assistant 0.110.1.
Available to test and provide logs if helpful.

@AlecRust
Copy link

Also having this issue pretty consistently and can do the same ^

@pawlizio
Copy link
Collaborator

I also can confirm that KLF freezes. Actually after each HA reboot I have to restart the KLF.

@Julius2342
Copy link
Owner

As said, i have no idea how to mitigate this and i need help:

  • The problem is that the connection within KLF 200 s not cleared up correctly. The device freezes and you cant connect.
  • There is no disconnect command within KLF 200.
  • The logic here was not changed within a year.

The only suspicion i have is that the SSL connection is not disconnected properly, bc the process ends before everythng is cleared up correctly. But this is just a suspicion. I have no idea what was changed within HASS and what could cause this problem.

@pawlizio
Copy link
Collaborator

I read in a FHEM forum that they automatically reboot the KLF via API if they recognized any connection problems (I don't know how, maybe they count them somehow) to avoid a manual reboot. This would support your suspicion.

I'm not familiar with all this IP sockets and event loops, but when I tried today to build up several new connections without closing them properly I could not identify any connection problems. I used the demo files from https://www.velux.com/klf200 to perform some tests.

Also I tried reproduce the "SSL handshake takes too long" issue by restarting HA several times but I could not identify any connection problems. However always if I update my HA, I have to reboot the KLF. Not sure what exactly is causing this issue.

@Julius2342
Copy link
Owner

@pawlizio : How can you reboot via API if you cant connect? 🤔

@pawlizio
Copy link
Collaborator

Of course you have to reboot in advance, not if you don't have any possibility to connect.

What I understand is that 2 sockets can be established at maximum with the KLF and that KLF does not close them properly after 15 minutes without communication, as written in their API description. So your suspicion is that KLF freezes or becomes irresponsive if you use both sockets without closing them properly,

Now a possible solution is that if pyvlx establishes a connection for the first time an automatic reboot of the KLF could be initiated. In this way you ensure that you always have the 2nd socket if you loose your connection. Just count the reconnects within pyvlx and on each 2nd connection force a reboot (after 1st connection, after 3rd connection and so on).

@rohrsh
Copy link

rohrsh commented Jun 25, 2020 via email

@abey79
Copy link

abey79 commented Jun 26, 2020

I'm experiencing this issue, mainly when I updated HA. I'm powering the KLF via one of the Raspberry Pi USB port, so in theory I could power the USB off to force a reboot but it's probably complicated to do with HassOS (plus I have other devices on the other USB ports).

@pawlizio
Copy link
Collaborator

I created a pull request #43 for testing purpose. I implemented the same in my custom component today.
As I could not reproduce the freeze of KLF systematically I only can wait and hope that there is an improvement on this issue.

@pawlizio
Copy link
Collaborator

@Julius2342: A few days ago I saw this velux API implementation and just now had an idea.

There is an explanation under which condition the TLS does not work:
"If there is no communication with the KLF every 10 minutes to 15 minutes, the connection will be disconnected as described in the manual. If this happens when the home monitor "GW_HOUSE_STATUS_MONITOR_ENABLE_REQ" is activated, the KLF200 is no longer reachable. The KLF200 no longer sends the TLS command "Change Cipher Spec."

As a consequent in order to avoid unresponsive communication, we should try to deactivate house status monitor before closing any connection. May this could solve most of the problems during HA restarts.

@enricozocca
Copy link

Hello! I've got similar issue: home-assistant/core#48182

Enrico

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants