# HG changeset patch # User Matt Harbison # Date 1579619588 18000 # Node ID 46c8f15fb2b4c67848fc2319b48c47a299904a12 # Parent b2408acaa4c93caa3d3d04b236e833a93b363ecd 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 diff -r b2408acaa4c9 -r 46c8f15fb2b4 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