rev-branch-cache: make sure we close the name file we open
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 24 Sep 2024 00:16:04 +0200
changeset 51941 7032da075572
parent 51940 9f7cf869e9f4
child 51942 c564be351754
rev-branch-cache: make sure we close the name file we open We were various opening without with or try. Adding a try would not hurt.
mercurial/branching/rev_cache.py
--- a/mercurial/branching/rev_cache.py	Mon Sep 23 23:52:45 2024 +0200
+++ b/mercurial/branching/rev_cache.py	Tue Sep 24 00:16:04 2024 +0200
@@ -316,27 +316,30 @@
 
     def _writenames(self, repo):
         """write the new branch names to revbranchcache"""
-        if self._rbcnamescount != 0:
-            f = repo.cachevfs.open(_rbcnames, b'ab')
-            if f.tell() == self._rbcsnameslen:
-                f.write(b'\0')
-            else:
+        f = None
+        try:
+            if self._rbcnamescount != 0:
+                f = repo.cachevfs.open(_rbcnames, b'ab')
+                if f.tell() == self._rbcsnameslen:
+                    f.write(b'\0')
+                else:
+                    f.close()
+                    f = None
+                    repo.ui.debug(b"%s changed - rewriting it\n" % _rbcnames)
+                    self._rbcnamescount = 0
+                    self._rbcrevslen = 0
+            if self._rbcnamescount == 0:
+                # before rewriting names, make sure references are removed
+                repo.cachevfs.unlinkpath(_rbcrevs, ignoremissing=True)
+                f = repo.cachevfs.open(_rbcnames, b'wb')
+            names = self._names[self._rbcnamescount :]
+            from_local = encoding.fromlocal
+            data = b'\0'.join(from_local(b) for b in names)
+            f.write(data)
+            self._rbcsnameslen = f.tell()
+        finally:
+            if f is not None:
                 f.close()
-                repo.ui.debug(b"%s changed - rewriting it\n" % _rbcnames)
-                self._rbcnamescount = 0
-                self._rbcrevslen = 0
-        if self._rbcnamescount == 0:
-            # before rewriting names, make sure references are removed
-            repo.cachevfs.unlinkpath(_rbcrevs, ignoremissing=True)
-            f = repo.cachevfs.open(_rbcnames, b'wb')
-        f.write(
-            b'\0'.join(
-                encoding.fromlocal(b)
-                for b in self._names[self._rbcnamescount :]
-            )
-        )
-        self._rbcsnameslen = f.tell()
-        f.close()
         self._rbcnamescount = len(self._names)
 
     def _writerevs(self, repo, start):