fix: Improve background thread management and error handling in update check

This commit is contained in:
claudi 2026-01-29 15:27:00 +01:00
parent a8eaa84310
commit e57e822bed

View file

@ -194,6 +194,7 @@ class MainWindow(QMainWindow):
"""
super().__init__(parent)
self.config = config
self._background_threads = [] # Keep references to background threads
# Set window properties
self.setWindowTitle(f"{config.app_name} v{config.app_version}")
@ -482,6 +483,7 @@ class MainWindow(QMainWindow):
Args:
manager: UpdateManager instance
"""
try:
# Create and start background thread
thread = QThread()
worker = UpdateCheckWorker(manager, self.config.app_version)
@ -490,11 +492,25 @@ class MainWindow(QMainWindow):
worker.update_available.connect(self._on_update_available)
worker.update_status.connect(self._on_update_status)
worker.finished.connect(thread.quit)
worker.finished.connect(worker.deleteLater)
thread.finished.connect(thread.deleteLater)
# Keep reference to thread to prevent garbage collection
self._background_threads.append(thread)
# Clean up finished threads from list
def cleanup_thread():
if thread in self._background_threads:
self._background_threads.remove(thread)
thread.finished.connect(cleanup_thread)
# Start thread
worker.moveToThread(thread)
thread.started.connect(worker.run)
thread.start()
except Exception as e:
logger.error(f"Failed to start update check thread: {e}")
def _on_update_status(self, status: str, emoji: str) -> None:
"""Handle update status changes.
@ -682,11 +698,25 @@ class UpdateCheckWorker(QObject):
# Notify checking status
self.update_status.emit("Checking for updates", "🔄")
# Run async check
try:
# Run async check with timeout
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
release = loop.run_until_complete(self.manager.check_for_updates())
loop.close()
except RuntimeError as e:
# Handle event loop already running or other asyncio issues
logger.warning(f"Asyncio error during update check: {e}")
# Try using existing loop
try:
loop = asyncio.get_event_loop()
if loop.is_closed():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
release = loop.run_until_complete(self.manager.check_for_updates())
except Exception as retry_error:
logger.error(f"Failed to check updates on retry: {retry_error}")
release = None
# Emit result
if release: