-
-
Notifications
You must be signed in to change notification settings - Fork 10
v0.22.1 #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v0.22.1 #339
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.22.0 | ||
| 0.22.1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Monkey patch Huey's filelock implementation to add Windows support. | ||
| import os | ||
| import sys | ||
|
|
||
|
|
||
| class FileLock: | ||
| """Operating system agnostic file lock implementation using os.open and os.close.""" | ||
|
|
||
| def __init__(self, filename): | ||
| self.filename = filename | ||
| self.fd = None | ||
|
|
||
| dirname = os.path.dirname(filename) | ||
| if not os.path.exists(dirname): | ||
| os.makedirs(dirname) | ||
| elif os.path.exists(self.filename): | ||
| os.unlink(self.filename) | ||
|
|
||
| def acquire(self): | ||
| flags = os.O_CREAT | os.O_TRUNC | os.O_RDWR | ||
| self.fd = os.open(self.filename, flags) | ||
|
Comment on lines
+19
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Current FileLock.acquire implementation does not provide actual mutual exclusion across processes. Because the file is opened with |
||
|
|
||
| def release(self): | ||
| if self.fd is not None: | ||
| fd, self.fd = self.fd, None | ||
| os.close(fd) | ||
|
|
||
| def __enter__(self): | ||
| self.acquire() | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| self.release() | ||
|
|
||
|
|
||
| if sys.platform == "win32": | ||
| import huey.utils | ||
|
|
||
| huey.utils.FileLock = FileLock | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Unconditionally deleting an existing lock file in init can interfere with other processes and may raise errors for empty dirnames.
Two issues here:
Deleting
self.filenamein__init__can remove an active lock held by another process and break locking semantics. A lock implementation should not unconditionally delete a potentially valid lock file without verifying ownership.If
filenamehas no directory (e.g."lockfile"),os.path.dirname(filename)is'', soos.makedirs(dirname)will be called with an empty string and fail. Guard against this with something likeif dirname and not os.path.exists(dirname):and reconsider whether the eager unlink is needed here at all.