fix(inputs.directory_monitor): Close input file before removal (#12294)

This commit is contained in:
Sven Rebhan 2022-11-29 14:06:55 +01:00 committed by GitHub
parent 54f4a3a38b
commit c31f8116ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 3 deletions

View File

@ -335,7 +335,6 @@ func (monitor *DirectoryMonitor) moveFile(srcPath string, dstBaseDir string) {
if err != nil {
monitor.Log.Errorf("Could not open input file: %s", err)
}
defer inputFile.Close()
outputFile, err := os.Create(dstPath)
if err != nil {
@ -348,8 +347,14 @@ func (monitor *DirectoryMonitor) moveFile(srcPath string, dstBaseDir string) {
monitor.Log.Errorf("Writing to output file failed: %s", err)
}
err = os.Remove(srcPath)
if err != nil {
// We need to close the file for remove on Windows as we otherwise
// will run into a "being used by another process" error
// (see https://github.com/influxdata/telegraf/issues/12287)
if err := inputFile.Close(); err != nil {
monitor.Log.Errorf("Could not close input file: %s", err)
}
if err := os.Remove(srcPath); err != nil {
monitor.Log.Errorf("Failed removing original file: %s", err)
}
}