From e2f2580ad0122b222fbcdfc17a5971f41704ed2a Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sun, 24 Feb 2013 15:34:33 +0200 Subject: [PATCH] pyximport: fix a bug crashing other processes pyxbuild overwrites .so file contents, corrupting any mmapped code image loaded. If pyximporting the same module from two different processes, on Linux this leads to crashes with bus error. This commit fixes the issue by unlinking the file first. On Linux et al., this does not change the file contents of any open FDs. On Windows, unlinking fails, and we just switch to using a different file name. --- pyximport/pyxbuild.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pyximport/pyxbuild.py b/pyximport/pyxbuild.py index 0d805408ee1..82401cdbc92 100644 --- a/pyximport/pyxbuild.py +++ b/pyximport/pyxbuild.py @@ -124,6 +124,17 @@ def pyx_to_dll(filename, ext = None, force_rebuild = 0, basename + '.reload%s'%count) try: import shutil # late import / reload_support is: debugging + try: + # Try to unlink first --- if the .so file + # is mmapped by another process, + # overwriting its contents corrupts the + # loaded image (on Linux) and crashes the + # other process. On Windows, unlinking an + # open file just fails. + if os.path.isfile(r_path): + os.unlink(r_path) + except OSError: + continue shutil.copy2(org_path, r_path) so_path = r_path except IOError: