daemon.cleanupMetricsPlugins(): fix

A linter (vet) found the following bug in the code:

> daemon/metrics.go:124::error: range variable p captured by func literal (vet)

Here a variable p is used in an async fashion by goroutine, and most
probably by the time of use it is set to the last element of a range.

For example, the following code

```go
	for _, c := range []string{"here ", "we ", "go"} {
		go func() {
			fmt.Print(c)
		}()
	}
```

will print `gogogo` rather than `here we go` as one would expect.

Fixes: 0e8e8f0f31 ("Add support for metrics plugins")
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2018-01-16 14:51:36 -08:00
parent be14665210
commit 9db2c62488

View file

@ -118,7 +118,8 @@ func (d *Daemon) cleanupMetricsPlugins() {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(len(ls)) wg.Add(len(ls))
for _, p := range ls { for _, plugin := range ls {
p := plugin
go func() { go func() {
defer wg.Done() defer wg.Done()
pluginStopMetricsCollection(p) pluginStopMetricsCollection(p)