Read-only locks should close fd before opening for write. (#1906)

- Fixes bad file descriptor error in lock acquire, #1904
- Fix bug introduced in previous PR #1857
- Backported fix from soon-to-be merged fine-grained DB locking branch.
This commit is contained in:
Todd Gamblin
2016-10-04 15:36:37 -07:00
committed by GitHub
parent 9daafc32f1
commit bff1656a1a
2 changed files with 37 additions and 0 deletions

View File

@@ -69,6 +69,14 @@ def _lock(self, op, timeout):
start_time = time.time()
while (time.time() - start_time) < timeout:
try:
# If this is already open read-only and we want to
# upgrade to an exclusive write lock, close first.
if self._fd is not None:
flags = fcntl.fcntl(self._fd, fcntl.F_GETFL)
if op == fcntl.LOCK_EX and flags | os.O_RDONLY:
os.close(self._fd)
self._fd = None
if self._fd is None:
mode = os.O_RDWR if op == fcntl.LOCK_EX else os.O_RDONLY
self._fd = os.open(self._file_path, mode)