From 74a0eabbc03209593ea1562498802359ae8a3db7 Mon Sep 17 00:00:00 2001 From: Jonathan Dekhtiar Date: Mon, 23 Dec 2024 23:02:58 -0500 Subject: [PATCH 1/2] Potential Race Condition Fix - OS Rename & Chmod --- gitdb/db/loose.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/gitdb/db/loose.py b/gitdb/db/loose.py index 87cde86..ccefe40 100644 --- a/gitdb/db/loose.py +++ b/gitdb/db/loose.py @@ -54,6 +54,7 @@ import tempfile import os import sys +import time __all__ = ('LooseObjectDB', ) @@ -205,7 +206,7 @@ def store(self, istream): # END assure target stream is closed except: if tmp_path: - os.remove(tmp_path) + remove(tmp_path) raise # END assure tmpfile removal on error @@ -228,9 +229,25 @@ 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 + # Retry up to 14 times - exponential wait & retry in ms. + # The total maximum wait time is 1000ms, which should be vastly enough for the + # OS to return and commit the file to disk. + for exp_backoff_ms in [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 181]: + 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(exp_backoff_ms / 1000.0) + 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) From b71e2730c3dcab148816f0193a45550ef0a38c79 Mon Sep 17 00:00:00 2001 From: Jonathan DEKHTIAR Date: Sun, 29 Dec 2024 19:44:26 -0500 Subject: [PATCH 2/2] Update gitdb/db/loose.py Co-authored-by: Sebastian Thiel --- gitdb/db/loose.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/gitdb/db/loose.py b/gitdb/db/loose.py index ccefe40..03d387e 100644 --- a/gitdb/db/loose.py +++ b/gitdb/db/loose.py @@ -245,9 +245,6 @@ def store(self, istream): "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)