Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ee9a79c

Browse files
committedDec 24, 2024·
Potential Race Condition Fix - OS Rename & Chmod
1 parent a8c894f commit ee9a79c

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed
 

‎gitdb/db/loose.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import tempfile
5555
import os
5656
import sys
57+
import time
5758

5859

5960
__all__ = ('LooseObjectDB', )
@@ -204,8 +205,8 @@ def store(self, istream):
204205
writer.close()
205206
# END assure target stream is closed
206207
except:
207-
if tmp_path:
208-
os.remove(tmp_path)
208+
with suppress(FileNotFoundError):
209+
remove(tmp_path)
209210
raise
210211
# END assure tmpfile removal on error
211212

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

231-
# make sure its readable for all ! It started out as rw-- tmp file
232-
# but needs to be rwrr
233-
chmod(obj_path, self.new_objects_mode)
232+
# Ensure rename is actually done and file is stable
233+
for _ in range(10): # Retry up to 10 times
234+
with suppress(PermissionError):
235+
# make sure its readable for all ! It started out as rw-- tmp file
236+
# but needs to be rwrr
237+
chmod(obj_path, self.new_objects_mode)
238+
break
239+
time.sleep(0.1)
240+
else:
241+
raise PermissionError(
242+
"Impossible to apply `chmod` to file {}".format(obj_path)
243+
)
244+
245+
# Cleanup
246+
with suppress(FileNotFoundError):
247+
remove(tmp_path)
234248
# END handle dry_run
235249

236250
istream.binsha = hex_to_bin(hexsha)

0 commit comments

Comments
 (0)
Please sign in to comment.