Close streams and delete intermediate temps in ImageBuildpack#50919
Open
SebTardif wants to merge 1 commit into
Open
Close streams and delete intermediate temps in ImageBuildpack#50919SebTardif wants to merge 1 commit into
SebTardif wants to merge 1 commit into
Conversation
ExportedLayers left the intermediate create-builder-scratch-source temp file behind after rebased layer files were written, and opened layer InputStreams without closing them when StreamUtils.copy does not close either stream. Delete the source temp in a finally block and use try-with-resources for the apply path so layer files can be deleted reliably. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
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.
Summary
Close the layer
InputStreamand delete the intermediate source tar temp file inImageBuildpack.ExportedLayersso image buildpack exports do not leak file handles or leave orphaned files under the system temp directory.Problem
When resolving an image buildpack that is not already present in the builder,
ExportedLayersmaterializes each exported layer as temp files:Intermediate source tar never deleted.
createLayerFilewritescreate-builder-scratch-source-*, rewrites it intocreate-builder-scratch-*, then returns only the rebased file. The source temp is never deleted. Each OCI image buildpack export therefore leaves one orphaned source tar per layer injava.io.tmpdir.Layer stream not closed on apply.
applyopens the rebased layer withFiles.newInputStreamand copies withStreamUtils.copy, which does not close either stream. The open handle can preventFiles.delete(path)from succeeding on Windows and keeps a file descriptor open until GC.Sibling paths in the same module (
TarGzipBuildpack, the try-with-resources block earlier increateLayerFile) already close streams correctly.Failure scenario
bootBuildImage(or the buildpack platform API) resolves adocker://…buildpack that is not already in the builder.exportImageLayersruns andcreateLayerFilecreates both temp files per layer.create-builder-scratch-*file is tracked; thecreate-builder-scratch-source-*file remains on disk indefinitely.apply, the layer stream is left open after copy, which is unnecessary retention and can block deletion on Windows.Observable impact: gradual growth of large tar files under the temp directory during repeated image builds, plus possible failed temp cleanup.
Change
sourceTarFilein afinallyblock after the rebased layer file has been written (and its streams closed).InputStreaminapplybeforeStreamUtils.copy.Validation
Origin
9e409702807e, Phillip Webb, 2024-04-23, "Support gzip compressed image layers").7730eee43998, Scott Frederick, 2023-02-27, "Use image manifest when exporting layers").Related prior cleanup in the same module: #50639 (close
FileOutputStreamdelegate inInspectingOutputStream).Related
StreamUtils.copyintentionally leaves streams open for the caller (Spring Framework contract).