Skip to content

Catch asyncio.exceptions.CancelledError when attempting to connect over websocket #7

Closed

Description

@dperfect

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.

https://github.com/OnFreund/PyKodi/blob/c02efc01659b8314223dfade6dbcbd289d77ec2a/pykodi/kodi.py#L136

Activity

  1. OnFreund commented on Jan 20, 2021

    @OnFreund
    Collaborator

    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.

  2. dperfect commented on Jan 20, 2021

    @dperfect
    Author

    @OnFreund - It does help in that it's being raised as a CannotConnectError and 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.

  3. OnFreund commented on Jan 21, 2021

    @OnFreund
    Collaborator

    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.

  4. OnFreund commented on Jan 21, 2021

    @OnFreund
    Collaborator

    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.

  5. dperfect commented on Jan 21, 2021

    @dperfect
    Author

    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.

  6. OnFreund commented on Jan 21, 2021

    @OnFreund
    Collaborator

    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?

  7. dperfect commented on Jan 21, 2021

    @dperfect
    Author

    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.

  8. dperfect commented on Jan 21, 2021

    @dperfect
    Author

    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).

    Apologies for the confusion!

  9. OnFreund commented on Jan 22, 2021

    @OnFreund
    Collaborator

    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.

  10. dperfect commented on Jan 22, 2021

    @dperfect
    Author

    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.

  11. OnFreund commented on Jan 23, 2021

    @OnFreund
    Collaborator

    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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions