Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix race condition on namespace package installation #9159

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/poetry/installation/wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,35 @@ def write_to_fs(
stream: BinaryIO,
is_executable: bool,
) -> RecordEntry:
import base64
import hashlib

from installer.records import Hash
from installer.records import RecordEntry
from installer.utils import _COPY_BUFSIZE
from installer.utils import copyfileobj_with_hashing
from installer.utils import make_file_executable

target_path = Path(self.scheme_dict[scheme]) / path
if target_path.exists():
# Contrary to the base library we don't raise an error here since it can
# break pkgutil-style and pkg_resource-style namespace packages.
logger.warning(f"Installing {target_path} over existing file")
# We instead log a warning and ignore it. See issue #9158.
logger.warning(f"{target_path} already exists. Ignoring.")
# Following is copied from copyfileobj_with_hashing, to calculate the hash
StefanBRas marked this conversation as resolved.
Show resolved Hide resolved
# without copying to the file again.
hasher = hashlib.new(self.hash_algorithm)
size = 0
while True:
buf = stream.read(_COPY_BUFSIZE)
if not buf:
break
hasher.update(buf)
size += len(buf)
hash_ = (
base64.urlsafe_b64encode(hasher.digest()).decode("ascii").rstrip("=")
)
return RecordEntry(path, Hash(self.hash_algorithm, hash_), size)

parent_folder = target_path.parent
if not parent_folder.exists():
Expand Down