Repository navigation
Always close open files when cloning and spooling - #5617
Merged
Merged
Conversation
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, the Windows variant of our tools.CloneFileByPath() function opens both the source and destination file paths provided for a "clone" operation but does not close the resultant file descriptors, so we now add deferred calls to ensure we always close both files. (This minor issue dates from the introduction in commit e55bc4c of the support for the "file clone" operation on ReFS in PR git-lfs#3790.)
chrisd8088
force-pushed
the
always-defer-close-tmp
branch
from
January 15, 2024 18:00
fdface9 to
b2e6884
Compare
chrisd8088
marked this pull request as ready for review
January 15, 2024 19:02
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. Although this issue does not affect non-Windows systems, as they generally permit files to be deleted before all open descriptors for the files are closed, we can keep our implementations aligned and consistent if we also make sure to close any files we open in the Linux and macOS variants of our tools.CheckCloneFileSupported() and tools.CloneFileByPath() functions. Note that on macOS, the CheckCloneFileSupported() function creates two temporary files and then only passes them by name to the CloneFileByPath() function, so we can close them immediately after they are opened. This is because the tools.CloneFile() function is not supported on macOS. By contrast, on Linux, the CheckCloneFileSupported() function uses CloneFile(), so it must defer closing the temporary files it creates rather than close them directly after opening them.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, we also update our tools.Spool() function (which is used in the "git lfs smudge" command to handle non-pointer data) to ensure that if it creates a temporary file into which it spools data, it always closes that file before trying to remove it. While this should have no advantage on non-Windows systems which allow for files to be deleted while there are still open file descriptors for them, on Windows this change should mean we never fail to clean up our temporary spool file, if we create one.
In a prior commit in this PR we updated the tools.Spool() function, which is used only by the "git lfs smudge" command and only when it must pass through non-pointer data. When this data is longer than the default internal memory buffer of 1024 bytes, the Spool() function creates a temporary file to buffer the data, and then defers an os.Remove() call to delete the file when the operation is complete. At present this use of a temporary file when spooling is not tested by the t/t-smudge.sh test suite, so we add a check in the existing "smudge with invalid pointer" test which forces the use of such a file by sending more than 1024 bytes of non-pointer data.
chrisd8088
force-pushed
the
always-defer-close-tmp
branch
from
January 16, 2024 03:27
b2e6884 to
01e28e1
Compare
bk2204
approved these changes
Jan 16, 2024
chrisd8088
added a commit
that referenced
this pull request
Jun 24, 2026
In commit 01e28e1 of PR #5617 we added a check to the "smudge with invalid pointer" test in our "t/t-smudge.sh" shell script which exercises our "git lfs smudge" filter command with invalid pointer data longer than the size of the internal memory buffer used by the Spool() function in our "tools" package. Under these conditions, the Spool() function should create a temporary file into which it can spool the additional non-pointer data. However, if we change the size of this internal buffer in the future, we will need to remember to adjust the "smudge with invalid pointer" test, or else its check of the "smudge" filter's use of a temporary file may silently become ineffective. In fact, we already have a test in our "t/t-clean.sh" shell script which no longer creates test data of the appropriate size because its initial version was written at a time when the size of the read buffer used to decode Git LFS pointers was only 512 bytes, and the test was never revised after the buffer size was increased to 1024 bytes. (We will document the history of this test's development in a subsequent commit in this PR, at which time we will also update the test.) In order to help avoid these types of issues with our shell tests, in a previous commit in this PR we revised our "lfs" package so that the constant which sets the size of our pointer read buffer is now exported with the name BlobSizeCutoff. We now make a similar update to our "tools" package by revising the name of the constant which sets the size of the buffer used by the Spool() function from "memoryBufferLimit" to MemoryBufferLimit, which allows the constant to be exported outside of the package. In a subsequent commit in this PR we will then be able to add a Go helper program to our test suite which outputs the value of this constant. This will allow us to also adjust the "smudge with invalid pointer" test so it no longer creates data of a size set by a hard-coded value but instead scales the length of the data to the current value of the MemoryBufferLimit constant. Note that we also correct one code comment where we inadvertently used mismatched delimiters around a literal term.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In commit e861b79 of PR #5595 the Windows variant of our
tools.CheckCloneFileSupported()function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by thegit lfs dedupcommand) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call toos.Remove()might not succeed.Prompted by that fix, this PR makes a number of similar changes:
The Windows variant of our
tools.CloneFileByPath()function opens both the source and destination file paths provided for a "clone" operation but does not close the resultant file descriptors, so we now add deferred calls to ensure we always close both files. (This minor issue dates from the introduction in commit e55bc4c of the support for the "file clone" operation on ReFS in PR #3790.)Although non-Windows systems generally permit files to be deleted before all open descriptors for the files are closed, we can keep our implementations aligned and consistent if we also make sure to close any files we open in the Linux and macOS variants of our
tools.CheckCloneFileSupported()andtools.CloneFileByPath()functions.Note that on macOS, the
CheckCloneFileSupported()function creates two temporary files and then only passes them by name to theCloneFileByPath()function, so we can close them immediately after they are opened. This is because thetools.CloneFile()function is not supported on macOS. By contrast, on Linux, theCheckCloneFileSupported()function usesCloneFile(), so it must defer closing the temporary files it creates rather than close them directly after opening them. (For Linux itsCloneFileByPath()function doesn't close the files it opens, at present, so we now ensure that it does so.)We update our
tools.Spool()function (which is used in thegit lfs smudgecommand to handle non-pointer data) to ensure that if it creates a temporary file into which it spools data, it always closes that file before trying to remove it. While this should have no advantage on non-Windows systems which allow for files to be deleted while there are still open file descriptors for them, on Windows this change should mean we never fail to clean up our temporary spool file, if we create one.As well, since the
tools.Spool()function's use of a temporary file when spooling is not tested by thet/t-smudge.shtest suite, we add a check in the existingsmudge with invalid pointertest which forces the use of such a file by sending more than 1024 bytes of non-pointer data, since that is the size of the default internal memory buffer used by theSpool()function.