fix: QThread crash on iPod connect and device detection — proper wait() lifecycle
Root cause: QThread C++ destructor ran while underlying OS thread was still terminating. The pattern of checking isRunning() and using short timeouts (wait(3000/5000)) left a window where the Python reference was dropped and Shiboken destroyed the C++ object before the thread fully exited. Fixes: - Remove all self.finished.connect(self.deleteLater) — unsafe, deleteLater from within run() or from worker thread races with thread termination - _cleanup_worker: always call wait() (no timeout) before nulling the Python reference — guarantees OS thread is fully dead - Add _is_worker_running() helper with try/except RuntimeError guard to safely check stale C++ objects - DeviceMonitor._poll/stop/wait: use wait() with no timeout - closeEvent: guard isRunning() with try/except RuntimeError
This commit is contained in:
+20
-7
@@ -11,6 +11,9 @@ logger = logging.getLogger(__name__)
|
||||
class _DetectWorker(QThread):
|
||||
finished = pyqtSignal(object)
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
devices = IPodDevice().detect_devices()
|
||||
@@ -38,19 +41,29 @@ class DeviceMonitor(QObject):
|
||||
|
||||
def stop(self):
|
||||
self.timer.stop()
|
||||
if self._poll_worker and self._poll_worker.isRunning():
|
||||
if self._poll_worker:
|
||||
self._poll_worker.quit()
|
||||
self._poll_worker.wait(3000)
|
||||
self._poll_worker.wait()
|
||||
self._poll_worker = None
|
||||
self._device_ids.clear()
|
||||
|
||||
def _poll(self):
|
||||
if self._poll_worker and self._poll_worker.isRunning():
|
||||
return
|
||||
self._poll_worker = _DetectWorker()
|
||||
self._poll_worker.finished.connect(self._on_devices_detected)
|
||||
self._poll_worker.start()
|
||||
if self._poll_worker:
|
||||
try:
|
||||
if self._poll_worker.isRunning():
|
||||
return
|
||||
except RuntimeError:
|
||||
self._poll_worker = None
|
||||
worker = _DetectWorker()
|
||||
worker.finished.connect(self._on_devices_detected)
|
||||
worker.start()
|
||||
self._poll_worker = worker
|
||||
|
||||
def _on_devices_detected(self, devices):
|
||||
if self._poll_worker:
|
||||
self._poll_worker.quit()
|
||||
self._poll_worker.wait()
|
||||
self._poll_worker = None
|
||||
current_ids = {d["id"] for d in devices}
|
||||
if current_ids != self._device_ids:
|
||||
self._device_ids = current_ids
|
||||
|
||||
Reference in New Issue
Block a user