Skip to content

Commit

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

Fixes elastic#3489
  • Loading branch information
ruflin authored and andrewkroh committed Jan 31, 2017
1 parent f7aa7b6 commit 3366bdb
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 3366bdb

Please sign in to comment.