Skip to content

git: be more robust against dangling lock files - #10532

Closed
radoering wants to merge 1 commit into
python-poetry:mainfrom
radoering:fix-file-locked
Closed

radoering wants to merge 1 commit into
python-poetry:mainfrom
radoering:fix-file-locked

Conversation

@radoering

@radoering radoering commented Sep 6, 2025 •

Copy link
Copy Markdown
Member

Pull Request Check List

Resolves: #7832

  • Added tests for changed code.
  • Updated documentation for changed code.

Summary by Sourcery

Make Git clone more robust against stale lock files by retrying ref imports and deleting dangling locks

Bug Fixes:

  • Prevent clone failures due to existing lock files in refs directory

Enhancements:

  • Retry importing refs up to twice when encountering a FileLocked error
  • Remove stale lock files automatically before retrying ref imports

Tests:

  • Add test to verify that cloning removes existing lock files and succeeds
  • Include a tag in the temporary Git repository fixture for testing locked tags

@sourcery-ai

sourcery-ai Bot commented Sep 6, 2025 •

Copy link
Copy Markdown

Reviewer's Guide

Enhance the Git backend’s clone operation to gracefully handle existing dangling lock files by retrying the import operation after removing the lock, and add tests to validate this behavior.

Class diagram for updated GitBackend clone logic

classDiagram
    class GitBackend {
        +_clone(url: str, refspec: GitRefSpec, target: Path) Repo
    }
    class LocalRepo {
        +refs: Refs
        +get_worktree()
    }
    class Refs {
        +import_refs(base, other)
    }
    class FileLocked {
        +lockfilename: bytes
    }
    GitBackend --> LocalRepo : uses
    LocalRepo --> Refs : has
    Refs <.. FileLocked : raises
    GitBackend --> FileLocked : handles exception
    GitBackend --> Logger : logs debug
    GitBackend --> FileSystem : removes lock file
Loading

File-Level Changes

Change Details Files
Retry import_refs on FileLocked exceptions
  • Wrapped import_refs in a two‐attempt loop
  • On FileLocked, log the lock filename and unlink the lock file before retry
src/poetry/vcs/git/backend.py
Add test for cloning over an existing locked tag
  • Introduced test_clone_existing_locked_tag to simulate a v1.lock and verify it’s removed
  • Assert that the repository clone still succeeds with the lock cleaned up
tests/vcs/git/test_backend.py
Seed temp_repo fixture with a v1 tag
  • Added a refs/tags/v1 entry pointing to the head commit in conftest
tests/vcs/git/conftest.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Consider catching and ignoring OSError around the Path.unlink call to avoid failures if the lock file doesn’t exist or can’t be removed.
  • Rather than hardcoding two retries, consider making the retry count configurable or introducing a small backoff to handle more persistent lock file conflicts.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider catching and ignoring OSError around the Path.unlink call to avoid failures if the lock file doesn’t exist or can’t be removed.
- Rather than hardcoding two retries, consider making the retry count configurable or introducing a small backoff to handle more persistent lock file conflicts.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dimbleby

dimbleby commented Sep 6, 2025

Copy link
Copy Markdown
Contributor

automatically removing a lock file surely undermines the point of the lock file?

@radoering

Copy link
Copy Markdown
Member Author

automatically removing a lock file surely undermines the point of the lock file?

Sure, but that is what many users (including myself) do manually if they encounter this state. Of course, it would be better if git/dulwich could handle it by itself. But if they do not, removing it may be a practical way to deal with it. I assume the lock file should not be there at this point anyway. Otherwise, we should at least give a better error message like "try again in some seconds/minutes and if you still experience this issue remove the lock file manually."

Maybe, @jelmer can assess if we risk some other (more serious) issues if we remove the lock file.

@dimbleby

dimbleby commented Sep 6, 2025 •

Copy link
Copy Markdown
Contributor

Agree that a better error message would be desirable. Also agree that if dulwich lock files can dangle then that suggests a bug in dulwich.

I assume the lock file should not be there at this point

I expect that the lock file serves some purpose - and that poetry assuming that dulwich has gone wrong would defeat that purpose.

A user is much better placed to decide whether they are indeed in the middle of performing two operations at once, or whatever, and to make a judgment about whether the lock file is really dangling - or is just doing its job.

@jelmer

jelmer commented Sep 7, 2025

Copy link
Copy Markdown
Contributor

I've added a comment in #7832 (comment) - I think we should try to understand first why this is happening. The lock files aren't specific to Dulwich.

@radoering

Copy link
Copy Markdown
Member Author

Superseded by #10535

@radoering radoering closed this Sep 7, 2025
@github-actions

github-actions Bot commented Oct 8, 2025

Copy link
Copy Markdown

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 8, 2025
@radoering
radoering deleted the fix-file-locked branch December 13, 2025 17:12
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.

(🐞) No error message when FileLocked appears for poetry show --outdated

3 participants