summaryrefslogtreecommitdiff
path: root/django/core/cache
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2016-05-26 08:36:00 -0700
committerTim Graham <timograham@gmail.com>2016-05-31 12:03:27 -0400
commit359be1c8702ede41e7fe823ed13350795ba96a61 (patch)
tree064509b4b6e7537d24890b0049ef9ca4e171387a /django/core/cache
parente3877c53edb33271b0f31d20e60a924848692026 (diff)
Fixed #26691 -- Removed checking for a file's existence before deleting.
File operations always raise a ENOENT error when a file doesn't exist. Checking the file exists before the operation adds a race condition condition where the file could be removed between operations. As the operation already raises an error on a missing file, avoid this race and avoid checking the file exists twice. Instead only check a file exists by catching the ENOENT error.
Diffstat (limited to 'django/core/cache')
-rw-r--r--django/core/cache/backends/filebased.py15
1 files changed, 7 insertions, 8 deletions
diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py
index 2a8b24ddfc..bff912d85d 100644
--- a/django/core/cache/backends/filebased.py
+++ b/django/core/cache/backends/filebased.py
@@ -35,14 +35,13 @@ class FileBasedCache(BaseCache):
def get(self, key, default=None, version=None):
fname = self._key_to_file(key, version)
- if os.path.exists(fname):
- try:
- with io.open(fname, 'rb') as f:
- if not self._is_expired(f):
- return pickle.loads(zlib.decompress(f.read()))
- except IOError as e:
- if e.errno == errno.ENOENT:
- pass # Cache file was removed after the exists check
+ try:
+ with io.open(fname, 'rb') as f:
+ if not self._is_expired(f):
+ return pickle.loads(zlib.decompress(f.read()))
+ except IOError as e:
+ if e.errno == errno.ENOENT:
+ pass # Cache file doesn't exist.
return default
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):