pullbundle: delay cache file opening
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 25 Sep 2018 12:53:34 +0200
changeset 4139 2bd652bece97
parent 4138 cfdc6f55599b
child 4140 9b71aa222f8e
pullbundle: delay cache file opening Otherwise we can end-up with a too many file open at the same time.
hgext3rd/pullbundle.py
--- a/hgext3rd/pullbundle.py	Tue Sep 25 12:20:26 2018 +0200
+++ b/hgext3rd/pullbundle.py	Tue Sep 25 12:53:34 2018 +0200
@@ -383,13 +383,17 @@
 def getcache(repo, bundlename):
     cdir = cachedir(repo)
     bundlepath = os.path.join(cdir, bundlename)
-    try:
-        fd = open(bundlepath, 'rb')
-        return util.filechunkiter(fd)
-    except IOError as exc:
-        if exc.errno != errno.ENOENT:
-            raise
+    if not os.path.exists(bundlepath):
         return None
+    # delay file opening as much as possible this introduce a small race
+    # condition if someone remove the file before we actually use it. However
+    # opening too many file will not work.
+
+    def data():
+        with open(bundlepath, 'rb') as fd:
+            for chunk in util.filechunkiter(fd):
+                yield chunk
+    return data()
 
 def cachewriter(repo, bundlename, stream):
     cdir = cachedir(repo)