From ab1b7a2605d86b848ee9748effe88c8fd2c75f48 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Fri, 19 Apr 2024 09:55:14 +0200
Subject: [PATCH] Save/Backup: Add mutual exclusion on save methode and block
 backup signal.

---
 src/View/MainWindow.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/View/MainWindow.py b/src/View/MainWindow.py
index 16076aa6..f4d8bad7 100644
--- a/src/View/MainWindow.py
+++ b/src/View/MainWindow.py
@@ -33,7 +33,7 @@ from PyQt5.QtGui import (
 
 from PyQt5.QtCore import (
     Qt, QTranslator, QEvent, QUrl, QTimer,
-    QCoreApplication,
+    QCoreApplication, QMutex,
 )
 from PyQt5.QtWidgets import (
     QMainWindow, QApplication, QAction,
@@ -403,6 +403,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         return ts
 
     def setup_timer_backup(self):
+        self._save_mutex = QMutex()
         self._backup_timer = QTimer(self)
 
         ts = self.get_config_backup_freq_to_sec()
@@ -565,6 +566,9 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         if self._study.is_saved:
             return
 
+        self._backup_timer.blockSignals(True)
+        self._save_mutex.lock()
+
         sql_request_count = self._study.sql_save_request_count()
         progress = QProgressDialog(
             "Saving...", None,
@@ -588,6 +592,9 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
 
         self.conf.set_last_study(self._study.filename)
 
+        self._save_mutex.unlock()
+        self._backup_timer.blockSignals(False)
+
     def save_as_study(self):
         """Save current study as new file
 
@@ -606,6 +613,9 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         if file_name == "":
             return
 
+        self._backup_timer.blockSignals(True)
+        self._save_mutex.lock()
+
         if file_name.rsplit(".", 1)[-1] == "pamhyr":
             logger.debug(
                 "Pamhyr extention is present : " +
@@ -642,6 +652,9 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
 
         self.conf.set_last_study(self._study.filename)
 
+        self._save_mutex.unlock()
+        self._backup_timer.blockSignals(False)
+
     def _backup(self):
         logger.debug("Backup signal...")
         if not self.conf.backup_enable:
@@ -653,6 +666,8 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
         if self._study.is_saved:
             return
 
+        self._save_mutex.lock()
+
         old = self._study.filename
         file_name = ""
         if old == "" or old is None:
@@ -693,6 +708,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
             logger_exception(e)
 
         self._study.filename = old
+        self._save_mutex.unlock()
 
     ##################
     # MSG AND DIALOG #
-- 
GitLab