[Django] #21602: FileSystemStorage._save() Should Save to a Temporary Filename and Rename to Attempt to be Atomic

20 views
Skip to first unread message

Django

unread,
Dec 12, 2013, 9:25:23 PM12/12/13
to django-...@googlegroups.com
#21602: FileSystemStorage._save() Should Save to a Temporary Filename and Rename to
Attempt to be Atomic
--------------------------------------+--------------------
Reporter: kevinastone | Owner: nobody
Type: Uncategorized | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
When using FileSystemStorage based backend with say, staticfiles, creates
a race condition where it's writing the intended destination file which
can lead to race conditions where your web server can serve incomplete
assets while they're being written.

Another case happens with post-processing since CachedStaticFilesStorage
actually deletes the hash-named file before saving over it.

These issue should be circumventing by directing the writing to a
temporary file name and then atomically renaming it to the intended
destination. Even better, FileSystemStorage._save() has to site in a
While-loop because of race conditions in get_available_name(). Instead,
it could defer calling get_available_name until it's prepared to rename
the file to its destination.

I'm working on implementing these changes in my own sub-class of
FileSystemStorage (and CachedStaticFilesStorage to avoid the delete()
call) and would be apply to submit them as a patch if there's interest.

--
Ticket URL: <https://code.djangoproject.com/ticket/21602>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 20, 2014, 2:01:05 PM3/20/14
to django-...@googlegroups.com
#21602: FileSystemStorage._save() Should Save to a Temporary Filename and Rename to
Attempt to be Atomic
--------------------------------------+------------------------------------

Reporter: kevinastone | Owner: nobody
Type: Uncategorized | Status: new
Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Alex):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

Agreed that atomic rename should be used.

--
Ticket URL: <https://code.djangoproject.com/ticket/21602#comment:1>

Django

unread,
Mar 25, 2014, 7:53:46 AM3/25/14
to django-...@googlegroups.com
#21602: FileSystemStorage._save() Should Save to a Temporary Filename and Rename to
Attempt to be Atomic
--------------------------------------+------------------------------------
Reporter: kevinastone | Owner: nobody
Type: Cleanup/optimization | Status: new

Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timo):

* type: Uncategorized => Cleanup/optimization


--
Ticket URL: <https://code.djangoproject.com/ticket/21602#comment:2>

Django

unread,
Oct 12, 2016, 7:42:06 AM10/12/16
to django-...@googlegroups.com
#21602: FileSystemStorage._save() Should Save to a Temporary Filename and Rename to
Attempt to be Atomic
--------------------------------------+------------------------------------
Reporter: Kevin Stone | Owner: nobody
Type: Cleanup/optimization | Status: new

Component: File uploads/storage | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tim Graham):

This is also suggested in #27334 (closed as duplicate).

--
Ticket URL: <https://code.djangoproject.com/ticket/21602#comment:3>

Reply all
Reply to author
Forward
0 new messages