You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Catch asyncio.exceptions.CancelledError when attempting to connect over websocket #7
When Kodi's Allow remote control from applications on other systems option is disabled with Windows 10 firewall enabled, it appears to be dropping the connection rather than rejecting it. This causes the connection to timeout with asyncio.exceptions.CancelledError. The exception should probably be handled as a CannotConnectError.
... but for some reason that's still being reported in the HA UI as "Unknown error occurred" (on the first step - never making it to the websocket prompt). With my limited Python experience and unfamiliarity with HA's codebase, I'm having a hard time identifying why it's still not working. I suspect it may be something higher up in the HA stack timing out or intercepting the error in a different way, but I really have no idea.
That's really strange. If you can verify that it was caught in line 164 there really shouldn't be anything that prevents the WS step from showing (it doesn't do much more than showing the form from that point).
My best guess is some environment issue - perhaps HA getting the package again from PyPI and thus reverting the change, or something like that.
In the meantime I've published a new version of the package (0.2.2). This way you can change the required version in HA (make sure to search for all instances of pykodi==0.2.1 and replace), and it should be easier to test.
Yes, I can confirm it's being caught on line 164, but still failing with "Unknown error occurred". The only thing I added, apart from the same change you made in 0.2.2, was a line between 164 and 165 to log it (_LOGGER.warn("Caught WSCannotConnect")), and that's showing up in the log when it happens.
I suspect it may have something to do with the async nature of the task, and the connection being timed out/canceled by whatever is supervising the task. I've timed the error multiple times, and it always takes exactly 50 seconds, which I can't seem to track down in the context of the kodi/component code. Again, this is over my head in Python, but I've seen similar issues in other languages where an async task is canceled (and handled) at a higher level, while a timeout/canceled exception is also thrown in the task itself, allowing it to perform any cleanup operations. I don't really have proof of that happening here, apart from the fact that the "Unknown error occurred" error doesn't seem to be coming from the code path in the kodi component - at least not from what I can see.
I don't believe steps within config flows are run within a timeout, so I don't think that's the cause.
Best guess would be that another exception is raised after. Can you add loggers to async_step_ws_port so we can see what's happening there?
Yes, it makes it all the way down to calling self._show_ws_port_form (line 222), and that in turn in making it down to self.async_show_form (line 288), so as mentioned, it seems that the code flow within config_flow.py is not responsible for the error that's actually being shown in the UI.
Ok, after some more digging, I found the issue: my HA setup's reverse proxy (haproxy on the same Portainer instance running in my home) was cutting the connection prematurely with 504 Gateway Time-out after 50 seconds. When hitting HA directly, it works as intended, but only proceeds to the websocket step after 2 minutes and 10 seconds. If that's the intended behavior, then I guess it's all good. On the other hand, somehow adjusting the timeout to be a bit shorter may help in other cases where HA is behind a reverse proxy (and would improve the UX so people don't have to wait as long, though that's probably a matter of opinion).
Ok, a reverse proxy doing that makes way more sense :)
I have to wonder if the reverse proxy is the one cancelling the request, and when hitting HA directly this would have worked even without catching CancelledError...
I think it makes sense to limit the wait, but don't know when I'll get around to changing that.
Yes, you're right - when hitting HA directly, it doesn't throw CancelledError, so I guess that wasn't really necessary (though maybe still a good idea to catch). Thanks for your patience and help.
Activity
OnFreund commentedon Jan 20, 2021
Thanks @dperfect. Can you check if this solves it for you:
#8
If it does, I'll merge, publish a new package, and update the HA integration to use that.
dperfect commentedon Jan 20, 2021
@OnFreund - It does help in that it's being raised as a
CannotConnectErrorand caught here in HA:https://github.com/home-assistant/core/blob/b2c9bd2ca67df277093790e3fc0def34d9f7fb04/homeassistant/components/kodi/config_flow.py#L81
and subsequently here:
https://github.com/home-assistant/core/blob/b2c9bd2ca67df277093790e3fc0def34d9f7fb04/homeassistant/components/kodi/config_flow.py#L164
... but for some reason that's still being reported in the HA UI as "Unknown error occurred" (on the first step - never making it to the websocket prompt). With my limited Python experience and unfamiliarity with HA's codebase, I'm having a hard time identifying why it's still not working. I suspect it may be something higher up in the HA stack timing out or intercepting the error in a different way, but I really have no idea.
OnFreund commentedon Jan 21, 2021
That's really strange. If you can verify that it was caught in line 164 there really shouldn't be anything that prevents the WS step from showing (it doesn't do much more than showing the form from that point).
My best guess is some environment issue - perhaps HA getting the package again from PyPI and thus reverting the change, or something like that.
OnFreund commentedon Jan 21, 2021
In the meantime I've published a new version of the package (0.2.2). This way you can change the required version in HA (make sure to search for all instances of
pykodi==0.2.1and replace), and it should be easier to test.dperfect commentedon Jan 21, 2021
Yes, I can confirm it's being caught on line 164, but still failing with "Unknown error occurred". The only thing I added, apart from the same change you made in 0.2.2, was a line between 164 and 165 to log it (
_LOGGER.warn("Caught WSCannotConnect")), and that's showing up in the log when it happens.I suspect it may have something to do with the async nature of the task, and the connection being timed out/canceled by whatever is supervising the task. I've timed the error multiple times, and it always takes exactly 50 seconds, which I can't seem to track down in the context of the kodi/component code. Again, this is over my head in Python, but I've seen similar issues in other languages where an async task is canceled (and handled) at a higher level, while a timeout/canceled exception is also thrown in the task itself, allowing it to perform any cleanup operations. I don't really have proof of that happening here, apart from the fact that the "Unknown error occurred" error doesn't seem to be coming from the code path in the kodi component - at least not from what I can see.
OnFreund commentedon Jan 21, 2021
I don't believe steps within config flows are run within a timeout, so I don't think that's the cause.
Best guess would be that another exception is raised after. Can you add loggers to
async_step_ws_portso we can see what's happening there?dperfect commentedon Jan 21, 2021
Yes, it makes it all the way down to calling
self._show_ws_port_form(line 222), and that in turn in making it down toself.async_show_form(line 288), so as mentioned, it seems that the code flow withinconfig_flow.pyis not responsible for the error that's actually being shown in the UI.dperfect commentedon Jan 21, 2021
Ok, after some more digging, I found the issue: my HA setup's reverse proxy (haproxy on the same Portainer instance running in my home) was cutting the connection prematurely with
504 Gateway Time-outafter 50 seconds. When hitting HA directly, it works as intended, but only proceeds to the websocket step after 2 minutes and 10 seconds. If that's the intended behavior, then I guess it's all good. On the other hand, somehow adjusting the timeout to be a bit shorter may help in other cases where HA is behind a reverse proxy (and would improve the UX so people don't have to wait as long, though that's probably a matter of opinion).Apologies for the confusion!
OnFreund commentedon Jan 22, 2021
Ok, a reverse proxy doing that makes way more sense :)
I have to wonder if the reverse proxy is the one cancelling the request, and when hitting HA directly this would have worked even without catching
CancelledError...I think it makes sense to limit the wait, but don't know when I'll get around to changing that.
dperfect commentedon Jan 22, 2021
Yes, you're right - when hitting HA directly, it doesn't throw
CancelledError, so I guess that wasn't really necessary (though maybe still a good idea to catch). Thanks for your patience and help.OnFreund commentedon Jan 23, 2021
OK, I won't revert that change because I think it does make some sense, but now I'm in no rush to update the PyKodi version used in HA :)