From 0ecc034575c544596853e37112fa3772f8363f97 Mon Sep 17 00:00:00 2001
From: Jonathan Karlsen <JONAK@equinor.com>
Date: Mon, 6 Jan 2025 14:05:22 +0100
Subject: [PATCH] Make sure event loop is running before running coroutines in
 executor

---
 src/ert/ensemble_evaluator/evaluator.py |  3 ++-
 src/ert/gui/simulation/run_dialog.py    |  4 ++--
 src/ert/run_models/base_run_model.py    | 14 ++++++++------
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/src/ert/ensemble_evaluator/evaluator.py b/src/ert/ensemble_evaluator/evaluator.py
index 3696660524f..5a5a093bd60 100644
--- a/src/ert/ensemble_evaluator/evaluator.py
+++ b/src/ert/ensemble_evaluator/evaluator.py
@@ -334,7 +334,8 @@ def _signal_cancel(self) -> None:
         if self._ensemble.cancellable:
             logger.debug("Cancelling current ensemble")
             assert self._loop is not None
-            self._loop.run_in_executor(None, self._ensemble.cancel)
+            if self._loop.is_running():
+                self._loop.run_in_executor(None, self._ensemble.cancel)
         else:
             logger.debug("Stopping current ensemble")
             self.stop()
diff --git a/src/ert/gui/simulation/run_dialog.py b/src/ert/gui/simulation/run_dialog.py
index dee4e941a0b..9c1b758ba26 100644
--- a/src/ert/gui/simulation/run_dialog.py
+++ b/src/ert/gui/simulation/run_dialog.py
@@ -363,7 +363,7 @@ def run() -> None:
                 restart=restart,
             )
 
-        simulation_thread = ErtThread(
+        self.simulation_thread = ErtThread(
             name="ert_gui_simulation_thread", target=run, daemon=True
         )
 
@@ -381,7 +381,7 @@ def run() -> None:
         self._ticker.start(self._RUN_TIME_POLL_RATE)
 
         self._worker_thread.start()
-        simulation_thread.start()
+        self.simulation_thread.start()
         self._notifier.set_is_simulation_running(True)
 
     def killJobs(self) -> QMessageBox.StandardButton:
diff --git a/src/ert/run_models/base_run_model.py b/src/ert/run_models/base_run_model.py
index a9f00f059f6..a24f68a2244 100644
--- a/src/ert/run_models/base_run_model.py
+++ b/src/ert/run_models/base_run_model.py
@@ -503,12 +503,14 @@ async def run_monitor(
                         EESnapshotUpdate,
                     }:
                         event = cast(EESnapshot | EESnapshotUpdate, event)
-                        await asyncio.get_running_loop().run_in_executor(
-                            None,
-                            self.send_snapshot_event,
-                            event,
-                            iteration,
-                        )
+                        if asyncio.get_running_loop().is_running():
+                            await asyncio.get_running_loop().run_in_executor(
+                                None,
+                                self.send_snapshot_event,
+                                event,
+                                iteration,
+                            )
+
                         if event.snapshot.get(STATUS) in {
                             ENSEMBLE_STATE_STOPPED,
                             ENSEMBLE_STATE_FAILED,