From 13c97512e5172b69462690e3bacfadc944fda4cb Mon Sep 17 00:00:00 2001 From: Lukas Schmelting Date: Sat, 20 Dec 2025 16:07:42 +0100 Subject: [PATCH] Prevent object file collisions in parallel extension builds Parallel builds of extensions that share source files may write to the same object file paths under a common build directory, resulting in race conditions and non-deterministic build outputs. Use a per-extension build directory to isolate object files and ensure deterministic, parallel-safe builds. Bug: #3942 --- newsfragments/3942.bugfix.rst | 1 + setuptools/_distutils/command/build_ext.py | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 newsfragments/3942.bugfix.rst diff --git a/newsfragments/3942.bugfix.rst b/newsfragments/3942.bugfix.rst new file mode 100644 index 0000000000..fac4bf7923 --- /dev/null +++ b/newsfragments/3942.bugfix.rst @@ -0,0 +1 @@ +Fixed non-deterministic parallel builds of extensions that share source files. diff --git a/setuptools/_distutils/command/build_ext.py b/setuptools/_distutils/command/build_ext.py index ec45b4403e..daf30519b7 100644 --- a/setuptools/_distutils/command/build_ext.py +++ b/setuptools/_distutils/command/build_ext.py @@ -562,9 +562,12 @@ def build_extension(self, ext) -> None: for undef in ext.undef_macros: macros.append((undef,)) + # Per-extension build dir to avoid conflicts in parallel builds. + ext_build_temp = os.path.join(self.build_temp, ext.name) + objects = self.compiler.compile( sources, - output_dir=self.build_temp, + output_dir=ext_build_temp, macros=macros, include_dirs=ext.include_dirs, debug=self.debug, @@ -595,7 +598,7 @@ def build_extension(self, ext) -> None: extra_postargs=extra_args, export_symbols=self.get_export_symbols(ext), debug=self.debug, - build_temp=self.build_temp, + build_temp=ext_build_temp, target_lang=language, )