From 623315665cb86abe9bd64069e9cebb522af70d07 Mon Sep 17 00:00:00 2001
From: Kien Le <kienle@passcal.nmt.edu>
Date: Thu, 23 Mar 2023 10:58:17 -0600
Subject: [PATCH] Clean up waveform thread when program exits

---
 sohstationviewer/view/main_window.py | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/sohstationviewer/view/main_window.py b/sohstationviewer/view/main_window.py
index 33cd7c286..5db6a731b 100755
--- a/sohstationviewer/view/main_window.py
+++ b/sohstationviewer/view/main_window.py
@@ -31,7 +31,6 @@ from sohstationviewer.view.plotting.waveform_dialog import WaveformDialog
 from sohstationviewer.view.search_message.search_message_dialog import (
     SearchMessageDialog
 )
-from sohstationviewer.view.plotting.state_of_health_widget import SOHWidget
 from sohstationviewer.view.help_view import HelpBrowser
 from sohstationviewer.view.ui.main_ui import UIMainWindow
 from sohstationviewer.view.util.enums import LogType
@@ -142,7 +141,7 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
         """
         waveform_dlg: widget to display waveform channels' plotting
         """
-        self.waveform_dlg: SOHWidget = WaveformDialog(self)
+        self.waveform_dlg: WaveformDialog = WaveformDialog(self)
         """
         tps_dlg: dialog to display time-power-squared of waveform channels
         """
@@ -854,8 +853,7 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
 
     def closeEvent(self, event: QtGui.QCloseEvent) -> None:
         """
-        Cleans up when the user exits the program. Currently only clean up
-        running data loaders.
+        Cleans up when the user exits the program.
 
         :param event: parameter of method being overridden
         """
@@ -867,6 +865,19 @@ class MainWindow(QtWidgets.QMainWindow, UIMainWindow):
             self.data_loader.thread.quit()
             self.data_loader.thread.wait()
 
+        # If we don't explicitly clean up the running processing threads,
+        # there is a high chance that they will attempt to access temporary
+        # files that have already been cleaned up. While this should not be a
+        # problem, it is still a bad idea to touch the file system when you are
+        # not supposed to.
+        if self.is_plotting_waveform:
+            self.waveform_dlg.plotting_widget.request_stop()
+            self.waveform_dlg.plotting_widget.thread_pool.waitForDone()
+
+        if self.is_plotting_tps:
+            self.tps_dlg.plotting_widget.request_stop()
+            self.tps_dlg.plotting_widget.thread_pool.waitForDone()
+
         # close all remaining windows
         for window in QtWidgets.QApplication.topLevelWidgets():
             window.close()
-- 
GitLab