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

MPD integration may break when MPD service becomes unreachable #103889

Closed
2franix opened this issue Nov 12, 2023 · 5 comments · Fixed by #108143
Closed

MPD integration may break when MPD service becomes unreachable #103889

2franix opened this issue Nov 12, 2023 · 5 comments · Fixed by #108143

Comments

@2franix
Copy link
Contributor

2franix commented Nov 12, 2023

The problem

This issue describes an upstream problem in python-mpd2 for which I have submitted a fix here
I figured it was helpful to open an issue here because:

  • it impacts the MPD integration in Home Assistant, which becomes totally unusable after the issue occurred. All MPD devices appear "unavailable" in HA, even after the connection to MPD is restored. The HA instance must be restarted for the integration to work again.
  • it can be used to track progress in resolving the upstream issue so that HA users can know what's going on.

Details of the symptoms in HA:
If the connection with MPD breaks while the integration is reading MPD's response to a command (for me, it happened while reading the response to the "status" command here) the ConnectionError raised within python-mpd2 is not propagated to the client code (HA's MpdDevice class in our use case). Instead, the coroutine executing the MPD command hangs forever and this is what causes the integration to freeze. Restoring the connection to MPD does not help.

What version of Home Assistant Core has the issue?

at least in any >= 2023.10.0

What was the last working version of Home Assistant Core?

I don't know

What type of installation are you running?

Home Assistant Core

Integration causing the issue

MPD

Link to integration documentation on our website

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

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

2023-11-10 22:54:00.724 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback MPDClient.__idle_result(<CommandResul...eading line')>)
Traceback (most recent call last):
  File "/home/homeassistant/.pyenv/versions/3.11.4/lib/python3.11/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 335, in __idle_result
    idle_changes = result.result()
                   ^^^^^^^^^^^^^^^
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 309, in __run
    await result._feed_from(self)
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 45, in _feed_from
    line = await mpdclient._read_line()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 391, in _read_line
    raise ConnectionError("Connection lost while reading line")
mpd.base.ConnectionError: Connection lost while reading line
2023-11-10 22:54:03.539 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback MPDClient.__idle_result(<CommandResul...set by peer')>)
Traceback (most recent call last):
  File "/home/homeassistant/.pyenv/versions/3.11.4/lib/python3.11/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 335, in __idle_result
    idle_changes = result.result()
                   ^^^^^^^^^^^^^^^
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 309, in __run
    await result._feed_from(self)
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 45, in _feed_from
    line = await mpdclient._read_line()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 389, in _read_line
    line = await self.__readline()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.cache/pypoetry/virtualenvs/homeassistant-maison-vaulx-gN-mN0gX-py3.11/lib/python3.11/site-packages/mpd/asyncio.py", line 362, in __readline
    data = await self.__rfile.readline()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.pyenv/versions/3.11.4/lib/python3.11/asyncio/streams.py", line 545, in readline
    line = await self.readuntil(sep)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/homeassistant/.pyenv/versions/3.11.4/lib/python3.11/asyncio/streams.py", line 637, in readuntil
    await self._wait_for_data('readuntil')
  File "/home/homeassistant/.pyenv/versions/3.11.4/lib/python3.11/asyncio/streams.py", line 522, in _wait_for_data
    await self._waiter
  File "/home/homeassistant/.pyenv/versions/3.11.4/lib/python3.11/asyncio/selector_events.py", line 995, in _read_ready__data_received
    data = self._sock.recv(self.max_size)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ConnectionResetError: [Errno 104] Connection reset by peer
2023-11-10 22:54:11.528 WARNING (MainThread) [homeassistant.components.media_player] Updating mpd media_player took longer than the scheduled update interval 0:00:10
2023-11-10 22:54:11.529 WARNING (MainThread) [homeassistant.helpers.entity] Update of media_player.ch3 is taking over 10 seconds
2023-11-10 22:54:21.529 WARNING (MainThread) [homeassistant.components.media_player] Updating mpd media_player took longer than the scheduled update interval 0:00:10

The last line corresponds to the next async update being aborted because the previous one is still running. It is actually stuck awaiting the "status" command, which will never complete.



### Additional information

_No response_
@home-assistant
Copy link

@2franix
Copy link
Contributor Author

2franix commented Nov 12, 2023

Once the upstream issue has been resolved, I'll submit a PR here to upgrade the python-mpd2 package! I don't have any ETA, as acceptance for my fix in upstream is out of my hands.

@seederp2p
Copy link

I have this problem.

Any news about it?

@2franix
Copy link
Contributor Author

2franix commented Dec 9, 2023

The PR was merged two weeks ago. Now I am waiting for a new release of python-mpd2 to be published to PyPI. Next I'll submit another PR here to pull that new version instead of the buggy 3.1.0.
In the meantime, depending on how you deploy HA, you may be able to patch it to pull the tip of python-mpd2's master branch:

  • locate the site-packages/homeassistant/components/mpd/manifest.json file within your Python environment and replace it with python_mpd2_patched_manifest.json (rename it to manifest.json after copying). This patched version removes the dependency to python-mpd2 so that HA stops pulling the v3.1.0 at startup
  • manually install the latest commit of python-mpd2: pip install python-mpd2@git+https://github.com/Mic92/python-mpd2

Restart HA and you should be good to go.

@2franix
Copy link
Contributor Author

2franix commented Jan 15, 2024

Version 3.1.1 of python-mpd2 that includes the fix was released over the weekend. I will work on upgrading this dependency in the mpd component ASAP, unless someone beats me to it.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants