object_db: prevent FD leaks when writing blobs #13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When writing a blob, there are two places where we use temporary files. First, we use them when we write the blob itself. Second, we use them when we encode the buffer. In both of these cases, however, we failed to close the file after we were done with it, leading to file descriptor leaks until Go garbage-collected the items.
Close each of these files when we're done with them. Place the defer statements after the os.Remove statements, since defer statements are done in LIFO order and Windows will be happier if we don't try to remove a file we have open.
With this change, I don't see any of the lingering files I reported in the original issue; they're all routinely closed, and only the pack remains open.
Fixes git-lfs/git-lfs#3689
/cc @ttaylorr as contributor
/cc @larsxschneider as reporter