Skip to content

Commit a0467a3

Browse files
committed
Fix wheel cache concurrency
Avoid concurrency issues when storing wheels into the cache of locally built wheels, when the temporary build directory is on a different filesystem than the cache directory. This commit also improves performance in such situations, by directly building the wheel into the cache filesystem.
1 parent 49c4ab2 commit a0467a3

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

news/13540.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Avoid concurrency issues and improve performance when storing wheels into the
2+
cache of locally built wheels, when the temporary build directory is on a
3+
different filesystem than the cache directory.

src/pip/_internal/wheel_builder.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import logging
66
import os.path
77
import re
8-
import shutil
98
from collections.abc import Iterable
9+
from tempfile import TemporaryDirectory
1010

1111
from pip._vendor.packaging.utils import canonicalize_name, canonicalize_version
1212
from pip._vendor.packaging.version import InvalidVersion, Version
@@ -24,7 +24,6 @@
2424
from pip._internal.utils.misc import ensure_dir, hash_file
2525
from pip._internal.utils.setuptools_build import make_setuptools_clean_args
2626
from pip._internal.utils.subprocess import call_subprocess
27-
from pip._internal.utils.temp_dir import TempDirectory
2827
from pip._internal.utils.urls import path_to_url
2928
from pip._internal.vcs import vcs
3029

@@ -189,7 +188,7 @@ def _build_one_inside_env(
189188
global_options: list[str],
190189
editable: bool,
191190
) -> str | None:
192-
with TempDirectory(kind="wheel") as temp_dir:
191+
with TemporaryDirectory(dir=output_dir) as temp_dir:
193192
assert req.name
194193
if req.use_pep517:
195194
assert req.metadata_directory
@@ -207,14 +206,14 @@ def _build_one_inside_env(
207206
name=req.name,
208207
backend=req.pep517_backend,
209208
metadata_directory=req.metadata_directory,
210-
tempd=temp_dir.path,
209+
tempd=temp_dir,
211210
)
212211
else:
213212
wheel_path = build_wheel_pep517(
214213
name=req.name,
215214
backend=req.pep517_backend,
216215
metadata_directory=req.metadata_directory,
217-
tempd=temp_dir.path,
216+
tempd=temp_dir,
218217
)
219218
else:
220219
wheel_path = build_wheel_legacy(
@@ -223,15 +222,17 @@ def _build_one_inside_env(
223222
source_dir=req.unpacked_source_directory,
224223
global_options=global_options,
225224
build_options=build_options,
226-
tempd=temp_dir.path,
225+
tempd=temp_dir,
227226
)
228227

229228
if wheel_path is not None:
230229
wheel_name = os.path.basename(wheel_path)
231230
dest_path = os.path.join(output_dir, wheel_name)
232231
try:
233232
wheel_hash, length = hash_file(wheel_path)
234-
shutil.move(wheel_path, dest_path)
233+
# We can do a rename here because wheel_path is guaranteed to be
234+
# in the same filesystem as output_dir.
235+
os.rename(wheel_path, dest_path)
235236
logger.info(
236237
"Created wheel for %s: filename=%s size=%d sha256=%s",
237238
req.name,

0 commit comments

Comments
 (0)