Skip to content

Race conditions in engine/SCons/Node/FS.py/CachePushFunc. #1725

Closed

Description

@bdbaddog

This issue was originally created at: 2007-09-05 06:55:16.
This issue was reported by: cko.
cko said at 2007-09-05 06:55:16

CachePushFunc tests for the existance of cachefile,
then it creates cachefile+'.tmp', then it copies
the data to cachefile+'.tmp' and finally it renames
cachefile+'.tmp' to cachefile.

This can lead to the following race condition:

Process A Process B
tests for the existance
of cachefile
tests for the existance
of cachefile

creates cachefile+'.tmp'
starts writing to
cachefile+'.tmp'
creates cachefile+'.tmp'
starts writing to
cachefile+'.tmp'
continues writing to
cachefile+'.tmp'

Obviously, both processes can write the same file at
the same time, in effect destroying the data.

There is another race condition in the same function,
when the directory is initially created:

Process A Process B
tests for the existance
of cachedir
tests for the existance
of cachedir
creates cachedir
tries to create cachedir
and crashes

Here is a suggested fix for both:

diff -ru scons-0.97.0d20070809/engine/SCons/Node/FS.py
scons-0.97.0d20070820/engine/SCons/Node/FS.py
--- scons-0.97.0d20070809/engine/SCons/Node/FS.py 2007-08-10
00:03:52.000000000 +0200
+++ scons-0.97.0d20070820/engine/SCons/Node/FS.py 2007-08-20
21:54:20.000000000 +0200
@@ -257,6 +257,15 @@
return
fs = t.fs
cachedir, cachefile = t.cachepath()

  • if not fs.isdir(cachedir):

  •    try:
    
  •        fs.makedirs(cachedir)
    
  •    except OSError:
    
  •        #  We may have received an OSError because another process
    
  •        #  has beaten us creating the directory.
    
  •        if not fs.isdir(cachedir):
    
  •            raise
    
  • if fs.exists(cachefile):
    # Don't bother copying it if it's already there. Note that
    # usually this "shouldn't happen" because if the file already
    @@ -270,10 +279,8 @@

    fs.CacheDebug('CachePush(%s): pushing to %s\n', t, cachefile)

  • if not fs.isdir(cachedir):

  •    fs.makedirs(cachedir)
    
  • tempfile = cachefile+'.tmp'

  • tempfile = cachefile+'.tmp_'+str(os.getpid())
    try:
    if fs.islink(t.path):
    fs.symlink(fs.readlink(t.path), tempfile)

We experience corrupted files in our caches occasionally,
so I consider this a very serious problem.

stevenknight said at 2007-09-07 04:28:05

Patch has been integrated for next release. Thank you! --SK

stevenknight said this issue blocks #1664 at 2007-09-07 04:28:05.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions