Skip to content

Commit aca6768

Browse files
authored
Merge pull request kubernetes-csi#1145 from rhrmo/master
Fix scheduleOpsInFlightMetric go routine leak
2 parents 7309e42 + e2b8496 commit aca6768

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

pkg/metrics/metrics.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package metrics
1818

1919
import (
20+
"context"
2021
"net/http"
2122
"sync"
2223
"time"
@@ -290,16 +291,25 @@ func (opMgr *operationMetricsManager) init() {
290291
// While we always maintain the number of operations in flight
291292
// for every metrics operation start/finish, if any are leaked,
292293
// this scheduled routine will catch any leaked operations.
293-
go opMgr.scheduleOpsInFlightMetric()
294+
ctx, cancel := context.WithCancel(context.Background())
295+
defer cancel()
296+
go opMgr.scheduleOpsInFlightMetric(ctx)
294297
}
295298

296-
func (opMgr *operationMetricsManager) scheduleOpsInFlightMetric() {
297-
for range time.Tick(inFlightCheckInterval) {
298-
func() {
299-
opMgr.mu.Lock()
300-
defer opMgr.mu.Unlock()
301-
opMgr.opInFlight.Set(float64(len(opMgr.cache)))
302-
}()
299+
func (opMgr *operationMetricsManager) scheduleOpsInFlightMetric(ctx context.Context) {
300+
for {
301+
select {
302+
case <-ctx.Done():
303+
return
304+
default:
305+
for range time.NewTicker(inFlightCheckInterval).C {
306+
func() {
307+
opMgr.mu.Lock()
308+
defer opMgr.mu.Unlock()
309+
opMgr.opInFlight.Set(float64(len(opMgr.cache)))
310+
}()
311+
}
312+
}
303313
}
304314
}
305315

0 commit comments

Comments
 (0)