From 64c46fce2d7203929cc0b14b9db956ba8c0fcbd9 Mon Sep 17 00:00:00 2001 From: imonlinux <39863321+imonlinux@users.noreply.github.com> Date: Mon, 17 Nov 2025 14:42:06 -0800 Subject: [PATCH] Update mpv_player.py added loop: asyncio.AbstractEventLoop to __init__ and used self.loop.call_threadsafe in _run_done_callback --- linux_voice_assistant/mpv_player.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/linux_voice_assistant/mpv_player.py b/linux_voice_assistant/mpv_player.py index 8da37e5..c69d3e0 100644 --- a/linux_voice_assistant/mpv_player.py +++ b/linux_voice_assistant/mpv_player.py @@ -6,6 +6,7 @@ improves readability with docstrings, and standardizes method signatures. """ from __future__ import annotations +import asyncio import logging import os from threading import Lock, Timer @@ -71,7 +72,13 @@ def _select_backend(player: MPV, device: Optional[str]) -> None: class MpvMediaPlayer: """A media player class that wraps the python-mpv library.""" - def __init__(self, device: Optional[str] = None, initial_volume: float = 1.0) -> None: + def __init__( + self, + loop: asyncio.AbstractEventLoop, + device: Optional[str] = None, + initial_volume: float = 1.0 + ) -> None: + self.loop = loop # <-- ADDED self.player = MPV( video=False, terminal=False, @@ -180,15 +187,16 @@ class MpvMediaPlayer: self._run_done_callback() def _run_done_callback(self) -> None: - """Safely runs the done_callback if it exists.""" + """Safely runs the done_callback on the main asyncio loop.""" with self._done_callback_lock: cb = self._done_callback self._done_callback = None if cb: try: - cb() + # Use call_soon_threadsafe to run the callback on the main loop + self.loop.call_soon_threadsafe(cb) # <-- MODIFIED except Exception: - _LOGGER.exception("Error in done_callback") + _LOGGER.exception("Error scheduling done_callback") def _mpv_log(self, level: str, prefix: str, text: str) -> None: """Routes mpv's internal logs to our logger."""