From 00b25b8ed99abc8846e628fff676ce9420a37f33 Mon Sep 17 00:00:00 2001 From: Blanca Fuentes Date: Mon, 16 Dec 2024 12:26:51 +0100 Subject: [PATCH] Fix autodetect tests --- reframe/frontend/executors/__init__.py | 40 +++++++++++++++++++------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/reframe/frontend/executors/__init__.py b/reframe/frontend/executors/__init__.py index e781644ef..77a6ef325 100644 --- a/reframe/frontend/executors/__init__.py +++ b/reframe/frontend/executors/__init__.py @@ -29,6 +29,7 @@ StatisticsError, TaskExit) from reframe.core.schedulers.local import LocalJobScheduler +from reframe.frontend.executors import all_tasks from reframe.frontend.printer import PrettyPrinter ABORT_REASONS = (AssertionError, FailureLimitError, @@ -813,16 +814,33 @@ def execute(self, testcases): def asyncio_run(coro): - loop = asyncio.get_event_loop() - - if loop.is_running(): + try: + loop = asyncio.get_event_loop() + for task in all_tasks(loop): + if isinstance(task, asyncio.tasks.Task): + try: + task.cancel() + except RuntimeError: + pass + if loop.is_closed(): + loop = asyncio.new_event_loop() + watcher = asyncio.get_child_watcher() + if isinstance(watcher, asyncio.SafeChildWatcher): + # Detach the watcher from the current loop to avoid issues + watcher.close() + watcher.attach_loop(None) + asyncio.set_event_loop(loop) + if isinstance(watcher, asyncio.SafeChildWatcher): + # Reattach the watcher to the new loop + watcher.attach_loop(loop) + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + + try: loop.run_until_complete(coro) + except (Exception, KeyboardInterrupt): + for task in all_tasks(loop): + if isinstance(task, asyncio.tasks.Task): + task.cancel() loop.close() - return - else: - try: - loop.run_until_complete(coro) - loop.close() - return - finally: - loop.close()