Skip to content

Commit

Permalink
[8.12](backport #37973) Fix file handle leak when handling errors in …
Browse files Browse the repository at this point in the history
…filestream (#37995)

* Fix file handle leak when handling errors in filestream (#37973)

Files were not closed properly in some rare error cases.

(cherry picked from commit f7e5b4c)

# Conflicts:
#	filebeat/input/filestream/input.go

---------

Co-authored-by: Denis <[email protected]>
  • Loading branch information
mergify[bot] and rdner committed Feb 13, 2024
1 parent d67bdbf commit 212b4f2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
*Filebeat*

- [threatintel] MISP pagination fixes {pull}37898[37898]
- Fix file handle leak when handling errors in filestream {pull}37973[37973]

*Heartbeat*

Expand Down
11 changes: 6 additions & 5 deletions filebeat/input/filestream/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func (inp *filestream) open(log *logp.Logger, canceler input.Canceler, fs fileSo
return nil, err
}

ok := false // used for cleanup
defer cleanup.IfNot(&ok, cleanup.IgnoreError(f.Close))

log.Debug("newLogFileReader with config.MaxBytes:", inp.readerConfig.MaxBytes)

// if the file is archived, it means that it is not going to be updated in the future
Expand All @@ -204,7 +207,6 @@ func (inp *filestream) open(log *logp.Logger, canceler input.Canceler, fs fileSo

dbgReader, err := debug.AppendReaders(logReader)
if err != nil {
f.Close()
return nil, err
}

Expand All @@ -222,7 +224,6 @@ func (inp *filestream) open(log *logp.Logger, canceler input.Canceler, fs fileSo
MaxBytes: encReaderMaxBytes,
})
if err != nil {
f.Close()
return nil, err
}

Expand All @@ -234,6 +235,7 @@ func (inp *filestream) open(log *logp.Logger, canceler input.Canceler, fs fileSo

r = readfile.NewLimitReader(r, inp.readerConfig.MaxBytes)

ok = true // no need to close the file
return r, nil
}

Expand All @@ -253,11 +255,11 @@ func (inp *filestream) openFile(log *logp.Logger, path string, offset int64) (*o
return nil, fmt.Errorf("failed to open file %s, named pipes are not supported", fi.Name())
}

ok := false
f, err := file.ReadOpen(path)
if err != nil {
return nil, fmt.Errorf("failed opening %s: %w", path, err)
}
ok := false
defer cleanup.IfNot(&ok, cleanup.IgnoreError(f.Close))

fi, err = f.Stat()
Expand All @@ -281,14 +283,13 @@ func (inp *filestream) openFile(log *logp.Logger, path string, offset int64) (*o

inp.encoding, err = inp.encodingFactory(f)
if err != nil {
f.Close()
if errors.Is(err, transform.ErrShortSrc) {
return nil, fmt.Errorf("initialising encoding for '%v' failed due to file being too short", f)
}
return nil, fmt.Errorf("initialising encoding for '%v' failed: %w", f, err)
}
ok = true

ok = true // no need to close the file
return f, nil
}

Expand Down

0 comments on commit 212b4f2

Please sign in to comment.