-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Description
Hello, I found a potential bug when I used the project, I'm not sure I'm right, we can discuss the problem to avoid a potential trouble possibly.
Blocking position:
client_golang/prometheus/registry.go
Line 472 in 26e3055
wg.Wait() |
At line 449 call the wg.Add(goroutineBudget), and wg.Wait() is blocked until wg.Done() is called the number of goroutineBudget times to awaken the wg.Wait().
But if the select statement chooses the default path, return the function, maybe the wg.Done() has not executed enough times to awaken the wg.Wait(), which can result in a goroutine leak
client_golang/prometheus/registry.go
Lines 451 to 463 in 26e3055
collectWorker := func() { | |
for { | |
select { | |
case collector := <-checkedCollectors: | |
collector.Collect(checkedMetricChan) | |
case collector := <-uncheckedCollectors: | |
collector.Collect(uncheckedMetricChan) | |
default: | |
return | |
} | |
wg.Done() | |
} | |
} |
Complete codes of the part
client_golang/prometheus/registry.go
Lines 449 to 475 in 26e3055
wg.Add(goroutineBudget) | |
collectWorker := func() { | |
for { | |
select { | |
case collector := <-checkedCollectors: | |
collector.Collect(checkedMetricChan) | |
case collector := <-uncheckedCollectors: | |
collector.Collect(uncheckedMetricChan) | |
default: | |
return | |
} | |
wg.Done() | |
} | |
} | |
// Start the first worker now to make sure at least one is running. | |
go collectWorker() | |
goroutineBudget-- | |
// Close checkedMetricChan and uncheckedMetricChan once all collectors | |
// are collected. | |
go func() { | |
wg.Wait() | |
close(checkedMetricChan) | |
close(uncheckedMetricChan) | |
}() |
vianamjr