Skip to content

Commit

Permalink
Fix crashes on exit by properly exiting threads
Browse files Browse the repository at this point in the history
The Python destructors (__del__) run much too late to be useful
to enforce proper shutdown order, i.e. stopping a QThread before
the application is torn down.
  • Loading branch information
jonathanperret committed Jun 27, 2024
1 parent aec00c4 commit 18405b7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/main/python/main/ayab/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,5 @@ def __init__(self, parent: GuiMain):
self.__worker.moveToThread(self)
self.start()

def __del__(self) -> None:
self.wait()

def play(self, sound: str) -> None:
self.__worker.play(sound, blocking=False)
14 changes: 12 additions & 2 deletions src/main/python/main/ayab/ayab.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"""Provides a graphical interface for users to operate AYAB."""

from __future__ import annotations
import sys
import logging

from PySide6.QtWidgets import QMainWindow
from PySide6.QtCore import QCoreApplication
from PySide6.QtGui import QCloseEvent

from .main_gui import Ui_MainWindow
from .gui_fsm import gui_fsm
Expand Down Expand Up @@ -100,6 +100,17 @@ def __init__(self, app_context: AppContext):
self.fsm.set_properties(self)
self.fsm.machine.start()

def closeEvent(self, event: QCloseEvent) -> None:
self.audio.quit()
self.audio.wait()

self.engine.cancel()

self.knit_thread.wait()
self.test_thread.wait()

super().closeEvent(event)

def __activate_ui(self) -> None:
self.ui.open_image_file_button.clicked.connect(self.scene.ayabimage.select_file)
self.ui.filename_lineedit.returnPressed.connect(
Expand Down Expand Up @@ -135,7 +146,6 @@ def __quit(self) -> None:
instance = QCoreApplication.instance()
if instance is not None:
instance.quit()
sys.exit()

def start_knitting(self) -> None:
"""Start the knitting process."""
Expand Down
3 changes: 0 additions & 3 deletions src/main/python/main/ayab/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def __init__(self, function: Callable[..., Any], *args: Any, **kwargs: Any):
self.args = args
self.kwargs = kwargs

def __del__(self) -> None:
self.wait()

def run(self) -> None:
try:
self.function(*self.args, **self.kwargs)
Expand Down

0 comments on commit 18405b7

Please sign in to comment.