From 18d4879fada608df86e2c1746287e854aa2e0dbf Mon Sep 17 00:00:00 2001 From: Lukas Schmelting Date: Fri, 26 Dec 2025 11:35:47 +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: pypa/setuptools#3942 --- distutils/command/build_ext.py | 7 +++++-- newsfragments/3942.bugfix.rst | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 newsfragments/3942.bugfix.rst diff --git a/distutils/command/build_ext.py b/distutils/command/build_ext.py index ec45b440..daf30519 100644 --- a/distutils/command/build_ext.py +++ b/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, ) diff --git a/newsfragments/3942.bugfix.rst b/newsfragments/3942.bugfix.rst new file mode 100644 index 00000000..fac4bf79 --- /dev/null +++ b/newsfragments/3942.bugfix.rst @@ -0,0 +1 @@ +Fixed non-deterministic parallel builds of extensions that share source files.