Skip to content

Commit 5297739

Browse files
committed
executor: fix race condition with forked git repos
When installing packages from different directories of a forked monorepo, a race condition may occur, where multiple git clients are interacting in parallel with the same git repository. Fix by serializing git operations that interact with the same git repository. This makes the test succeed. Remove xfail. Links: #9658 (comment) https://github.com/orgs/python-poetry/discussions/9718#discussioncomment-10785589
1 parent 854465a commit 5297739

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/poetry/installation/executor.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from poetry.utils.helpers import remove_directory
3434
from poetry.utils.isolated_build import IsolatedBuildError
3535
from poetry.utils.isolated_build import IsolatedBuildInstallError
36+
from poetry.vcs.git import Git
3637

3738

3839
if TYPE_CHECKING:
@@ -46,6 +47,12 @@
4647
from poetry.utils.env import Env
4748

4849

50+
def _package_get_name(package: Package) -> str | None:
51+
if url := package.repository_url:
52+
return Git.get_name_from_source_url(url)
53+
return None
54+
55+
4956
class Executor:
5057
def __init__(
5158
self,
@@ -167,8 +174,9 @@ def execute(self, operations: list[Operation]) -> int:
167174
if is_parallel_unsafe:
168175
serial_operations.append(operation)
169176
elif operation.package.source_type == "git":
170-
# Git operations on the same repository should be executed serially
171-
serial_git_operations[operation.package.source_url].append(
177+
# Serially execute git operations that get cloned to the same directory,
178+
# to prevent multiple parallel git operations in the same repo.
179+
serial_git_operations[_package_get_name(operation.package)].append(
172180
operation
173181
)
174182
else:
@@ -604,8 +612,6 @@ def _prepare_archive(
604612
)
605613

606614
def _prepare_git_archive(self, operation: Install | Update) -> Path:
607-
from poetry.vcs.git import Git
608-
609615
package = operation.package
610616
assert package.source_url is not None
611617

tests/installation/test_executor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,6 @@ def test_executor_should_install_multiple_packages_from_same_git_repository(
11331133
assert spy.call_count == 2
11341134

11351135

1136-
@pytest.mark.xfail
11371136
def test_executor_should_install_multiple_packages_from_forked_git_repository(
11381137
mocker: MockerFixture,
11391138
tmp_venv: VirtualEnv,

0 commit comments

Comments
 (0)