Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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: 14 additions & 0 deletions msal_extensions/cache_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import sys
import errno
import time

import portalocker
from distutils.version import LooseVersion

Expand All @@ -25,7 +27,19 @@ def __init__(self, lockfile_path):
flags=portalocker.LOCK_EX | portalocker.LOCK_NB,
**open_kwargs)

def try_to_create_lock_file(self):
retries_no = 20
for i in range(retries_no):
if os.path.isfile(self._lockpath):
time.sleep(0.25)
else:
open(self._lockpath, 'wb+')
return True
return False

def __enter__(self):
if not self.try_to_create_lock_file():
print("Failed to create lock file")
file_handle = self._lock.__enter__()
file_handle.write('{} {}'.format(os.getpid(), sys.argv[0]).encode('utf-8'))
return file_handle
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache_lock_file_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_lock_for_high_workload(temp_location):


def test_lock_for_timeout(temp_location):
num_of_processes = 10
num_of_processes = 20
sleep_interval = 1
_run_multiple_processes(num_of_processes, temp_location, sleep_interval)
count = _validate_result_in_cache(temp_location)
Expand Down