From 7e10772da1094539c41069c4cf0fadd7096924e4 Mon Sep 17 00:00:00 2001 From: Nicolas Ruflin Date: Tue, 31 Jan 2017 16:37:13 +0100 Subject: [PATCH] Fix leaking go routine in docker stats fetching (#3492) Go routine on line 79 never stopped as queue was not closed. Fixes https://github.com/elastic/beats/issues/3489 (cherry picked from commit 3366bdb62fe6c603119601bb17b115c8f99bd1e6) --- CHANGELOG.asciidoc | 3 ++- metricbeat/module/docker/docker.go | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 25636f6f1de..fabd41b7619 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -8,7 +8,7 @@ // Template, add newest changes here === Beats version HEAD -https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff] +https://github.com/elastic/beats/compare/v5.2.0...master[Check the HEAD diff] ==== Breaking changes @@ -33,6 +33,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff] *Heartbeat* *Metricbeat* +- Fix go routine leak in docker module. {pull}3492[3492] *Packetbeat* diff --git a/metricbeat/module/docker/docker.go b/metricbeat/module/docker/docker.go index 67437eef1b8..79aa42988da 100644 --- a/metricbeat/module/docker/docker.go +++ b/metricbeat/module/docker/docker.go @@ -66,24 +66,26 @@ func FetchStats(client *docker.Client) ([]Stat, error) { var wg sync.WaitGroup - containersList := []Stat{} + containersList := make([]Stat, 0, len(containers)) queue := make(chan Stat, 1) wg.Add(len(containers)) for _, container := range containers { go func(container docker.APIContainers) { + defer wg.Done() queue <- exportContainerStats(client, &container) }(container) } go func() { - for container := range queue { - containersList = append(containersList, container) - wg.Done() - } + wg.Wait() + close(queue) }() - wg.Wait() + // This will break after the queue has been drained and queue is closed. + for container := range queue { + containersList = append(containersList, container) + } return containersList, err }