Skip to content

Commit

Permalink
Global cache lock, more strict error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
isidentical committed Jun 1, 2020
1 parent 21e54c3 commit 6c917cb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion parso/_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def u(string):
FileNotFoundError = FileNotFoundError
except NameError:
# Python 2.7 (both IOError + OSError)
FileNotFoundError = EnvironmentError
FileNotFoundError = (IOError, OSError)


def utf8_repr(func):
Expand Down
17 changes: 12 additions & 5 deletions parso/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ def _get_cache_clear_lock(cache_path = None):
collection once a day (can be configured in _CACHE_CLEAR_THRESHOLD).
"""
cache_path = cache_path or _get_default_cache_path()

return FileIO(os.path.join(cache_path, "{}-cache-lock".format(_VERSION_TAG)))
return FileIO(os.path.join(cache_path, "PARSO-CACHE-LOCK"))


parser_cache = {}
Expand Down Expand Up @@ -226,7 +225,10 @@ def clear_inactive_cache(
file.stat().st_atime + _CACHED_FILE_MAXIMUM_SURVIVAL
<= time.time()
):
os.remove(file.path)
try:
os.remove(file.path)
except OSError: # silently ignore all failures
continue
else:
return True

Expand All @@ -238,8 +240,13 @@ def _remove_cache_and_update_lock(cache_path = None):
clear_lock_time is None # first time
or clear_lock_time + _CACHE_CLEAR_THRESHOLD <= time.time()
):
if clear_inactive_cache(cache_path = cache_path):
lock._touch()
if not lock._touch():
# First make sure that as few as possible other cleanup jobs also
# get started. There is still a race condition but it's probably
# not a big problem.
return False

clear_inactive_cache(cache_path = cache_path)

def _get_hashed_path(hashed_grammar, path, cache_path=None):
directory = _get_cache_directory_path(cache_path=cache_path)
Expand Down
8 changes: 6 additions & 2 deletions parso/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ def _touch(self):
try:
os.utime(self.path, None)
except FileNotFoundError:
file = open(self.path, 'a')
file.close()
try:
file = open(self.path, 'a')
file.close()
except OSError: # TODO Maybe log this?
return False
return True

def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self.path)
Expand Down

0 comments on commit 6c917cb

Please sign in to comment.