Skip to content

Commit

Permalink
Fix file handle leak when handling errors in filestream (#37973)
Browse files Browse the repository at this point in the history
Files were not closed properly in some rare error cases.

(cherry picked from commit f7e5b4c)

# Conflicts:
#	filebeat/input/filestream/input.go
  • Loading branch information
rdner authored and mergify[bot] committed Feb 13, 2024
1 parent d67bdbf commit 1ead383
Show file tree
Hide file tree
Showing 2 changed files with 11 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
15 changes: 10 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,15 +283,18 @@ 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

<<<<<<< HEAD

Check failure on line 292 in filebeat/input/filestream/input.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

expected statement, found '<<' (typecheck)
return f, nil
=======

Check failure on line 294 in filebeat/input/filestream/input.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

expected statement, found '==' (typecheck)
ok = true // no need to close the file
return f, encoding, nil
>>>>>>> f7e5b4c41f (Fix file handle leak when handling errors in filestream (#37973))

Check failure on line 297 in filebeat/input/filestream/input.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

expected statement, found '>>' (typecheck)
}

func checkFileBeforeOpening(fi os.FileInfo) error {
Expand Down

0 comments on commit 1ead383

Please sign in to comment.