← All challenges
mediumdjango/django · v4.2

FileBasedCache has_key is susceptible to race conditions

Try this bug with AI

Fix it in the original repo with an AI agent and get feedback on how you worked.

2 free rounds each week

Round mode
  • Time limit60 min
  • Token budgetup to 1M
  • Worth up to210 XP
Sign up or sign in to start

Read the sample report

From a simulated Django round

You changed code before reproducing the bug.

Run the failing test before the first edit, so you know the failure you are fixing and can tell when it is gone.

Problem statement

Description

	(last modified by Marti Raudsepp)

I received the exception from Django's cache framework:
FileNotFoundError: [Errno 2] No such file or directory: '/app/var/cache/d729e4cf4ba88cba5a0f48e0396ec48a.djcache'
[...]
File "django/core/cache/backends/base.py", line 229, in get_or_set
self.add(key, default, timeout=timeout, version=version)
File "django/core/cache/backends/filebased.py", line 26, in add
if self.has_key(key, version):
File "django/core/cache/backends/filebased.py", line 94, in has_key
with open(fname, "rb") as f:
The code is:
def has_key(self, key, version=None):
fname = self._key_to_file(key, version)
if os.path.exists(fname):
with open(fname, "rb") as f:
return not self._is_expired(f)
return False
Between the exists() check and open(), it's possible for the file to be deleted. In fact, the _is_expired() method itself deletes the file if it finds it to be expired. So if many threads race to read an expired cache at once, it's not that unlikely to hit this window.

The environment starts at commit 1d0fa848e084 (django/django 4.2), dependencies installed and tests runnable from the first minute. You are graded by hidden tests taken from the fix that was actually merged upstream.