Skip to content

Commit

Permalink
Fix leaking go routine in docker stats fetching (#3492) (#3514)
Browse files Browse the repository at this point in the history
Go routine on line 79 never stopped as queue was not closed.

Fixes #3489
(cherry picked from commit 3366bdb)
  • Loading branch information
ruflin authored and tsg committed Feb 3, 2017
1 parent 537ae25 commit 5514964
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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*

Expand Down
14 changes: 8 additions & 6 deletions metricbeat/module/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 5514964

Please sign in to comment.