Skip to content

fix upload retry 'file already closed' issue' - #4042

Merged
bk2204 merged 1 commit into
git-lfs:masterfrom
nataliechen1:master
Feb 26, 2020
Merged

bk2204 merged 1 commit into
git-lfs:masterfrom
nataliechen1:master

Conversation

@nataliechen1

Copy link
Copy Markdown
Contributor

In our infra, there is this peculiar situation where lfs upload request would be redirected to our lfs server.
Even though redirect requests are handled by git-lfs by rewinding request body and creating a new http request for retry, the underlying http module closes the file handle upon request completion.
So, retry would always fail with error like
`
< HTTP/1.1 307 Temporary Redirect
< Content-Length: 0
< Authorization: dontcare
< Content-Type: application/octet-stream
< Cookie: hadoop.auth="u=git-lfs&p=[email protected]&t=kerberos-dt&e=1582650048997&s=N9xsabgxNkzMjXH0lWKuzK8646A="
< Date: Tue, 25 Feb 2020 07:13:38 GMT
< Location: http://ubuntu:14000/webhdfs/v1/user/git-lfs/a9/e0/a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77/?op=CREATE&data=true&replication=1&delegation=IgAHZ2l0LWxmcwdnaXQtbGZzAIoBcHsl8X2KAXCfMnV9FRMUgHVxD2f3eu37cDeUJ2egsLnsNbAHd2ViaGRmcxMxOTIuMTY4LjAuMTA5OjE0MDAw
< Set-Cookie: hadoop.auth="u=git-lfs&p=[email protected]&t=kerberos-dt&e=1582650048997&s=N9xsabgxNkzMjXH0lWKuzK8646A="
<
15:13:38.674821 trace git-lfs: api: redirect PUT http://localhost:9080/plugins/git-lfs/lfs-test/objects/a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77 to http://ubuntu:14000/webhdfs/v1/user/git-lfs/a9/e0/a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77/
15:13:38.674846 trace git-lfs: HTTP: PUT http://ubuntu:14000/webhdfs/v1/user/git-lfs/a9/e0/a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77/

PUT /webhdfs/v1/user/git-lfs/a9/e0/a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77/?op=CREATE&data=true&replication=1&delegation=IgAHZ2l0LWxmcwdnaXQtbGZzAIoBcHsl8X2KAXCfMnV9FRMUgHVxD2f3eu37cDeUJ2egsLnsNbAHd2ViaGRmcxMxOTIuMTY4LjAuMTA5OjE0MDAw HTTP/1.1
Host: ubuntu:14000
Content-Length: 3000
Content-Type: application/octet-stream
User-Agent: git-lfs/2.10.0 (GitHub; linux amd64; go 1.12.6; git 043dddc)

15:13:38.678552 trace git-lfs: xfer: adapter "basic" worker 0 finished job for "a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77"
15:13:38.678760 trace git-lfs: tq: retrying object a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77: LFS: Put http://ubuntu:14000/webhdfs/v1/user/git-lfs/a9/e0/a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77/?op=CREATE&data=true&replication=1&delegation=IgAHZ2l0LWxmcwdnaXQtbGZzAIoBcHsl8X2KAXCfMnV9FRMUgHVxD2f3eu37cDeUJ2egsLnsNbAHd2ViaGRmcxMxOTIuMTY4LjAuMTA5OjE0MDAw: read /projects/temp/lfs-test/.git/lfs/objects/a9/e0/a9e064317304049e503f0a7906d573695d4cbf7c89e7b2059117e72eae3fac77: file already closed

`

So, this fix is to intercept the close of the file handle and let whoever directly open the file closes it.

@bk2204 bk2204 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

This looks like a great fix. Is it possible we could get a test for it? We can probably adjust t/cmd/lfstest-gitserver.go to handle this particular case and then write a test in one of the t/*.sh files.

Comment thread tq/basic_upload.go
}

cbr := tools.NewBodyWithCallback(f, t.Size, ccb)
cbr := tools.NewFileBodyWithCallback(f, t.Size, ccb)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems sane. We defer f.Close() up above, so we won't leak the file descriptor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added.

@bk2204 bk2204 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks again for the patch.

@bk2204
bk2204 merged commit fa9649c into git-lfs:master Feb 26, 2020
chrisd8088 added a commit that referenced this pull request Aug 5, 2026
In commit d7d9fd7 of PR #4042 we
introduced the "closingFileReader" structure to our "tools" package,
which wraps a File structure from the Go standard library's "os"
package but replaces its Close() method with a no-op stub method.

In the same commit we also added the NewFileBody() and
NewFileBodyWithCallback() factory functions, the latter of which
calls the former and then passes the "closingFileReader" structure
it instantiates to the NewBodyWithCallback() function.  We then
call the NewFileBodyWithCallback() function in both our SSH and
HTTP upload transfer adapters in instances where we expect the
underlying File structure's Close() method to be called later
than the Close() method of the "closingFileReader" structure.

In the case of our HTTP upload transfer adapter in particular,
when the upload request for an individual object is redirected to
another URL, the Go standard library's "net/http" package will
invoke the Close() method on the Body field of the current Request
structure after the initial request is complete.  However, we do
not want this call to cause the underlying file resource to be
closed as well, so we need to provide a stub Close() method which
takes no action when it is called by the HTTP transport mechanism.

To verify that the changes in PR #4042 were effective, in commit
d7d9fd7 we also implemented a new
"redirect upload" test in our "t/t-upload-redirect.sh" shell test
script, which checks that when the Git LFS client receives an HTTP
redirection in response to an object upload request, the client is
able to make a complete and successful object transfer to the new
URL provided in the initial response.

However, we did not also define a Go test function which exercises
the "closingFileReader" structure and related factory functions.
Because we expect to rename this structure and the NewFileBody()
function in a subsequent commit in this PR, we now add a new
TestFileBodyCallbackReaderIgnoresClose() test function which verifies
that the structure's Close() method works as expected.  In particular,
it invokes the Close() method and confirms that the underlying file
resource is not closed, and that additional read operations on the
file resource will still succeed.
chrisd8088 added a commit that referenced this pull request Aug 5, 2026
In commit d7d9fd7 of PR #4042 we
introduced the "closingFileReader" structure to our "tools" package,
which wraps a File structure from the Go standard library's "os"
package but replaces its Close() method with a no-op stub method.
In the same commit we also added the NewFileBody() factory function,
which initializes a new "closingFileReader" structure.

The "closingFileReader" structure and the NewFileBody() function were
modelled on the "closingByteReader" structure and NewByteBody()
function which were already present in our "tools/copycallback.go"
source file at the time when PR #4042 was developed.  However, the
two structures and their methods serve different purposes.

The "closingByteReader" structure provides a no-op Close() method
so that we can use the structure returned by the NewReader() function
of the Go standard library's "bytes" package in contexts that require
a structure that implements the ReadSeekCloser interface from the
standard library's "io" package.  Since the underlying byte slice
cannot be closed, the Close() method we provide is necessarily just
a no-op.

By contrast, the "closingFileReader" structure's underlying resource
is a File structure from the Go standard library's "os" package,
which does provide a Close() method of its own.  The reason we
substitute this method with a no-op Close() method is so that we
can use the "closingFileReader" structure in instances where we
expect the underlying File structure's Close() method to be called
later than the Close() method of the "closingFileReader" structure.

In the case of our HTTP upload transfer adapter in particular,
when the upload request for an individual object is redirected to
another URL, the Go standard library's "net/http" package will
invoke the Close() method on the Body field of the current Request
structure after the initial request is complete.  However, we do
not want this call to cause the underlying file resource to be
closed as well, so we need to provide a stub Close() method which
takes no action when it is called by the HTTP transport mechanism.

Thus while the "closingByteReader" structure provided a way to
"close" a byte slice (although this operation is not actually
possible), the "closingFileReader" structure provides a way to
avoid "closing" a file resource.

In a prior commit in this PR we renamed the "closingByteReader"
structure to ClosingByteReader in order to export it for later use
in several of our packages, and we also renamed the NewByteBody()
function to NewClosingByteReader().

For greater clarity as to the purpose of the "closingFileReader"
structure, we now rename it to "nopClosingFile", which more clearly
indicates that the structure just wraps a File structure and provides
a no-op Close() method.  For consistency, we also rename the
NewFileBody() function to newNopClosingFile().  As there is only
one caller of this function, the NewFileBodyWithCallback() function,
this contains the sole reference we need to update to reflect the
revised newNopClosingFile() name.

Fortunately, we already have a test function which demonstrates that
these changes have not introduced a regression.  In a previous commit
in this PR we introduced the TestFileBodyCallbackReaderIgnoresClose()
function, which verifies that when the Close() method is called on
the structure returned by the NewFileBodyWithCallback() function,
it does not actually close the underlying file resource.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants