lfs: move the initialization of the upload request into the try block
authorMatt Harbison <matt_harbison@yahoo.com>
Tue, 21 Jan 2020 10:13:08 -0500
changeset 44141 46c8f15fb2b4
parent 44140 b2408acaa4c9
child 44142 63db6657d280
lfs: move the initialization of the upload request into the try block This (almost) guarantees that the file is closed in the case of an exception. The one hole is if the `seek(SEEK_END)`/`tell()`/`seek(0)` sequence fails. But that's going to go away when subclassing `httpconnection.httpsendfile` to fix the worker problem, so I'm not going to worry too much. (And that class appears to have the same problem.) Differential Revision: https://phab.mercurial-scm.org/D7959
hgext/lfs/blobstore.py
--- a/hgext/lfs/blobstore.py	Tue Jan 21 09:55:35 2020 -0500
+++ b/hgext/lfs/blobstore.py	Tue Jan 21 10:13:08 2020 -0500
@@ -490,15 +490,17 @@
                     _(b'detected corrupt lfs object: %s') % oid,
                     hint=_(b'run hg verify'),
                 )
-            request.data = lfsuploadfile(localstore.open(oid))
-            request.get_method = lambda: 'PUT'
-            request.add_header('Content-Type', 'application/octet-stream')
-            request.add_header('Content-Length', len(request.data))
 
         for k, v in headers:
             request.add_header(pycompat.strurl(k), pycompat.strurl(v))
 
         try:
+            if action == b'upload':
+                request.data = lfsuploadfile(localstore.open(oid))
+                request.get_method = lambda: 'PUT'
+                request.add_header('Content-Type', 'application/octet-stream')
+                request.add_header('Content-Length', len(request.data))
+
             with contextlib.closing(self.urlopener.open(request)) as res:
                 contentlength = res.info().get(b"content-length")
                 ui = self.ui  # Shorten debug lines