Skip to content
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

fix: improve file locking mechanism with atomic creation #455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions qiniu/http/regions_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,19 @@
self._origin_file = origin_file

def __enter__(self):
if os.access(self.lock_file_path, os.R_OK | os.W_OK):
try:
# Atomic file creation
open_flags = os.O_EXCL | os.O_RDWR | os.O_CREAT
fd = os.open(self.lock_file_path, open_flags)
os.close(fd)
except FileExistsError:
raise FileAlreadyLocked('File {0} already locked'.format(self._origin_file))

Check warning on line 151 in qiniu/http/regions_provider.py

View check run for this annotation

qiniu-x / pylint

qiniu/http/regions_provider.py#L151

Consider explicitly re-raising using 'except FileExistsError as exc' and 'raise FileAlreadyLocked('File {0} already locked'.format(self._origin_file)) from exc' (raise-missing-from)

Check warning on line 151 in qiniu/http/regions_provider.py

View check run for this annotation

qiniu-x / pylint

qiniu/http/regions_provider.py#L151

Formatting a regular string which could be an f-string (consider-using-f-string)
with open(self.lock_file_path, 'w'):
pass

def __exit__(self, exc_type, exc_val, exc_tb):
os.remove(self.lock_file_path)
try:
os.remove(self.lock_file_path)
except FileNotFoundError:
pass # Lock file might have been removed

@property
def lock_file_path(self):
Expand Down