Minimal reproducible example:
import os
from filelock import Timeout, FileLock
file_path = "high_ground.txt"
lock_path = "high_ground.txt.lock"
lock1 = FileLock(lock_path)
with lock1:
lock2 = FileLock(lock_path)
with lock2:
print("I have the high ground!")
This is dangerous, and even when we just create one filelock, we don't know if any other code already created the filelock in the same process and we are within the filelock.
A possible solution would be to check and get existing fd on a file in UnixFileLock._acquire function. In Linux, this is easy because Python fd is just integer, and all fds are checkable in /proc/{process_id}/fd/ .
cc @gaborbernat