forked from trav/lintunes
v0.32.1: a stuck Rabbit connection can no longer freeze the window
Opening Connections called find_device() on the GUI thread, which lists the gvfs-MTP mount; on a wedged mount that blocks forever in an uninterruptible FUSE wait and the app had to be force-quit. DeviceWatcher now probes on a daemon thread (one at a time, STUCK after 3 s, late answers still land), the menu reads its cached answer, and a stuck mount shows "Rabbit isn't responding…" with a Reset Connection button (kill gvfsd-mtp, remount via gio). Tests can no longer reach the real mount (conftest patches find_device), which also takes the suite from ~200 s to ~22 s.
This commit is contained in:
+82
-12
@@ -1,4 +1,5 @@
|
||||
import shutil
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from PyQt6.QtWidgets import (
|
||||
@@ -6,7 +7,7 @@ from PyQt6.QtWidgets import (
|
||||
QPlainTextEdit, QTextEdit, QAbstractSpinBox, QComboBox, QApplication,
|
||||
QLabel, QMessageBox, QFileDialog, QProgressBar, QPushButton,
|
||||
)
|
||||
from PyQt6.QtCore import Qt, QEvent, QTimer
|
||||
from PyQt6.QtCore import Qt, QEvent, QObject, QTimer, pyqtSignal
|
||||
from PyQt6.QtGui import QAction, QKeySequence
|
||||
|
||||
from lintunes import device_sync, music_folder, theme, url_import
|
||||
@@ -82,6 +83,11 @@ class _CancelSyncButton(QPushButton):
|
||||
super().leaveEvent(event)
|
||||
|
||||
|
||||
class _ResetRelay(QObject):
|
||||
"""Carries reset_mtp_connection's answer back to the GUI thread."""
|
||||
done = pyqtSignal(object)
|
||||
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
def __init__(self, manager, prefs, lastfm=None, parent=None):
|
||||
super().__init__(parent)
|
||||
@@ -93,6 +99,11 @@ class MainWindow(QMainWindow):
|
||||
# Owns the cast session, if any. Built before the transport bar, which
|
||||
# takes it to drive the little cast indicator under the volume slider.
|
||||
self._cast = CastController(self.player, self)
|
||||
# Where the Rabbit is, found off the GUI thread: a wedged gvfs-MTP
|
||||
# mount blocks any listing of it forever (Round 71). Nothing probes
|
||||
# until the Connections menu opens.
|
||||
self._devices = device_sync.DeviceWatcher(parent=self)
|
||||
self._devices.changed.connect(self._refresh_device_actions)
|
||||
# Keep the machine awake while audio is actually playing.
|
||||
self._inhibitor = SleepInhibitor()
|
||||
# Context playback started from: "library" or "playlist:<pid>". Scopes
|
||||
@@ -413,8 +424,8 @@ class MainWindow(QMainWindow):
|
||||
self._cassette.build_menu(connections_menu)
|
||||
self._cast_action = self._add_action(
|
||||
connections_menu, "Connect to Chromecast…", "", self._toggle_cast)
|
||||
# Re-checked every time the menu opens: cheap (one gvfs listdir), and
|
||||
# always reflects plug/unplug, the cast session and the current view.
|
||||
# Re-checked every time the menu opens. The device part is a
|
||||
# background probe; its answer updates the open menu when it lands.
|
||||
connections_menu.aboutToShow.connect(self._refresh_connection_actions)
|
||||
|
||||
@property
|
||||
@@ -475,11 +486,8 @@ class MainWindow(QMainWindow):
|
||||
def _refresh_connection_actions(self):
|
||||
# An export in flight owns the same status-bar widgets, so it blocks
|
||||
# a sync just as another sync would.
|
||||
busy = self._busy_worker() is not None
|
||||
device = device_sync.find_device()
|
||||
# Whole-library sync doesn't care which view is open — only that a
|
||||
# device is plugged in and at least one playlist is ticked.
|
||||
self._andtunes_action.setEnabled(not busy and device is not None)
|
||||
self._devices.probe()
|
||||
self._refresh_device_actions()
|
||||
self._cassette.refresh_menu()
|
||||
# Enabled whenever there's an APK to offer; the click finds the
|
||||
# Rabbit (adb or MTP) and says so if it can't — cheaper than asking
|
||||
@@ -583,6 +591,57 @@ class MainWindow(QMainWindow):
|
||||
# cancelled sync has news for the journals.
|
||||
self._manager.reload_from_disk()
|
||||
|
||||
def _refresh_device_actions(self):
|
||||
"""Sync to Rabbit from the watcher's cached answer — never a fresh
|
||||
look at the mount, which is what froze the menu."""
|
||||
busy = self._busy_worker() is not None
|
||||
if self._devices.state == device_sync.STUCK:
|
||||
# Enabled: the click explains and offers the reset.
|
||||
self._andtunes_action.setText("Rabbit isn't responding…")
|
||||
self._andtunes_action.setEnabled(True)
|
||||
return
|
||||
self._andtunes_action.setText("Sync to Rabbit")
|
||||
# Whole-library sync doesn't care which view is open — only that a
|
||||
# device is plugged in and at least one playlist is ticked.
|
||||
self._andtunes_action.setEnabled(
|
||||
not busy and self._devices.device is not None)
|
||||
|
||||
def _rabbit_stuck(self):
|
||||
"""The mount stopped answering: say so, and offer the CLAUDE.md
|
||||
recovery (kill gvfsd-mtp, remount) as one button."""
|
||||
box = QMessageBox(self)
|
||||
box.setIcon(QMessageBox.Icon.Warning)
|
||||
box.setWindowTitle("Rabbit isn't responding")
|
||||
box.setText("The Rabbit's USB connection has stopped answering, so "
|
||||
"LinTunes can't reach it.")
|
||||
box.setInformativeText(
|
||||
"Reset Connection restarts the desktop's MTP service and "
|
||||
"remounts the Rabbit. If that doesn't do it, unplug the Rabbit "
|
||||
"and plug it back in.")
|
||||
reset = box.addButton("Reset Connection",
|
||||
QMessageBox.ButtonRole.AcceptRole)
|
||||
box.addButton(QMessageBox.StandardButton.Cancel)
|
||||
box.setDefaultButton(reset)
|
||||
box.exec()
|
||||
if box.clickedButton() is not reset:
|
||||
return
|
||||
self.statusBar().showMessage("Resetting the Rabbit connection…")
|
||||
relay = self._reset_relay = _ResetRelay(self)
|
||||
relay.done.connect(self._on_rabbit_reset)
|
||||
threading.Thread(
|
||||
target=lambda: relay.done.emit(device_sync.reset_mtp_connection()),
|
||||
daemon=True).start()
|
||||
|
||||
def _on_rabbit_reset(self, error):
|
||||
if error:
|
||||
self.statusBar().clearMessage()
|
||||
QMessageBox.warning(self, "Rabbit isn't responding",
|
||||
f"The reset didn't work: {error}\n\n"
|
||||
"Unplug the Rabbit and plug it back in.")
|
||||
return
|
||||
self.statusBar().showMessage("Rabbit connection reset", 6000)
|
||||
self._devices.probe()
|
||||
|
||||
def _on_sync_failed(self, message):
|
||||
self._sync_inhibitor.release()
|
||||
self._hide_sync_widgets()
|
||||
@@ -591,7 +650,7 @@ class MainWindow(QMainWindow):
|
||||
# ---- connections: andTunes (whole-library sync) ----
|
||||
|
||||
def _show_device_sync_settings(self):
|
||||
device = device_sync.find_device()
|
||||
device = self._devices.device # only for its name
|
||||
dialog = DeviceSyncSettingsDialog(
|
||||
self._prefs, self._manager.library, self,
|
||||
device_name=device.name if device else "Rabbit")
|
||||
@@ -606,8 +665,13 @@ class MainWindow(QMainWindow):
|
||||
# Everything may have changed since the menu opened — re-verify.
|
||||
if self._busy_worker() is not None:
|
||||
return
|
||||
device = device_sync.find_device()
|
||||
if device is None:
|
||||
self._devices.when_ready(self._sync_to_device)
|
||||
|
||||
def _sync_to_device(self, device, state):
|
||||
if state == device_sync.STUCK:
|
||||
self._rabbit_stuck()
|
||||
return
|
||||
if device is None or self._busy_worker() is not None:
|
||||
return
|
||||
playlists = self._selected_sync_playlists()
|
||||
if not playlists:
|
||||
@@ -731,9 +795,15 @@ class MainWindow(QMainWindow):
|
||||
apk = andtunes_install.bundled_apk()
|
||||
if apk is None:
|
||||
return
|
||||
self._devices.when_ready(
|
||||
lambda device, state: self._install_andtunes_to(apk, device, state))
|
||||
|
||||
def _install_andtunes_to(self, apk, device, state):
|
||||
adb = andtunes_install.adb_path()
|
||||
serial = andtunes_install.rabbit_serial(adb) if adb else None
|
||||
device = device_sync.find_device()
|
||||
if serial is None and state == device_sync.STUCK:
|
||||
self._rabbit_stuck()
|
||||
return
|
||||
if serial is None and device is None:
|
||||
QMessageBox.information(
|
||||
self, "Rabbit not found",
|
||||
|
||||
Reference in New Issue
Block a user