git.iko.soy/typeable/nixpkgs
on master help sign in
python2: backport fix for pyc race condition

This is python bug https://bugs.python.org/issue13146. Fixed since python 3.4. It makes pyc creation atomic, preventing a race condition. The patch has been rebased on our deterministic build patch. It wasn't backported to python 2.7 because there was a complaint about changed semantics. Since files are now created in a temporary directory and then moved, symlinks will be overridden. See https://bugs.python.org/issue17222. That is an edge-case however. Ubuntu and debian have backported the fix in 2013 already, making it mainstream enough for us to adopt.

commit9db3a5869ee882787497a8d762cce99375606e62 authorTimo Kaufmann <timokau@zoho.com> dateMon 1 Jul 2019, 20:01 (7 years ago) parent75aee3c
2 files changed+47
pkgs/development/interpreters/python/cpython/2.7/atomic_pyc.patch+41 pkgs/development/interpreters/python/cpython/2.7/default.nix+6
pkgs/development/interpreters/python/cpython/2.7/atomic_pyc.patchnew file+41
@@ -0,0 +1,41 @@
1+diff --git a/Lib/py_compile.py b/Lib/py_compile.py
2+index 978da73..3559eb9 100644
3+--- a/Lib/py_compile.py
4++++ b/Lib/py_compile.py
5+@@ -120,16 +120,27 @@ def compile(file, cfile=None, dfile=None, doraise=False):
6+ return
7+ if cfile is None:
8+ cfile = file + (__debug__ and 'c' or 'o')
9+- with open(cfile, 'wb') as fc:
10+- fc.write('\0\0\0\0')
11+- if "DETERMINISTIC_BUILD" in os.environ:
12++ # Atomically write the pyc/pyo file. Issue #13146.
13++ # id() is used to generate a pseudo-random filename.
14++ path_tmp = '{}.{}'.format(cfile, id(cfile))
15++ try:
16++ with open(path_tmp, 'wb') as fc:
17+ fc.write('\0\0\0\0')
18+- else:
19+- wr_long(fc, timestamp)
20+- marshal.dump(codeobject, fc)
21+- fc.flush()
22+- fc.seek(0, 0)
23+- fc.write(MAGIC)
24++ if "DETERMINISTIC_BUILD" in os.environ:
25++ fc.write('\0\0\0\0')
26++ else:
27++ wr_long(fc, timestamp)
28++ marshal.dump(codeobject, fc)
29++ fc.flush()
30++ fc.seek(0, 0)
31++ fc.write(MAGIC)
32++ os.rename(path_tmp, cfile)
33++ except OSError:
34++ try:
35++ os.unlink(path_tmp)
36++ except OSError:
37++ pass
38++ raise
39+
40+ def main(args=None):
41+ """Compile several source files.
pkgs/development/interpreters/python/cpython/2.7/default.nix+6
@@ -79,6 +79,12 @@ let
7979 sha256 = "0l9rw6r5r90iybdkp3hhl2pf0h0s1izc68h5d3ywrm92pq32wz57";
8080 })
8181
82+ # Fix race-condition during pyc creation. Has a slight backwards
83+ # incompatible effect: pyc symlinks will now be overridden
84+ # (https://bugs.python.org/issue17222). Included in python >= 3.4,
85+ # backported in debian since 2013.
86+ # https://bugs.python.org/issue13146
87+ ./atomic_pyc.patch
8288 ] ++ optionals (x11Support && stdenv.isDarwin) [
8389 ./use-correct-tcl-tk-on-darwin.patch
8490 ] ++ optionals stdenv.isLinux [