From 5daafef5e67d3057def2a099362f53c34aabdab6 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 6 Dec 2023 11:38:23 +1000 Subject: [PATCH] Fix mpv idle notification not using main thread Message arrived on a background non-Qt thread, and called run_in_background(), which assumes it's running on the GUI thread. This resulted in single_shot() failing to run in reviewer's on_av_player_did_end_playing on Linux/macOS. --- qt/aqt/sound.py | 4 +++- qt/aqt/taskman.py | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/qt/aqt/sound.py b/qt/aqt/sound.py index 1f617e23e..98078cee2 100644 --- a/qt/aqt/sound.py +++ b/qt/aqt/sound.py @@ -442,7 +442,9 @@ class MpvManager(MPV, SoundOrVideoPlayer): def on_property_idle_active(self, value: bool) -> None: if value and self._on_done: - self._on_done() + from aqt import mw + + mw.taskman.run_on_main(self._on_done) def shutdown(self) -> None: self.close() diff --git a/qt/aqt/taskman.py b/qt/aqt/taskman.py index ae5a4d421..0acbcba6c 100644 --- a/qt/aqt/taskman.py +++ b/qt/aqt/taskman.py @@ -11,7 +11,7 @@ from __future__ import annotations from concurrent.futures import Future from concurrent.futures.thread import ThreadPoolExecutor -from threading import Lock +from threading import Lock, current_thread, main_thread from typing import Any, Callable import aqt @@ -65,7 +65,11 @@ class TaskManager(QObject): # to the database that we want to run first - if we delay them until after the # background task starts, and it takes out a long-running lock on the database, # the UI thread will hang until the end of the op. - self._on_closures_pending() + if current_thread() is main_thread(): + self._on_closures_pending() + else: + print("bug: run_in_background not called from main thread") + traceback.print_stack() if args is None: args = {}