Make lock files inheritable on Python >= 3.4

This commit is contained in:
Matt 2021-03-04 11:41:51 -06:00
parent 8b151dbb98
commit d5f24888c2
1 changed files with 6 additions and 0 deletions

View File

@ -314,6 +314,12 @@ def lock_file(filename):
file = open(filename,'w')
locks.append(file)
try:
# https://docs.python.org/3/library/os.html#inheritance-of-file-descriptors
# "Since Python 3.4, file descriptors created by Python are non-inheritable
# by default." Therefore, we must explicitly flag the file as inheritable,
# otherwise it will get discarded upon calling os.execvp() and the lock
# will be lost.
"set_inheritable" in dir(os) and os.set_inheritable(file.fileno(), True)
return fcntl.lockf(file.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
except IOError:
return -1