From c0bc5142001eda39fbcf9d8a7b78f68a32ad2cd9 Mon Sep 17 00:00:00 2001 From: Adrian Serrano Date: Mon, 9 Apr 2018 18:40:06 +0200 Subject: [PATCH 1/2] Filebeat: Fix goroutine leak in channel.SubOutlet An internal goroutine wasn't stopped when Close() was called. --- filebeat/channel/util.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/filebeat/channel/util.go b/filebeat/channel/util.go index d0806011722..c54c32d0e15 100644 --- a/filebeat/channel/util.go +++ b/filebeat/channel/util.go @@ -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) { + for { + select { + case <-o.done: + return + case event := <-o.ch: + o.res <- out.OnEvent(event) + } + } +} + func (o *subOutlet) Close() error { isOpen := o.isOpen.Swap(false) if isOpen { From 6105f001c4fca38117a842649db3220a1c819c68 Mon Sep 17 00:00:00 2001 From: Adrian Serrano Date: Mon, 9 Apr 2018 18:29:59 +0200 Subject: [PATCH 2/2] Filebeat: Fix leak in log harvester Harvester relied on Stop() being called to free resources allocated during construction. In the event of Setup() failing for some reason, the Start() / Stop() sequence is never invoked and this resources are never released. --- filebeat/input/log/harvester.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/filebeat/input/log/harvester.go b/filebeat/input/log/harvester.go index 48a48c16cb3..cca5f6000a0 100644 --- a/filebeat/input/log/harvester.go +++ b/filebeat/input/log/harvester.go @@ -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) } @@ -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) }