[#389] Possible fix for "Too many open files" media issue.

It seems that the process keeps a lot of open connections to Chromecast
devices during playback.

The most likely culprit is the `_refresh_chromecasts` logic.

We should start a `cast` object and register a status listener only if a
Chromecast with the same identifier isn't already registered in the
plugin.
This commit is contained in:
Fabio Manganiello 2024-04-15 23:01:10 +02:00
parent 027bcea612
commit 33d4c8342d
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -105,7 +105,7 @@ class MediaChromecastPlugin(MediaPlugin, RunnablePlugin):
:param callback: If blocking is false, then you can provide a callback :param callback: If blocking is false, then you can provide a callback
function that will be invoked when a new device is discovered function that will be invoked when a new device is discovered
""" """
self.chromecasts = { casts = {
self._get_device_property(cast, 'friendly_name'): cast self._get_device_property(cast, 'friendly_name'): cast
for cast in self._get_chromecasts( for cast in self._get_chromecasts(
tries=tries, tries=tries,
@ -116,11 +116,18 @@ class MediaChromecastPlugin(MediaPlugin, RunnablePlugin):
) )
} }
for name, cast in self.chromecasts.items(): for name, cast in casts.copy().items():
if not self.chromecasts.get(name):
self.logger.info('Discovered new Chromecast: %s', name)
self.chromecasts[name] = cast
self._update_listeners(name, cast) self._update_listeners(name, cast)
for cast in self.chromecasts.values():
cast.wait() cast.wait()
else:
casts.pop(name)
for name, cast in casts.items():
cast.wait()
self.logger.info('Refreshed Chromecast state: %s', name)
def _event_callback(self, _, cast: pychromecast.Chromecast): def _event_callback(self, _, cast: pychromecast.Chromecast):
with self._refresh_lock: with self._refresh_lock:
@ -133,6 +140,7 @@ class MediaChromecastPlugin(MediaPlugin, RunnablePlugin):
name=name, cast=cast, callback=self._event_callback name=name, cast=cast, callback=self._event_callback
) )
cast.media_controller.register_status_listener(self._media_listeners[name]) cast.media_controller.register_status_listener(self._media_listeners[name])
self.logger.debug('Started media listener for %s', name)
def get_chromecast(self, chromecast=None, n_tries=2): def get_chromecast(self, chromecast=None, n_tries=2):
if isinstance(chromecast, pychromecast.Chromecast): if isinstance(chromecast, pychromecast.Chromecast):