mirror of https://github.com/django/django.git
Fixed #34209 -- Prevented FileBasedCache.has_key() crash caused by a race condition.
This commit is contained in:
parent
1d0fa848e0
commit
32268456d6
|
@ -90,9 +90,10 @@ class FileBasedCache(BaseCache):
|
||||||
|
|
||||||
def has_key(self, key, version=None):
|
def has_key(self, key, version=None):
|
||||||
fname = self._key_to_file(key, version)
|
fname = self._key_to_file(key, version)
|
||||||
if os.path.exists(fname):
|
try:
|
||||||
with open(fname, "rb") as f:
|
with open(fname, "rb") as f:
|
||||||
return not self._is_expired(f)
|
return not self._is_expired(f)
|
||||||
|
except FileNotFoundError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _cull(self):
|
def _cull(self):
|
||||||
|
|
|
@ -1762,6 +1762,12 @@ class FileBasedCacheTests(BaseCacheTests, TestCase):
|
||||||
with open(cache_file, "rb") as fh:
|
with open(cache_file, "rb") as fh:
|
||||||
self.assertIs(cache._is_expired(fh), True)
|
self.assertIs(cache._is_expired(fh), True)
|
||||||
|
|
||||||
|
def test_has_key_race_handling(self):
|
||||||
|
self.assertIs(cache.add("key", "value"), True)
|
||||||
|
with mock.patch("builtins.open", side_effect=FileNotFoundError) as mocked_open:
|
||||||
|
self.assertIs(cache.has_key("key"), False)
|
||||||
|
mocked_open.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(RedisCache_params, "Redis backend not configured")
|
@unittest.skipUnless(RedisCache_params, "Redis backend not configured")
|
||||||
@override_settings(
|
@override_settings(
|
||||||
|
|
Loading…
Reference in New Issue