changeset 51941:7032da075572

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.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 24 Sep 2024 00:16:04 +0200
parents 9f7cf869e9f4
children c564be351754
files mercurial/branching/rev_cache.py
diffstat 1 files changed, 23 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- 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):