Fix python3.8 race condition in task.close()

This commit is contained in:
allegroai 2021-05-10 20:33:30 +03:00
parent 3225715ae3
commit 0e0d86f6d8
2 changed files with 9 additions and 5 deletions

View File

@ -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):

View File

@ -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)