Преглед изворни кода

Properly close pack files on exceptions.

Cherry-picked from max0d41 @ GitHub.

Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
max пре 11 година
родитељ
комит
4bf10e80dd
1 измењених фајлова са 18 додато и 8 уклоњено
  1. 18 8
      dulwich/object_store.py

+ 18 - 8
dulwich/object_store.py

@@ -492,7 +492,15 @@ class DiskObjectStore(PackBasedObjectStore):
             raise
         pack_files.sort(reverse=True)
         suffix_len = len(".pack")
-        return [Pack(f[:-suffix_len]) for _, f in pack_files]
+        result = []
+        try:
+            for _, f in pack_files:
+                result.append(Pack(f[:-suffix_len]))
+        except:
+            for p in result:
+                p.close()
+            raise
+        return result
 
     def _pack_cache_stale(self):
         try:
@@ -617,15 +625,17 @@ class DiskObjectStore(PackBasedObjectStore):
         :param path: Path to the pack file.
         """
         p = PackData(path)
-        entries = p.sorted_entries()
-        basename = os.path.join(self.pack_dir,
-            "pack-%s" % iter_sha1(entry[0] for entry in entries))
-        f = GitFile(basename+".idx", "wb")
         try:
-            write_pack_index_v2(f, entries, p.get_stored_checksum())
+            entries = p.sorted_entries()
+            basename = os.path.join(self.pack_dir,
+                "pack-%s" % iter_sha1(entry[0] for entry in entries))
+            f = GitFile(basename+".idx", "wb")
+            try:
+                write_pack_index_v2(f, entries, p.get_stored_checksum())
+            finally:
+                f.close()
         finally:
-            f.close()
-        p.close()
+            p.close()
         os.rename(path, basename + ".pack")
         final_pack = Pack(basename)
         self._add_known_pack(final_pack)