Skip to content

Install() race condition #2124

Closed
Closed

Description

@bdbaddog

This issue was originally created at: 2008-07-02 11:01:32.
This issue was reported by: jonasolsson.
jonasolsson said at 2008-07-02 11:01:32

When running on a dual quad core XP SP2 machine and with -j 10 I get
random occurances of permission denied or file not found errors when
trying to access files installed with the Install() builder.
In this edited example a import library is Install'ed and then when
trying to link with it I get 'cannot open file' error.

Here's one edited example:

cl /nologo /GF /GS- /EHa /W4 /Zc:wchar_t-
/arch:SSE2 /TP /WX /X /MDd /Od /RTC1 /wd4103 /wd4115 /wd4200 /wd4201 /wd4214 /
wd4505 /wd4996 /DWIN32 /DUNICODE /D_UNICODE /D_32BIT /IZ:\tools\wtl75
\include /IZ:\tools\msvs2005net\vc\platformsdk\include /IZ:\tools\msvs2005net\v
c\include /IZ:\tools\msvs2005net\vc\atlmfc\include /IZ:\tools\xpsp2sdk\include
/c a.cpp /Fobuild\debug\c.obj
a.cpp
Install file: "build\debug\b.lib" as "export\debug\b.lib"
cl /nologo /GF /GS- /EHa /W4 /Zc:wchar_t-
/arch:SSE2 /TP /WX /X /MDd /Od /RTC1 /wd4103 /wd4115 /wd4200 /wd4201 /wd4214 /
wd4505 /wd4996 /DWIN32 /DUNICODE /D_UNICODE /D_32BIT /IZ:\tools\wtl75
\include /IZ:\tools\msvs2005net\vc\platformsdk\include /IZ:\tools\msvs2005net\v
c\include /IZ:\tools\msvs2005net\vc\atlmfc\include /IZ:\tools\xpsp2sdk\include
/c c.cpp /Fobuild\debug\c.obj
cl /nologo /GF /GS- /EHa /W4 /Zc:wchar_t-
/arch:SSE2 /TP /WX /X /MDd /Od /RTC1 /wd4103 /wd4115 /wd4200 /wd4201 /wd4214 /
wd4505 /wd4996 /DWIN32 /DUNICODE /D_UNICODE /D_32BIT /IZ:\tools\wtl75
\include /IZ:\tools\msvs2005net\vc\platformsdk\include /IZ:\tools\msvs2005net\v
c\include /IZ:\tools\msvs2005net\vc\atlmfc\include /IZ:\tools\xpsp2sdk\include
/c d.cpp /Fobuild\debug\d.obj
d.cpp
cl /nologo /GF /GS- /EHa /W4 /Zc:wchar_t-
/arch:SSE2 /TP /WX /X /MDd /Od /RTC1 /wd4103 /wd4115 /wd4200 /wd4201 /wd4214 /
wd4505 /wd4996 /DWIN32 /DUNICODE /D_UNICODE /D_32BIT /IZ:\tools\wtl75
\include /IZ:\tools\msvs2005net\vc\platformsdk\include /IZ:\tools\msvs2005net\v
c\include /IZ:\tools\msvs2005net\vc\atlmfc\include /IZ:\tools\xpsp2sdk\include
/c e.cpp /Fobuild\debug\e.obj
e.cpp
cl /nologo /GF /GS- /EHa /W4 /Zc:wchar_t-
/arch:SSE2 /TP /WX /X /MDd /Od /RTC1 /wd4103 /wd4115 /wd4200 /wd4201 /wd4214 /
wd4505 /wd4996 /DWIN32 /DUNICODE /D_UNICODE /D_32BIT /IZ:\tools\wtl75
\include /IZ:\tools\msvs2005net\vc\platformsdk\include /IZ:\tools\msvs2005net\v
c\include /IZ:\tools\msvs2005net\vc\atlmfc\include /IZ:\tools\xpsp2sdk\include
/c f.cpp /Fobuild\debug\f.obj
e.cpp
f.cpp
link /nologo /machine:X86 /debug /incremental:no /OPT:NOWIN98 /WX /defaultlib:k
ernel32 /defaultlib:user32 /defaultlib:gdi32 /defaultlib:advapi32 /defaultlib:w
inspool /defaultlib:comdlg32 /defaultlib:shell32 /defaultlib:ole32 /defaultlib:
oleaut32 /defaultlib:uuid /defaultlib:odbc32 /defaultlib:odbccp32 /dll /out:bui
ld\debug\g.dll /implib:build\debug\g.lib /LIBPATH:export\Debug b.lib e.obj
cl /nologo /GF /GS- /EHa /W4 /Zc:wchar_t-
/arch:SSE2 /TP /WX /X /MDd /Od /RTC1 /wd4103 /wd4115 /wd4200 /wd4201 /wd4214 /
wd4505 /wd4996 /DWIN32 /DUNICODE /D_UNICODE /D_32BIT /IZ:\tools\wtl75
\include /IZ:\tools\msvs2005net\vc\platformsdk\include /IZ:\tools\msvs2005net\v
c\include /IZ:\tools\msvs2005net\vc\atlmfc\include /IZ:\tools\xpsp2sdk\include
/c f.cpp /Fobuild\debug\f.obj
LINK : fatal error LNK1104: cannot open file 'export\Debug\b.lib'

gregnoel said at 2008-07-15 19:13:57

Bug party triage. It's not clear if this is a bug (two different names are used
for the file), but a traceback is clearly a very unfriendly way to diagnose the
conflict. This issue may be retriaged later.

jonasolsson said at 2008-07-28 14:30:09

I constructed a simple test case:

env = Environment()

testFiles = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z'
]

for file1 in testFiles:
for file2 in testFiles:
f = open(file1 + file2 + '.cpp', "w")
f.write('int test_' + file1 + file2 + '()\n')
f.write('{\nreturn 0;\n}')
f.close()

main = open('main.cpp', "w")
for file1 in testFiles:
for file2 in testFiles:
main.write('int test_' + file1 + file2 + '(void);\n')
main.write('int main()\n')
main.write('{\n');
for file1 in testFiles:
for file2 in testFiles:
main.write('test_' + file1 + file2 + '();\n')
main.write('return 0;\n}')
main.close()

source = ['main.cpp']
for file1 in testFiles:
for file2 in testFiles:
installedFile = 'test-' + file1 + file2 + '.cpp'
env.InstallAs(installedFile, file1 + file2 + '.cpp')
source.append(installedFile)

And then when executing I get random errors, like:

Install file: "gy.cpp" as "test-gy.cpp"
test-gt.cpp
cl /nologo /TP /c test-gx.cpp /Fotest-gx.obj
cl /nologo /TP /c test-gy.cpp /Fotest-gy.obj
Install file: "ha.cpp" as "test-ha.cpp"
cl /nologo /TP /c test-gz.cpp /Fotest-gz.obj
test-gx.cpp
cl /nologo /TP /c test-ha.cpp /Fotest-ha.obj
Install file: "hb.cpp" as "test-hb.cpp"
test-gv.cpp
cl /nologo /TP /c test-hb.cpp /Fotest-hb.obj
Install file: "hc.cpp" as "test-hc.cpp"
Install file: "hd.cpp" as "test-hd.cpp"
cl /nologo /TP /c test-hc.cpp /Fotest-hc.obj
test-gz.cpp
test-gy.ctest-ha.cpp
cl /nologo /TP /c test-hd.cpp /Fotest-hd.obj
pp
Install file: "he.cpp" as "test-he.cpp"
cl /nologo /TP /c test-he.cpp /Fotest-he.obj
c1xx : fatal error C1083: Cannot open source file: 'test-ha.cpp': Permission
Ins
tall file: "hf.cpp" as "test-hf.cpp"
denied
test-hb.cspcons: *** [test-ha.obj] Error 2
p
cl /nologo /TP /c test-hf.cpp /Fotest-hf.obj
test-hc.cpp
test-he.cpp
test-hd.cpp
test-hf.cpp
scons: building terminated because of errors.

jonasolsson said at 2008-07-28 15:09:47

I missed one line in the cut and paste of the test script. The last
line of the SConstruct should be:

env.Program(target= 'testInstall', source = source)

jonasolsson said at 2008-07-28 15:18:31

Created an attachment (id=461)
Test for Install() race condition, use scons -j 6

jonasolsson said at 2008-07-30 15:41:44

Created an attachment (id=462)
Patch to use os.system('copy ..') to do the copy instead of using shutil.copy2() is one (ugly) way to fix problem

gregnoel said at 2008-07-31 05:18:15

I revamped the test SConstruct and started an overnight test run, which repeated
the same -j6 test over and over again until there was an error. In the morning,
I was interrupted by a minor family emergency, so I didn't get back to it until
it had been running over twenty (20) hours. It takes the test build about 75
seconds to run, so that's the next best thing to 1,000 test runs. There were
zero (0) failures (OS X 10.5, gcc 4.0.1, scons SVN).

For the record, here's the final SConstruct:

testAlpha = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ]

Uncomment one of these:

#testDigit = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
testDigit = ['']

env = Environment()

main1 = ''
main2 = 'int main()\n{\n'
source = ['main.cpp']
for n1 in testAlpha:
for n2 in testAlpha:
for n3 in testDigit:
name = n1 + n2 + n3
file = name + '.cpp'
f = open(file, "w")
f.write('int test_' + name + '(){return 0;}\n')
f.close()
main1 = main1 + 'int test_' + name + '(void);\n'
main2 = main2 + ' test_' + name + '();\n'
test = 'test-' + file
#env.InstallAs(test, file)
env.Command(test, file, Copy(test, file))
source.append(test)

f = open('main.cpp', "w")
f.write(main1 + main2 + ' return 0;\n}\n')
f.close()

c = env.Program('testInstall', source)
env.Command('run', c, c[0].abspath)

I also used Copy() instead of InstallAs() and it still works fine (although
that's been tested only a few dozen times).

In absence of further evidence, I have to believe that the error is caused by
bugs in DOS, which is notorious for not being able to close and reopen files
rapidly. (Forcing the copy to be done by an external command probably slows
down the processing just enough so that the race doesn't occur as often.) I've
set the milestone so that it will be re-triaged at the next bug party, but the
fact that the race doesn't show up when run on a real operating system strongly
suggests that there's nothing we can do about it.

jonasolsson said at 2008-07-31 08:52:29

I think the issue is related to the inheritance of open file handles. But
the installation of the pywin32 extension does not fix this issue.
Perhaps shutil.copy2() doesn't use the patched way of opening files?
In that case a new copy2() is perhaps required.

azverkan said at 2008-08-04 17:55:56

Reproduced this locally.

The exact error when cl.exe fails is "SHARING VIOLATION", as the file in
question is already open for read/write in one of cmd.exe processes that is used
to spawn cl.exe.

gregnoel said at 2008-08-05 07:08:21

Bug party triage. File is opened and then the no-inherit flag is set in a
second statement. It's possible for another thread to fork between those two
operations leading to a race. Brandon to investigate in more detail and
determine if we can do anything. Leave the issue -unspecified- so we'll review
it again next week.

stevenknight said at 2008-08-06 16:24:43

FWIW, I'm now seeing this regularly in my builds as well. Everything was
working fine, then I added env.Install() of a bunch of libraries into a common
directory, after which I've started seeing regular occurrences of the same
LNK1104 errors.

gregnoel said at 2008-08-12 11:11:33

Bug party triage. No report from Brandon, so we'll change it to research until
the report is added, at which time Brandon should change it back to
-unspecified- so we'll triage it again. Steven will merge issue 2165 into this
issus, which has a possible explanation for this problem (interference from
virus scanner).

stevenknight said at 2008-08-18 16:47:58

*** Issue 2165 has been marked as a duplicate of this issue. ***

stevenknight said at 2008-08-18 16:48:39

Here's the text from issue #2165 about other possible race condition explanations:

This is to record a possible race condition suggested by a colleague:

The main SCons thread that walks the DAG is responsible for calling
task.prepare(), which ends up calling the necessary per-Node prepare() function
to unlink a file before we try to create it.

It then puts the job on the queue and a separate worker thread is responsible
for actually trying to build the file.

The theory is that it's possible on Windows for some statefulness about the
file's existence (and being open) to be stuck after the main thread closes it,
and still in effect when the worker thread tries to access it.

One possibility here is that some virus-scanning software apparently hooks the
Windows analog to the close() call and locks the file for some indefinite amount
of time afterwards to make sure that everything is virus-free. That doesn't
really explain the potential unlink-in-prepare() scenario, but may be a source
of other issues where a second thread tries to access a file created by another
thread.

azverkan said at 2009-03-01 23:40:27

I haven't run into the virus scanner issue, but I've been unable to make the
SetHandleInformation HANDLE_FLAG_INHERIT work without placing a global thread
lock around the code before the handle gets opened and after the
SetHandleInformation and wrapping all process spawning with the same lock to
prevent any spawns from occurring during the brief interval when the handle has
been opened but has not disabled inheritance yet.

This bug is still extremely easy to reproduce if you run SCons with
sys.setcheckinterval() set to a value between 0 and 10 or so.

Having to place a global lock inside SCons is not the preferred way to solve
this issue as there are still issues with Python extension modules opening
files. I think the best way to try and solve the issue is to spawn a number of
separate processes in a known state with no handles open on startup. And then
proxy the the subprocess requests from the main SCons process into these
separate processes.

stevenknight said at 2009-03-03 06:57:29

Reset target milestone to -unspecified- for Bug Party re-triaging.

jason_at_intel said at 2009-03-04 21:24:09

I did a little testing on a vista system quad core.

It was very easy to reproduce the issue with a -j10 for me.

I did get python crash sometimes however, There bug is some unhanded exception,
however the debugger can't attach to it :-( for some reason.

I did see that a simple addition of Progress(".\n") makes the problem go away.

This suggests a small delay fixes the problem. This needs to be looked into more.

The python crash there maybe a python issue when running in a threaded environment.

jason_at_intel said at 2009-03-04 22:21:13

Just to test if this windows or python. I modifed SCons to call
win32file.CopyFile(...) instead of the shutil.copy2(...)

For me at least this also made the issue go away. ( sample hack below)

def copy_func(dest, src):
import win32file
SCons.Node.FS.invalidate_node_memos(dest)
if SCons.Util.is_List(src) and os.path.isdir(dest):
for file in src:
win32file.CopyFile(file,dest,False)
#shutil.copy2(file, dest)
return 0
elif os.path.isfile(src):
win32file.CopyFile(src,dest,False)
#return shutil.copy2(src, dest)
else:
return shutil.copytree(src, dest, 1)
return 0

This may suggest that the python impl on windows has a delay in the closing (
or cleaning up) of the file handle (GB collector threading bug?) Calling the
System API in windows maybe be faster and safer as native code is called for
the file processing instead.

azverkan said at 2009-03-04 23:47:02

The other interesting part about using worker processes for spawning is that
chdir() will work with parallel builds.

azverkan said at 2009-03-05 01:17:44

Posted 2 test cases in a git tree.

http://github.com/azverkan/scons-testing/tree/master

test.py hammers the process spawning code outside of scons and is able to
reproduce handle inheritance bugs extremely quickly. You can use this same
script with the ctypes patch. Unless I've screwed something up that I haven't
noticed yet, the pywin32 code seems to work fine and the ctypes code fails to
prevent handle inheritance.

On my machine with an extremely contrived test case of 51 threads, the
multiprocessing module beats the pants off of the locking / pywin32 version.

test.py --threads 50 --lock --pywin32
Real 5.402000s

test.py --threads 50 --multiprocessing
Real 0.677000s

With a single background thread, locking and pywin32 module inheritance beats
the multiprocessing approach.

test.py --threads 1 --lock --pywin32
Real 0.220000s

test.py --threads 1 --multiprocessing
Real 0.413000s

The SConstruct test case tests the same multiprocessing module in SCons. Here
the multiprocessing module is around 10% slower across the board. Oddly enough
the Python 2.6 release on Windows tends to crash with a large number of threads
if you do not use the multiprocessing module. I'll try get a debug version of
Python on my windows box to try and find out why.

azverkan said at 2009-03-05 01:21:12

Specifically ctypes.cdll.msvcrt._get_osfhandle() is what fail for me. Using the
pywin32 version of msvcrt.get_osfhandle() with the
ctypes.windll.kernel32.SetHandleInformation() function makes it succeed for me.

gregnoel said at 2009-03-05 09:20:55

Bug party triage. Brandon, we think this is worth prototyping. Can you create
a SCons variant, maybe in a branch somewhere, that works the same way as your
test code? If not, don't hesitate to change this issue back to -unspecified-
and we'll reconsider how to proceed. Tks.

garyo said at 2014-05-24 10:44:56

Now that our floor is 2.7 (or at least 2.6 which is where multiprocessing was
introduced) perhaps we should use that on Windows. Unfortunately Brandon's test
github repo is gone now. I'll assign to me.

garyo said at 2014-05-24 11:33:38

Sorry, last comment was incorrect. As of rev 4aaae80f393d, we switched to
subprocess for all spawning. I expect that should fix this issue. Please reopen
with a test case if it still happens.

Votes for this issue: 8.

jonasolsson attached Sconstruct at 2008-07-28 15:18:31.

Test for Install() race condition, use scons -j 6

jonasolsson attached install.py.diff at 2008-07-30 15:41:44.

Patch to use os.system('copy ..') to do the copy instead of using shutil.copy2() is one (ugly) way to fix problem

gregnoel said this issue blocks #2291 at 2009-03-05 14:25:54.

stevenknight said this issue is duplicated by #2165 at 2008-08-18 16:47:59.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions