Skip to content

Commit 244a915

Browse files
authored
fix: close temp file handle before removal (#3672)
* fix(dind): close temp file handle before removal On some OS (such as Windows), keeping a file descriptor open prevents you from deleting the file. * fix(k3s): close temp file handle before removal On some OS (such as Windows), keeping a file descriptor open prevents you from deleting the file.
1 parent 0c15e97 commit 244a915

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

modules/dind/dind.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func (c *Container) LoadImage(ctx context.Context, image string) (err error) {
8181
if err != nil {
8282
return fmt.Errorf("create temporary images file: %w", err)
8383
}
84+
// Close the file handle immediately: SaveImages and CopyFileToContainer
85+
// open the file by name.
86+
if err = imagesTar.Close(); err != nil {
87+
return fmt.Errorf("close temporary images file: %w", err)
88+
}
8489
defer func() {
8590
err = errors.Join(err, os.Remove(imagesTar.Name()))
8691
}()

modules/k3s/k3s.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ func (c *K3sContainer) LoadImagesWithOpts(ctx context.Context, images []string,
193193
if err != nil {
194194
return fmt.Errorf("creating temporary images file %w", err)
195195
}
196+
// Close the file handle immediately: SaveImages and CopyFileToContainer
197+
// open the file by name.
198+
if err = imagesTar.Close(); err != nil {
199+
return fmt.Errorf("close temporary images file: %w", err)
200+
}
196201
defer func() {
197202
_ = os.Remove(imagesTar.Name())
198203
}()

0 commit comments

Comments
 (0)