Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix goroutine leak on initialization failures of log input #12125

Merged
merged 4 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add missing Kubernetes metadata fields to Filebeat CoreDNS module, and fix a documentation error. {pull}11591[11591]
- Reduce memory usage if long lines are truncated to fit `max_bytes` limit. The line buffer is copied into a smaller buffer now. This allows the runtime to release unused memory earlier. {pull}11524[11524]
- Fix memory leak in Filebeat pipeline acker. {pull}12063[12063]
- Fix goroutine leak caused on initialization failures of log input. {pull}12125[12125]

*Heartbeat*

Expand Down
12 changes: 12 additions & 0 deletions filebeat/input/log/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func NewInput(
outlet channel.Connector,
context input.Context,
) (input.Input, error) {
cleanupNeeded := true

// Note: underlying output.
// The input and harvester do have different requirements
Expand All @@ -87,11 +88,21 @@ func NewInput(
if err != nil {
return nil, err
}
defer func() {
if cleanupNeeded {
out.Close()
}
}()

// stateOut will only be unblocked if the beat is shut down.
// otherwise it can block on a full publisher pipeline, so state updates
// can be forwarded correctly to the registrar.
stateOut := channel.CloseOnSignal(channel.SubOutlet(out), context.BeatDone)
defer func() {
if cleanupNeeded {
stateOut.Close()
}
}()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In go-txfile we have a package for this :)
https://github.com/elastic/go-txfile/blob/master/internal/cleanup/cleanup.go

it's used like this:

ok := false
defer cleanup.IfNot(&ok, func() { 
  ...
})

...

ok = true
return // something

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reordered the code so this is not needed, tell me what you think. The only risk is that the harvester created to check the config will have nil outleters, but they shouldn't be used on creation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@urso cleanup is under internal and then it cannot be imported from beats, I have added a similar convenience helper.


meta := context.Meta
if len(meta) == 0 {
Expand Down Expand Up @@ -137,6 +148,7 @@ func NewInput(

logp.Info("Configured paths: %v", p.config.Paths)

cleanupNeeded = false
return p, nil
}

Expand Down