Opened 3 years ago

Closed 3 years ago

#28500 closed Bug (fixed)

"EOFError: Ran out of input" from Django file cache when file is empty

Reported by: Justin Crown Owned by: caleb logan
Component: Core (Cache system) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using Django file cache, if a cache file ends up empty for some reason (yes, this actually happened in the wild), an EOFError is thrown.

Steps to reproduce:

Cache must be set to django.core.cache.backends.filebased.FileBasedCache.

>>> from django.core.cache import cache
>>> cache.set('testing' 'test')

In a shell

> cache_dir/file_that_got_created.djcache

Back in the REPL

>>> cache.get('testing')
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/django/core/cache/backends/filebased.py", line 40, in get
    if not self._is_expired(f):
  File "/usr/local/lib/python3.6/site-packages/django/core/cache/backends/filebased.py", line 137, in _is_expired
    exp = pickle.load(f)
EOFError: Ran out of input

As to how these files would end up empty to begin with, I would hazard to guess that it is caused by a rare race condition in the file moving process:

https://github.com/django/django/blob/master/django/core/files/move.py#L59

Whereby if this process is interrupted by a system shutdown, the file is created in the appropriate location but is empty.

Desired behavior would be that in this situation, an exception would not be thrown. Rather, None (or default) would be returned.

I am planning on working on a patch towards this, but wanted to log the ticket first.

Change History (3)

comment:1 Changed 3 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 3 years ago by caleb logan

Owner: changed from nobody to caleb logan
Status: newassigned

comment:3 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 68f0e8d8:

Fixed #28500 -- Fixed crash in FileBasedCache._is_expired() if the cache file is empty.

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