Skip to content

Commit

Permalink
pythongh-93584: Avoid file race condition in build process
Browse files Browse the repository at this point in the history
  • Loading branch information
tiran committed Jun 7, 2022
1 parent 70690c7 commit a7c361d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Lib/_bootsubprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
subprocess is unavailable. setup.py is not used on Windows.
"""
import os
import time


# distutils.spawn used by distutils.command.build_ext
Expand Down Expand Up @@ -70,7 +71,9 @@ def check_output(cmd, **kwargs):
if not _check_cmd(cmd):
raise ValueError(f"unsupported command: {cmd!r}")

tmp_filename = "check_output.tmp"
# include pid and time stamp to avoid file name clashes with parallel
# builds.
tmp_filename = f"check_output-{os.getpid()}-{int(time.time() )}.tmp"
if not isinstance(cmd, str):
cmd = " ".join(cmd)
cmd = f"{cmd} >{tmp_filename}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid file race condition in ``setup.py`` ``add_multiarch_paths()`` method
and ``_bootsubprocess.check_output()`` function.
17 changes: 11 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import shlex
import sys
import sysconfig
import time
import warnings
from glob import glob, escape
import _osx_support
Expand Down Expand Up @@ -688,9 +689,15 @@ def check_extension_import(self, ext):
def add_multiarch_paths(self):
# Debian/Ubuntu multiarch support.
# https://wiki.ubuntu.com/MultiarchSpec
tmpfile = os.path.join(self.build_temp, 'multiarch')
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)

# Poor man's conflict avoidance. PGO build use $(MAKE), which runs
# make in a separate, untracked process. Sometimes main and subproces
# run `make sharedmods` at the same time. One process removes
# "multiarch" file of the other process.
tmpfile = os.path.join(
self.build_temp, f"multiarch-{os.getpid()}-{int(time.time() )}.tmp"
)
os.makedirs(self.build_temp, exist_ok=True)
ret = run_command(
'%s -print-multiarch > %s 2> /dev/null' % (CC, tmpfile))
multiarch_path_component = ''
Expand All @@ -713,9 +720,7 @@ def add_multiarch_paths(self):
opt = ''
if CROSS_COMPILING:
opt = '-t' + sysconfig.get_config_var('HOST_GNU_TYPE')
tmpfile = os.path.join(self.build_temp, 'multiarch')
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
os.makedirs(self.build_temp, exist_ok=True)
ret = run_command(
'dpkg-architecture %s -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
(opt, tmpfile))
Expand Down

0 comments on commit a7c361d

Please sign in to comment.