Skip to content

Commit

Permalink
Merge pull request #5 from claydugo/test_first_fix_later
Browse files Browse the repository at this point in the history
Do not use thread-local lock behavior
  • Loading branch information
hmaarrfk authored Apr 18, 2023
2 parents 8d137ef + 23d0025 commit fdcf78f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# Uncomment below once tests for filelock 3.11 are fixed
# python-version: ['3.7', '3.9', '3.11']
python-version: ['3.9']
python-version: ['3.7', '3.9', '3.11']
fail-fast: false

steps:
- uses: actions/checkout@v1
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.6 (2023/04/18)

- Fix compatibility with filelock 3.12.0.
- multiuserfilelock (all version) are incompatible with filelock 3.11.0.

## 0.0.5 (2021/11/23)

- Ensure windows tests pass.
Expand Down
8 changes: 7 additions & 1 deletion multiuserfilelock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import shutil
import tempfile
from pathlib import Path
import filelock
from packaging.version import Version

# Creating the locks directly in the /tmp dir on linux causes
# many problems since the `/tmp` dir has the sticky bit enabled.
Expand Down Expand Up @@ -37,10 +39,14 @@ def __init__(self, *args, user=None, group=None, chmod=0o666, **kwargs):
self._user = user
self._group = group
self._chmod = chmod

if Version(filelock.__version__) >= Version('3.12.0'):
kwargs['thread_local'] = False

# Will create a ._lock_file object
# but will not create the files on the file system
super().__init__(*args, **kwargs)
self._lock_file_path = Path(self._lock_file)
self._lock_file_path = Path(self.lock_file)
parent = self._lock_file_path.parent
# Even though the "other write" permissions are enabled
# it seems that the operating systems disables that for the /tmp dir
Expand Down
37 changes: 37 additions & 0 deletions multiuserfilelock/tests/test_acquire.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from multiuserfilelock import MultiUserFileLock, Timeout
from threading import Thread
from queue import Queue
import pytest


Expand Down Expand Up @@ -26,3 +28,38 @@ def test_double_acquire(tmp_path):

lock1.release()
lock2.acquire()


def test_release_lock_from_different_thread(tmp_path):
results = Queue()

def thread_lock_acquire(filename):
lock = MultiUserFileLock(filename)
lock.acquire()
assert lock.is_locked
results.put(lock)

def thread_lock_release(lock):
lock.release()

lock_filename = tmp_path / 'test.lock'
lock_thread = Thread(target=thread_lock_acquire, args=(lock_filename,))

lock_thread.start()
lock_thread.join(timeout=1)
assert not lock_thread.is_alive()
lock = results.get(timeout=1)
assert lock.is_locked

lock_thread = Thread(target=thread_lock_release, args=(lock,))
lock_thread.start()
lock_thread.join(timeout=1)
assert not lock_thread.is_alive()
assert not lock.is_locked

lock_thread2 = Thread(target=thread_lock_acquire, args=(lock_filename,))
lock_thread2.start()
lock_thread2.join(timeout=1)
assert not lock_thread2.is_alive()
lock2 = results.get(timeout=1)
assert lock2.is_locked
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
filelock
filelock !=3.11.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
license='BSD',
python_requires='>=3.7',
install_requires=[
'filelock',
'filelock!=3.11.0',
],
packages=find_packages(),
cmdclass=versioneer.get_cmdclass(),
Expand Down

0 comments on commit fdcf78f

Please sign in to comment.