From dffb5620b3b02dedea8a63fd51ce9281a6911062 Mon Sep 17 00:00:00 2001 From: Grzegorz Zdunek Date: Wed, 4 Oct 2023 17:31:52 +0200 Subject: [PATCH] `removeSecure()` should close the file before removing it on Windows (#32948) * `removeSecure()` should close the file before removing it on Windows * Do not return early if `Close()` returns an error --- lib/utils/fs.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/utils/fs.go b/lib/utils/fs.go index 8d00de71114..5cb56a8f55c 100644 --- a/lib/utils/fs.go +++ b/lib/utils/fs.go @@ -323,7 +323,10 @@ func removeSecure(filePath string, fi os.FileInfo) error { } } } - return trace.ConvertSystemError(os.Remove(filePath)) + // The file should be closed before removing it on Windows. + closeErr := trace.ConvertSystemError(f.Close()) + removeErr := trace.ConvertSystemError(os.Remove(filePath)) + return trace.NewAggregate(closeErr, removeErr) } else { removeErr := os.Remove(filePath) if f != nil {