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 leaks in filebeat log harvester #6809

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 12 additions & 5 deletions filebeat/channel/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ func SubOutlet(out Outleter) Outleter {
res: make(chan bool, 1),
}

go func() {
for event := range s.ch {
s.res <- out.OnEvent(event)
}
}()
go s.drainLoop(out)

return s
}

func (o *subOutlet) drainLoop(out Outleter) {
Copy link

Choose a reason for hiding this comment

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

maybe name it workerLoop? This loop is forwarding all received events to out.OnEvent.

for {
select {
case <-o.done:
return
case event := <-o.ch:
o.res <- out.OnEvent(event)
}
}
Copy link

Choose a reason for hiding this comment

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

This change is dangerous, as returning early on o.done might prevent the queue from being drained. Not draining o.ch can lead to a deadlock on shutdown. For not leaking the go-routine, o.ch must be closed properly.

}

func (o *subOutlet) Close() error {
isOpen := o.isOpen.Swap(false)
if isOpen {
Expand Down
2 changes: 2 additions & 0 deletions filebeat/input/log/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (h *Harvester) ID() uuid.UUID {
func (h *Harvester) Setup() error {
err := h.open()
if err != nil {
h.stop()
return fmt.Errorf("Harvester setup failed. Unexpected file opening error: %s", err)
}

Expand All @@ -152,6 +153,7 @@ func (h *Harvester) Setup() error {
if h.source != nil {
h.source.Close()
}
h.stop()
return fmt.Errorf("Harvester setup failed. Unexpected encoding line reader error: %s", err)
}

Expand Down