Runtime: fix PLC not terminating when being Repaired (i.e purged). wxPython4
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Fri, 21 Oct 2022 18:33:06 +0200
branchwxPython4
changeset 3642 cd3d15e8ef42
parent 3641 d8dc29dfc344
child 3643 c6068b674b30
Runtime: fix PLC not terminating when being Repaired (i.e purged).
Beremiz_service.py
runtime/PLCObject.py
runtime/Worker.py
--- a/Beremiz_service.py	Fri Oct 21 17:52:45 2022 +0200
+++ b/Beremiz_service.py	Fri Oct 21 18:33:06 2022 +0200
@@ -587,7 +587,7 @@
 
     # interleaved worker copes with wxreactor by delegating all asynchronous
     # calls to wx's mainloop
-    runtime.MainWorker.interleave(waker_func, FirstWorkerJob)
+    runtime.MainWorker.interleave(waker_func, reactor.stop, FirstWorkerJob)
 
     try:
         reactor.run(installSignalHandlers=False)
--- a/runtime/PLCObject.py	Fri Oct 21 17:52:45 2022 +0200
+++ b/runtime/PLCObject.py	Fri Oct 21 18:33:06 2022 +0200
@@ -623,7 +623,7 @@
 
     def RepairPLC(self):
         self.PurgePLC()
-        MainWorker.quit()
+        MainWorker.finish()
 
     @RunInMain
     def PurgePLC(self):
--- a/runtime/Worker.py	Fri Oct 21 17:52:45 2022 +0200
+++ b/runtime/Worker.py	Fri Oct 21 18:33:06 2022 +0200
@@ -52,6 +52,8 @@
         self.free = Condition(self.mutex)
         self.job = None
         self.enabled = False
+        self.stopper = None
+        self.own_thread = None
 
     def reraise(self, job):
         """
@@ -87,13 +89,14 @@
 
         self.mutex.release()
 
-    def interleave(self, waker, *args, **kwargs):
+    def interleave(self, waker, stopper, *args, **kwargs):
         """
         as for twisted reactor's interleave, it passes all jobs to waker func
         additionaly, it creates a new thread to wait for new job.
         """
         self.feed = Condition(self.mutex)
         self._threadID = _thread.get_ident()
+        self.stopper = stopper
 
         def wakerfeedingloop():
             self.mutex.acquire()
@@ -119,6 +122,8 @@
                         self.feed.notify()
                         self.done.notify()
                     self.mutex.release()
+                if self._finish:
+                    break
                 waker(job_todo)
                 self.feed.wait()
 
@@ -189,3 +194,9 @@
         self.todo.notify()
         self.done.notify()
         self.mutex.release()
+
+    def finish(self):
+        if self.own_thread is None:
+            self.quit()
+        if self.stopper is not None:
+            self.stopper()