Opened 21 months ago

Last modified 18 months ago

#21602 new Cleanup/optimization

FileSystemStorage._save() Should Save to a Temporary Filename and Rename to Attempt to be Atomic

Reported by: kevinastone Owned by: nobody
Component: File uploads/storage Version: master
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 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.

Change History (2)

comment:1 Changed 18 months ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Agreed that atomic rename should be used.

comment:2 Changed 18 months ago by timo

  • Type changed from Uncategorized to Cleanup/optimization
Note: See TracTickets for help on using tickets.
Back to Top