Skip to content

Commit

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

* 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 14, 2024
1 parent b7309bd commit 8f17069
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 @@ -39,6 +39,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
*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 @@ -173,6 +173,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 @@ -197,7 +200,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 @@ -215,7 +217,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 @@ -227,6 +228,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 @@ -246,11 +248,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 @@ -274,14 +276,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 8f17069

Please sign in to comment.