home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.13k stars 29.8k forks source link

RecursionError in Husqvarna Automower coordinator #123008

Open MartinHjelmare opened 1 month ago

MartinHjelmare commented 1 month ago

The problem

Problem

If there's an error when connecting the websocket the coordinator will retry after sleeping a bit. The retry is done in a recursive fashion in the same task by calling client_listen again. If the integration isn't reloaded for a long time the risk increases that eventually the coordinator will hit maximum recursion limit since the same task is recursing more and more on every connection problem even if there are successful connections in between.

Solution

Instead of recursing in the same task the coordinator should create a new background task for the call to client_listen on retry.

Problem code

https://github.com/home-assistant/core/blob/d16a2fac80b5e3e897442bef786d89e4aa393a94/homeassistant/components/husqvarna_automower/coordinator.py#L62-L87

What version of Home Assistant Core has the issue?

core-2024.7.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Husqvarna Automower

Link to integration documentation on our website

https://www.home-assistant.io/integrations/husqvarna_automower/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Logger: homeassistant
Source: components/husqvarna_automower/coordinator.py:71
First occurred: July 30, 2024 at 17:55:16 (1 occurrences)
Last logged: July 30, 2024 at 17:55:16

Error doing job: Task exception was never retrieved (None)
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/asyncio/selector_events.py", line 649, in _sock_connect
    sock.connect(address)
BlockingIOError: [Errno 115] Operation in progress

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/husqvarna_automower/coordinator.py", line 82, in client_listen
    await self.client_listen(
  File "/usr/src/homeassistant/homeassistant/components/husqvarna_automower/coordinator.py", line 82, in client_listen
    await self.client_listen(
  File "/usr/src/homeassistant/homeassistant/components/husqvarna_automower/coordinator.py", line 82, in client_listen
    await self.client_listen(
  [Previous line repeated 972 more times]
  File "/usr/src/homeassistant/homeassistant/components/husqvarna_automower/coordinator.py", line 71, in client_listen
    await automower_client.auth.websocket_connect()
  File "/usr/local/lib/python3.12/site-packages/aioautomower/auth.py", line 191, in websocket_connect
    self.ws = await self._websession.ws_connect(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 835, in _ws_connect
    resp = await self.request(
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 581, in _request
    conn = await self._connector.connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 544, in connect
    proto = await self._create_connection(req, traces, timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 944, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 1226, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 1025, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1104, in create_connection
    sock = await self._connect_sock(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1007, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.12/asyncio/selector_events.py", line 639, in sock_connect
    self._sock_connect(fut, sock, address)
  File "/usr/local/lib/python3.12/asyncio/selector_events.py", line 656, in _sock_connect
    handle = self._add_writer(
             ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/selector_events.py", line 313, in _add_writer
    self._selector.register(fd, selectors.EVENT_WRITE,
  File "/usr/local/lib/python3.12/selectors.py", line 343, in register
    key = super().register(fileobj, events, data)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/selectors.py", line 242, in register
    key = SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/selectors.py", line 229, in _fileobj_lookup
    return _fileobj_to_fd(fileobj)
           ^^^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded

Additional information

No response

home-assistant[bot] commented 1 month ago

Hey there @thomas55555, mind taking a look at this issue as it has been labeled with an integration (husqvarna_automower) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `husqvarna_automower` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign husqvarna_automower` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


husqvarna_automower documentation husqvarna_automower source (message by IssueLinks)