Skip to content

Commit

Permalink
Fixed case where code would try to unlock a file handle externally ma…
Browse files Browse the repository at this point in the history
…naged
  • Loading branch information
jmfernandez committed Aug 6, 2024
1 parent 37d9987 commit 2cb90d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion RWFileLock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)

# https://www.python.org/dev/peps/pep-0396/
__version__ = version = "0.2.0"
__version__ = version = "0.2.1"
__author__ = "José M. Fernández <https://orcid.org/0000-0002-4806-5140>"
__copyright__ = "© 2020-2024 Barcelona Supercomputing Center (BSC), ES"
__license__ = "LGPLv2"
Expand Down
17 changes: 11 additions & 6 deletions RWFileLock/rw_file_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ def w_lock(self) -> "None":
fcntl.lockf(self.lock_fd, (fcntl.LOCK_EX | fcntl.LOCK_NB))
self.isLocked = True
self.isShareLock = False
except IOError:
raise LockError("Already locked by others")
except IOError as ioe:
raise LockError("Already locked by others") from ioe
except OSError as ose:
raise LockError("Read only file descriptor?") from ose

def w_blocking_lock(self) -> "None":
if self.isLocked and not self.isShareLock:
Expand All @@ -109,15 +111,18 @@ def w_blocking_lock(self) -> "None":
fcntl.lockf(self.lock_fd, fcntl.LOCK_EX)
self.isLocked = True
self.isShareLock = False
except OSError:
raise LockError("Read only file descriptor?")
except OSError as ose:
raise LockError("Read only file descriptor?") from ose

def unlock(self) -> "None":
if self.isLocked:
try:
fcntl.lockf(self.lock_fd, fcntl.LOCK_UN)
except IOError:
raise LockError("Already locked by others")
except OSError as ose:
if self.should_close:
raise LockError("Unexpected unlocking error") from ose
except IOError as ioe:
raise LockError("Unexpected unlocking error") from ioe
finally:
self.isLocked = False
else:
Expand Down

0 comments on commit 2cb90d1

Please sign in to comment.