Mic92 / python-mpd2

Python library which provides a client interface for the Music Player Daemon.
GNU Lesser General Public License v3.0
352 stars 119 forks source link

_MPDClient__wfile attribute can be missing on async MPDClient #152

Closed mweinelt closed 3 years ago

mweinelt commented 3 years ago

The async MPDClient apparently can get into a weird state, where trying to call disconnect() on it results in the following traceback:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
    await task
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 174, in async_update
    self._disconnect()
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 133, in _disconnect
    self._client.disconnect()
  File "/usr/local/lib/python3.8/site-packages/mpd/asyncio.py", line 188, in disconnect
    if self.__wfile is not None:
AttributeError: 'MPDClient' object has no attribute '_MPDClient__wfile'

We're already catching mpd.ConnectionError for disconnect, but that AttributeError should probably be handled in python-mpd2.

mweinelt commented 3 years ago

This can happen if the connection setup fails, then self.__rfile and self.__wfile are never initialized.

https://github.com/Mic92/python-mpd2/blob/bc571eb12482ee2b80d5c3696ac35f6ba27b545d/mpd/asyncio.py#L163-L168