fix: Improve background thread management and error handling in update check
This commit is contained in:
parent
a8eaa84310
commit
e57e822bed
1 changed files with 48 additions and 18 deletions
|
|
@ -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,19 +483,34 @@ class MainWindow(QMainWindow):
|
|||
Args:
|
||||
manager: UpdateManager instance
|
||||
"""
|
||||
# Create and start background thread
|
||||
thread = QThread()
|
||||
worker = UpdateCheckWorker(manager, self.config.app_version)
|
||||
|
||||
# Connect signals
|
||||
worker.update_available.connect(self._on_update_available)
|
||||
worker.update_status.connect(self._on_update_status)
|
||||
worker.finished.connect(thread.quit)
|
||||
|
||||
# Start thread
|
||||
worker.moveToThread(thread)
|
||||
thread.started.connect(worker.run)
|
||||
thread.start()
|
||||
try:
|
||||
# Create and start background thread
|
||||
thread = QThread()
|
||||
worker = UpdateCheckWorker(manager, self.config.app_version)
|
||||
|
||||
# Connect signals
|
||||
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
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
release = loop.run_until_complete(self.manager.check_for_updates())
|
||||
loop.close()
|
||||
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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue