Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#34209 closed Bug (fixed)

FileBasedCache has_key is susceptible to race conditions

Reported by: Marti Raudsepp Owned by: Marti Raudsepp
Component: Core (Cache system) Version: 4.1
Severity: Normal Keywords: race, race-condition, FileBasedCache
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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.

Change History (8)

comment:1 by Marti Raudsepp, 2 years ago

Owner: changed from nobody to Marti Raudsepp
Status: newassigned

comment:2 by Marti Raudsepp, 2 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 2 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Thanks for the report.

comment:4 by Mariusz Felisiak, 2 years ago

Needs tests: set

comment:5 by Marti Raudsepp, 2 years ago

Added a test.

comment:6 by Mariusz Felisiak, 2 years ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 32268456:

Fixed #34209 -- Prevented FileBasedCache.has_key() crash caused by a race condition.

comment:8 by Marti Raudsepp, 2 years ago

This fix doesn't qualify for backporting?

Note: See TracTickets for help on using tickets.
Back to Top