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

Avoid race conditions when installing multiple dependencies from the same git repository. #9658

Merged

Conversation

gustavgransbo
Copy link
Contributor

@gustavgransbo gustavgransbo commented Aug 30, 2024

Multiple installs from the same git repository causes a race condition when the repository is cloned.
This change only allows one operation per repository to be executed by the parallel workers.
Any extra operation on the same git repository will be performed serially.
Since the git repository is cached after the first operation, this is fast.

Building on the setup of @JonathanRayner, I was able to consistently reproduce the error in this repository.

Thank you @JonathanRayner for the hard work!

Pull Request Check List

Resolves: #6958

  • Added tests for changed code.

The race condition occurs inside poetry.vcs.git.Git.clone, this method is mocked in all tests: conftest.py.
This makes the actual race condition hard to test.

Also, since the race condition required quite a bit of external setup, I think it could be challenging to add a good test for this.

  • Updated documentation for changed code.
    This change should not require any new documentation.

@seeker25
Copy link

Thank you so much

Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just a minor nitpick.

I thought a bit about how to write a unit test but even with mocking I could not come up with a good test. Maybe, we can just add kind of a smoke test like test_executor_should_append_subdirectory_for_git but with two packages from the same git repo and check that both are still installed. At least, the order should be fix now, since they are installed serially.

src/poetry/installation/executor.py Outdated Show resolved Hide resolved
@seeker25
Copy link

seeker25 commented Sep 5, 2024

A workaround for now, that works for 1.8.3

poetry config installer.max-workers 1

@radoering
Copy link
Member

@gustavgransbo @TheSven73 Just to be sure before I take another detailed look and potentially merge it: If we outsource #9658 (comment) into a separate issue/PR, this PR is ready from your end, isn't it?

@gustavgransbo
Copy link
Contributor Author

@gustavgransbo @TheSven73 Just to be sure before I take another detailed look and potentially merge it: If we outsource #9658 (comment) into a separate issue/PR, this PR is ready from your end, isn't it?

Yes, from my perspective it's ready to be merged!

Copy link
Contributor

@TheSven73 TheSven73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
needs owner / maintainer approval

src/poetry/installation/executor.py Outdated Show resolved Hide resolved
Copy link
Contributor

@TheSven73 TheSven73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
needs owner / maintainer approval

Multiple installs from the same git repository causes arace condition
when the repository is cloned.
This commit only allows one operation per repository to be executed by
the parallel workers.
Any extra operation will be performed serially.
Since the git repository is cached after the first operation, this is
blazingly fast.

Resolves: python-poetry#6958
Operations for a single repository are grouped and executed serially by
a single worker.
This simplifies the subsequent if/else block.
@radoering radoering force-pushed the feature/avoid-git-race-condition branch from 423f21d to 259100f Compare September 27, 2024 15:28
Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the profound review @TheSven73 and thanks for keeping up with it @gustavgransbo .

@radoering radoering merged commit cb43217 into python-poetry:main Sep 27, 2024
75 checks passed
@TheSven73
Copy link
Contributor

TheSven73 commented Sep 27, 2024

Apologies for the time-fragmented review. And thanks so much @gustavgransbo for tackling this challenging issue !!

@gustavgransbo
Copy link
Contributor Author

Thanks a lot for the thorough review @TheSven73! It’s a privilege to receive such well thought out suggestions, and the resulting solution was much better for it.

@gustavgransbo gustavgransbo deleted the feature/avoid-git-race-condition branch September 28, 2024 12:15
TheSven73 added a commit to TheSven73/poetry that referenced this pull request Sep 29, 2024
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:
python-poetry#9658 (comment)
https://github.com/orgs/python-poetry/discussions/9718#discussioncomment-10785589
TheSven73 added a commit to TheSven73/poetry that referenced this pull request Sep 29, 2024
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:
python-poetry#9658 (comment)
https://github.com/orgs/python-poetry/discussions/9718#discussioncomment-10785589
TheSven73 added a commit to TheSven73/poetry that referenced this pull request Oct 13, 2024
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:
python-poetry#9658 (comment)
https://github.com/orgs/python-poetry/discussions/9718#discussioncomment-10785589
TheSven73 added a commit to TheSven73/poetry that referenced this pull request Oct 14, 2024
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:
python-poetry#9658 (comment)
https://github.com/orgs/python-poetry/discussions/9718#discussioncomment-10785589
radoering pushed a commit that referenced this pull request Oct 14, 2024
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
Secrus pushed a commit to Secrus/poetry that referenced this pull request Oct 27, 2024
Secrus pushed a commit to Secrus/poetry that referenced this pull request Oct 27, 2024
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:
python-poetry#9658 (comment)
https://github.com/orgs/python-poetry/discussions/9718#discussioncomment-10785589
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NotGitRepository error when installing multiple packages from one git repository
4 participants