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)
|
super().__init__(parent)
|
||||||
self.config = config
|
self.config = config
|
||||||
|
self._background_threads = [] # Keep references to background threads
|
||||||
|
|
||||||
# Set window properties
|
# Set window properties
|
||||||
self.setWindowTitle(f"{config.app_name} v{config.app_version}")
|
self.setWindowTitle(f"{config.app_name} v{config.app_version}")
|
||||||
|
|
@ -482,6 +483,7 @@ class MainWindow(QMainWindow):
|
||||||
Args:
|
Args:
|
||||||
manager: UpdateManager instance
|
manager: UpdateManager instance
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
# Create and start background thread
|
# Create and start background thread
|
||||||
thread = QThread()
|
thread = QThread()
|
||||||
worker = UpdateCheckWorker(manager, self.config.app_version)
|
worker = UpdateCheckWorker(manager, self.config.app_version)
|
||||||
|
|
@ -490,11 +492,25 @@ class MainWindow(QMainWindow):
|
||||||
worker.update_available.connect(self._on_update_available)
|
worker.update_available.connect(self._on_update_available)
|
||||||
worker.update_status.connect(self._on_update_status)
|
worker.update_status.connect(self._on_update_status)
|
||||||
worker.finished.connect(thread.quit)
|
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
|
# Start thread
|
||||||
worker.moveToThread(thread)
|
worker.moveToThread(thread)
|
||||||
thread.started.connect(worker.run)
|
thread.started.connect(worker.run)
|
||||||
thread.start()
|
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:
|
def _on_update_status(self, status: str, emoji: str) -> None:
|
||||||
"""Handle update status changes.
|
"""Handle update status changes.
|
||||||
|
|
@ -682,11 +698,25 @@ class UpdateCheckWorker(QObject):
|
||||||
# Notify checking status
|
# Notify checking status
|
||||||
self.update_status.emit("Checking for updates", "🔄")
|
self.update_status.emit("Checking for updates", "🔄")
|
||||||
|
|
||||||
# Run async check
|
try:
|
||||||
|
# Run async check with timeout
|
||||||
loop = asyncio.new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
asyncio.set_event_loop(loop)
|
asyncio.set_event_loop(loop)
|
||||||
release = loop.run_until_complete(self.manager.check_for_updates())
|
release = loop.run_until_complete(self.manager.check_for_updates())
|
||||||
loop.close()
|
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
|
# Emit result
|
||||||
if release:
|
if release:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue