Skip to content

Always close open files when cloning and spooling - #5617

Merged
chrisd8088 merged 4 commits into
git-lfs:mainfrom
chrisd8088:always-defer-close-tmp
Jan 16, 2024
Merged

chrisd8088 merged 4 commits into
git-lfs:mainfrom
chrisd8088:always-defer-close-tmp

Conversation

@chrisd8088

@chrisd8088 chrisd8088 commented Jan 15, 2024 •

Copy link
Copy Markdown
Member

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 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.

Prompted by that fix, this PR makes a number of similar changes:

  1. 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.)

  2. 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() 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. (For Linux its CloneFileByPath() function doesn't close the files it opens, at present, so we now ensure that it does so.)

  3. We 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.

    As well, since the tools.Spool() function's use of a temporary file when spooling is not tested by the t/t-smudge.sh test suite, 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, since that is the size of the default internal memory buffer used by the Spool() function.

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
chrisd8088 force-pushed the always-defer-close-tmp branch from fdface9 to b2e6884 Compare January 15, 2024 18:00
@chrisd8088
chrisd8088 marked this pull request as ready for review January 15, 2024 19:02
@chrisd8088
chrisd8088 requested a review from a team as a code owner 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
chrisd8088 force-pushed the always-defer-close-tmp branch from b2e6884 to 01e28e1 Compare January 16, 2024 03:27
@chrisd8088
chrisd8088 merged commit a7dc910 into git-lfs:main Jan 16, 2024
@chrisd8088
chrisd8088 deleted the always-defer-close-tmp branch January 16, 2024 17:36
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.
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