Skip to content

Race condition writing objects in LooseObjectDB #85

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

Closed
lordmauve opened this issue Feb 13, 2023 · 1 comment · Fixed by #91
Closed

Race condition writing objects in LooseObjectDB #85

lordmauve opened this issue Feb 13, 2023 · 1 comment · Fixed by #91

Comments

@lordmauve
Copy link

There's a TOCTTOU in LooseObjectDB when storing a new object, that causes storing an object to fail with FileExistsError:

   File "patch_tree.py", line 132, in write_tree
     return self.db.store(istream).binsha
   File ".venv/lib/python3.9/gitdb/db/git.py", line 77, in store
     return self._loose_db.store(istream)
   File ".venv/lib/python3.9/gitdb/db/loose.py", line 226, in store
     mkdir(obj_dir)
FileExistsError: [Errno 17] File exists: '/tmp/tmp.flUcclypOQ/objects/a0'

The problem code is

            if not isdir(obj_dir):
                mkdir(obj_dir)

and what is happening is

  1. isdir() returns False because the directory does not exist at that moment
  2. The directory is created in another thread
  3. mkdir() fails with FileExistsError

This can be avoided by using an EAFP pattern, e.g.

with contextlib.suppress(FileExistsError):
    mkdir(obj_dir)
@lordmauve lordmauve changed the title Race condition in LooseDB Race condition in LooseObjectDB Feb 13, 2023
@lordmauve lordmauve changed the title Race condition in LooseObjectDB Race condition writing objects in LooseObjectDB Feb 13, 2023
@Byron
Copy link
Member

Byron commented Feb 14, 2023

Thanks for reporting and for providing a suggestion for a fix. A PR is definitely welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants