Fix PY3.x fails calling SemLock._after_fork with forkserver context, forking while lock is acquired (https://github.com/allegroai/clearml-agent/issues/73)

This commit is contained in:
allegroai 2021-08-20 00:33:37 +03:00
parent e47f570ce5
commit 14108c4f23

View File

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