Skip to content

Commit

Permalink
Potential Race Condition Fix - OS Rename & Chmod
Browse files Browse the repository at this point in the history
  • Loading branch information
DEKHTIARJonathan committed Dec 24, 2024
1 parent a8c894f commit ee9a79c
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions gitdb/db/loose.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import tempfile
import os
import sys
import time


__all__ = ('LooseObjectDB', )
Expand Down Expand Up @@ -204,8 +205,8 @@ def store(self, istream):
writer.close()
# END assure target stream is closed
except:
if tmp_path:
os.remove(tmp_path)
with suppress(FileNotFoundError):
remove(tmp_path)
raise
# END assure tmpfile removal on error

Expand All @@ -228,9 +229,22 @@ def store(self, istream):
rename(tmp_path, obj_path)
# end rename only if needed

# make sure its readable for all ! It started out as rw-- tmp file
# but needs to be rwrr
chmod(obj_path, self.new_objects_mode)
# Ensure rename is actually done and file is stable
for _ in range(10): # Retry up to 10 times
with suppress(PermissionError):
# make sure its readable for all ! It started out as rw-- tmp file
# but needs to be rwrr
chmod(obj_path, self.new_objects_mode)
break
time.sleep(0.1)
else:
raise PermissionError(
"Impossible to apply `chmod` to file {}".format(obj_path)
)

# Cleanup
with suppress(FileNotFoundError):
remove(tmp_path)
# END handle dry_run

istream.binsha = hex_to_bin(hexsha)
Expand Down

0 comments on commit ee9a79c

Please sign in to comment.