Skip to content

Commit

Permalink
Fix leaking go routine in docker stats fetching
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
  • Loading branch information
ruflin committed Jan 31, 2017
1 parent c4603f8 commit 0f758b4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]
- Kafka module case sensitive host name matching. {pull}3193[3193]
- Fix interface conversion panic in couchbase module {pull}3272[3272]
- Fix overwriting explicit empty config sections {issue}2918[2918]
- 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 0f758b4

Please sign in to comment.