[Python-bugs-list] .pyc writing/reading race condition (PR#189)

gudo@python.org gudo@python.org
Mon, 24 Jan 2000 14:27:26 -0500 (EST)


Full_Name: GvR
Version: 1.5.2
OS: any
Submission from: (NULL) (204.254.19.46)


Patrick Miller reminded me that there's still a race condition in the reading
and writing of .pyc files.  If two processes decide to write the .pyc, and
a third reads the header after the first complete but before the second
starts, but reads the rest of the file after the second starts but before it
is done, the third can read corrupt data (and won't fall back to reading the
.py source).

Solution: when writing the .pyc file, (1) unlink the file before writing,
(2) use open() with the Unix flags that fail creation if the file exists
(and then just treat it like any open failure when writing the .pyc file).