Skip to content

Commit

Permalink
Fix: Global flock fallbacks (#1834)
Browse files Browse the repository at this point in the history
Use correct fallbacks for global flock file.

- Prevents crash if `XDG_RUNTIME_DIR` is not set.
- Default to `~/.cache` instead of `~/cache`.

Fix #1834

Thank you to Timothy Southwick (@NickNackGus) for this contribution.

---------

Co-authored-by: buhtz <c.buhtz@posteo.jp>
  • Loading branch information
NickNackGus and buhtz authored Aug 11, 2024
1 parent 675ea27 commit 6e2be6b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Version 1.5.3-dev (development of upcoming release)
* Breaking Change: Minimal Python version 3.9 required (#1731)
* Fix: Prevent duplicates in Exclude/Include list of Manage Profiles dialog
* Fix: Fix Qt segmentation fault when canceling out of unconfigured BiT (#1095) (Derek Veit @DerekVeit)
* Fix: Correct global flock fallbacks (#1834) (Timothy Southwick @NickNackGus)
* Refactor: Remove class OrderedSet

Version 1.5.2 (2024-08-06)
Expand Down
19 changes: 11 additions & 8 deletions common/flock.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self,

# Workaround for #1751. Remove after refactoring Snapshots.backup()
if disable:
return
return None

folder = Path(Path.cwd().root) / 'run' / 'lock'

Expand All @@ -84,23 +84,26 @@ def __init__(self,
self._file_path = folder / filename

if self._can_use_file(self._file_path):
return
return None

# Try user specific file lock
# e.g. /run/user/<UID>
self._file_path = Path(os.environ['XDG_RUNTIME_DIR']) / filename
self._file_path = Path(
os.environ.get('XDG_RUNTIME_DIR',
f'/run/user/{os.getuid()}')
) / filename

if self._can_use_file(self._file_path):
return
return None

# At last, try users cache dir.
self._file_path = Path(
os.environ.get('XDG_CACHE_HOME',
Path.home() / 'cache')
Path.home() / '.cache')
) / filename

if self._can_use_file(self._file_path):
return
return None

raise RuntimeError(
f'Can not establish global flock file {self._file_path}')
Expand Down Expand Up @@ -146,7 +149,7 @@ def __enter__(self):
# Workaround for #1751. Remove after refactoring Snapshots.backup()
# See __init__() for details
if self._file_path is None:
return
return None

self._log('Set')

Expand All @@ -162,7 +165,7 @@ def __exit__(self, exc_type, exc_value, exc_tb):
# Workaround for #1751. Remove after refactoring Snapshots.backup()
# See __init__() for details
if self._flock_handle is None:
return
return None

self._log('Release')
fcntl.fcntl(self._flock_handle, fcntl.LOCK_UN)
Expand Down

0 comments on commit 6e2be6b

Please sign in to comment.