From 14108c4f239c81a2908dea7bcb17896f36d0af67 Mon Sep 17 00:00:00 2001 From: allegroai <> Date: Fri, 20 Aug 2021 00:33:37 +0300 Subject: [PATCH] Fix PY3.x fails calling SemLock._after_fork with forkserver context, forking while lock is acquired (https://github.com/allegroai/clearml-agent/issues/73) --- clearml/utilities/locks/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/clearml/utilities/locks/utils.py b/clearml/utilities/locks/utils.py index 20a88f16..6d390380 100644 --- a/clearml/utilities/locks/utils.py +++ b/clearml/utilities/locks/utils.py @@ -233,6 +233,18 @@ class RLock(Lock): def acquire(self, timeout=None, check_interval=None, fail_when_locked=None): if self._lock: + # cleanup bad python behaviour when forking while lock is acquired + # see Issue https://github.com/allegroai/clearml-agent/issues/73 + # and https://bugs.python.org/issue6721 + if self._pid != os.getpid(): + # noinspection PyBroadException + try: + if self._lock._semlock._count(): # noqa + # this should never happen unless python forgot calling _after_fork + self._lock._semlock._after_fork() # noqa + except BaseException: + pass + if not self._lock.acquire(block=timeout != 0, timeout=timeout): # We got a timeout... reraising raise exceptions.LockException()