Skip to content

Commit 9ffc5b7

Browse files
authored
Merge pull request #100 from AzureAD/skip-read-lock
Skip read lock to improve concurrency
2 parents 4701ef4 + 2c9690e commit 9ffc5b7

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

msal_extensions/token_cache.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,21 @@ def modify(self, credential_type, old_entry, new_key_value_pairs=None):
7373
self._last_sync = time.time()
7474

7575
def find(self, credential_type, **kwargs): # pylint: disable=arguments-differ
76-
with CrossPlatLock(self._lock_location):
77-
self._reload_if_necessary()
78-
return super(PersistedTokenCache, self).find(credential_type, **kwargs)
76+
# Use optimistic locking rather than CrossPlatLock(self._lock_location)
77+
retry = 3
78+
for attempt in range(1, retry + 1):
79+
try:
80+
self._reload_if_necessary()
81+
except Exception: # pylint: disable=broad-except
82+
# Presumably other processes are writing the file, causing dirty read
83+
if attempt < retry:
84+
logger.debug("Unable to load token cache file in No. %d attempt", attempt)
85+
time.sleep(0.5)
86+
else:
87+
raise # End of retry. Re-raise the exception as-is.
88+
else: # If reload encountered no error, the data is considered intact
89+
return super(PersistedTokenCache, self).find(credential_type, **kwargs)
90+
return [] # Not really reachable here. Just to keep pylint happy.
7991

8092

8193
class FileTokenCache(PersistedTokenCache):

0 commit comments

Comments
 (0)