Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
19 changes: 12 additions & 7 deletions msal_extensions/cache_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ class CrossPlatLock(object):
"""
def __init__(self, lockfile_path):
self._lockpath = lockfile_path
self._fh = None
self._lock = portalocker.Lock(
lockfile_path,
mode='wb+',
# In posix systems, we HAVE to use LOCK_EX(exclusive lock) bitwise ORed
# with LOCK_NB(non-blocking) to avoid blocking on lock acquisition.
# More information here: https://docs.python.org/3/library/fcntl.html#fcntl.lockf
flags=portalocker.LOCK_EX | portalocker.LOCK_NB,
buffering=0)

def __enter__(self):
pid = os.getpid()

self._fh = open(self._lockpath, 'wb+', buffering=0)
portalocker.lock(self._fh, portalocker.LOCK_EX)
self._fh.write('{} {}'.format(pid, sys.argv[0]).encode('utf-8'))
file_handle = self._lock.__enter__()
file_handle.write('{} {}'.format(os.getpid(), sys.argv[0]).encode('utf-8'))
return file_handle

def __exit__(self, *args):
self._fh.close()
self._lock.__exit__(*args)
try:
# Attempt to delete the lockfile. In either of the failure cases enumerated below, it is
# likely that another process has raced this one and ended up clearing or locking the
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package_data={'': ['LICENSE']},
install_requires=[
'msal>=0.4.1,<2.0.0',
'portalocker~=1.0',
'portalocker~=1.6',
],
tests_require=['pytest'],
)