Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leaking go routine in docker stats fetching #3492

Merged
merged 1 commit into from
Jan 31, 2017

Conversation

ruflin
Copy link
Member

@ruflin ruflin commented Jan 30, 2017

See #3489

@@ -84,6 +84,7 @@ func FetchStats(client *docker.Client) ([]Stat, error) {
}()

wg.Wait()
close(queue)
Copy link
Member

@andrewkroh andrewkroh Jan 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider swapping things around so that you can call wg.Done() from the worker goroutines. This more closely aligns with the Fan-in, Fan-out examples here.

 	for _, container := range containers {
 		go func(container docker.APIContainers) {
			defer wg.Done()
 			queue <- exportContainerStats(client, &container)
 		}(container)
 	}

 	go func() {
  	  	wg.Wait()
  	  	close(queue)
  	}()
  
  	// This will break after the queue has been drained and queue is closed.
 	for container := range queue {
 		containersList = append(containersList, container)
 	}

Copy link
Member

@andrewkroh andrewkroh Jan 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And up on line 69, consider changing it to containersList = make([]Stat, 0, len(containers)) since we know ahead of time how much capacity the slice needs. This will reduce the number of allocations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitively makes the code much cleaner.

Go routine on line 79 never stopped as queue was not closed.

Fixes elastic#3489
@ruflin ruflin force-pushed the goroutine-leaking branch from 1efb268 to 0f758b4 Compare January 31, 2017 10:50
@ruflin ruflin added the needs_backport PR is waiting to be backported to other branches. label Jan 31, 2017
@andrewkroh andrewkroh merged commit 3366bdb into elastic:master Jan 31, 2017
ruflin added a commit to ruflin/beats that referenced this pull request Feb 3, 2017
Go routine on line 79 never stopped as queue was not closed.

Fixes elastic#3489
(cherry picked from commit 3366bdb)
tsg pushed a commit that referenced this pull request Feb 3, 2017
Go routine on line 79 never stopped as queue was not closed.

Fixes #3489
(cherry picked from commit 3366bdb)
@tsg tsg removed the needs_backport PR is waiting to be backported to other branches. label Mar 1, 2017
leweafan pushed a commit to leweafan/beats that referenced this pull request Apr 28, 2023
…ic#3514)

Go routine on line 79 never stopped as queue was not closed.

Fixes elastic#3489
(cherry picked from commit 4e615a7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants