@@ -93,6 +93,17 @@ func RedactResult(bundlePath string, input CollectorResult, additionalRedactors
9393 readerCloseFn = func () error { return nil } // No-op for in-memory data
9494 }
9595
96+ // Ensure the reader is eventually closed even on error paths.
97+ // This defer is guarded by setting readerCloseFn to nil after any explicit close
98+ // to prevent double-closing (notably when we must close before rewriting files on Windows).
99+ defer func () {
100+ if readerCloseFn != nil {
101+ if err := readerCloseFn (); err != nil {
102+ klog .Warningf ("Failed to close reader for %s: %v" , file , err )
103+ }
104+ }
105+ }()
106+
96107 // If the file is .tar, .tgz or .tar.gz, it must not be redacted. Instead it is
97108 // decompressed and each file inside the tar redacted and compressed back into the archive.
98109 if filepath .Ext (file ) == ".tar" || filepath .Ext (file ) == ".tgz" || strings .HasSuffix (file , ".tar.gz" ) {
@@ -109,12 +120,13 @@ func RedactResult(bundlePath string, input CollectorResult, additionalRedactors
109120 return
110121 }
111122
112- // Ensure the reader is closed after processing
123+ // Close the reader before we write back to the same file path (Windows safety)
113124 if err := readerCloseFn (); err != nil {
114125 klog .Warningf ("Failed to close reader for %s: %v" , file , err )
115126 errorCh <- errors .Wrap (err , "failed to close reader" )
116127 return
117128 }
129+ readerCloseFn = nil
118130
119131 err = RedactResult (tmpDir , subResult , additionalRedactors )
120132 if err != nil {
@@ -150,12 +162,13 @@ func RedactResult(bundlePath string, input CollectorResult, additionalRedactors
150162 return
151163 }
152164
153- // Close the reader now that we've consumed all the data
165+ // Close the reader now that we've consumed all the data (Windows safety)
154166 if err := readerCloseFn (); err != nil {
155167 klog .Warningf ("Failed to close reader for %s: %v" , file , err )
156168 errorCh <- errors .Wrap (err , "failed to close reader" )
157169 return
158170 }
171+ readerCloseFn = nil
159172
160173 // Now replace the file with the buffered redacted content
161174 err = input .ReplaceResult (bundlePath , file , & redactedBuf )
0 commit comments