From 0e0d86f6d8b087a6a43d18c835607643d8e8d116 Mon Sep 17 00:00:00 2001 From: allegroai <> Date: Mon, 10 May 2021 20:33:30 +0300 Subject: [PATCH] Fix python3.8 race condition in task.close() --- clearml/task.py | 4 ++-- clearml/utilities/process/mp.py | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/clearml/task.py b/clearml/task.py index 7d3bde95..16d0f240 100644 --- a/clearml/task.py +++ b/clearml/task.py @@ -1396,7 +1396,7 @@ class Task(_Task): self.__register_at_exit(None) if not is_sub_process: # make sure we enable multiple Task.init callas with reporting sub-processes - BackgroundMonitor.clear_main_process() + BackgroundMonitor.clear_main_process(self) # noinspection PyProtectedMember Logger._remove_std_logger() @@ -3050,7 +3050,7 @@ class Task(_Task): # make sure no one will re-enter the shutdown method self._at_exit_called = True - BackgroundMonitor.wait_for_sub_process() + BackgroundMonitor.wait_for_sub_process(self) @classmethod def __register_at_exit(cls, exit_callback, only_remove_signal_and_exception_hooks=False): diff --git a/clearml/utilities/process/mp.py b/clearml/utilities/process/mp.py index 21d08ec3..c4759154 100644 --- a/clearml/utilities/process/mp.py +++ b/clearml/utilities/process/mp.py @@ -414,8 +414,8 @@ class BackgroundMonitor(object): return bool(cls._main_process) @classmethod - def clear_main_process(cls): - cls.wait_for_sub_process() + def clear_main_process(cls, task): + cls.wait_for_sub_process(task) BackgroundMonitor._main_process = None BackgroundMonitor._parent_pid = None BackgroundMonitor._sub_process_started = None @@ -423,9 +423,13 @@ class BackgroundMonitor(object): SingletonThreadPool.clear() @classmethod - def wait_for_sub_process(cls, timeout=None): + def wait_for_sub_process(cls, task, timeout=None): if not cls.is_subprocess_enabled(): return + + for d in BackgroundMonitor._instances.get(id(task.id), []): + d.stop() + tic = time() while cls.is_subprocess_alive() and (not timeout or time()-tic < timeout): sleep(0.03)