Skip to content

Commit afa3df0

Browse files
authored
Merge pull request #469 from ctrlaltdel121/master
stop tickers in defer to prevent leak
2 parents 90a74f2 + b868b7e commit afa3df0

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

internal/psutil.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ func WaitForProcessToExist(
3030
queryInterval time.Duration,
3131
matcher func(context.Context, *process.Process) (bool, error),
3232
) ([]*process.Process, error) {
33-
for range time.NewTicker(queryInterval).C {
33+
ticker := time.NewTicker(queryInterval)
34+
defer ticker.Stop()
35+
for range ticker.C {
3436
select {
3537
case <-ctx.Done():
3638
return nil, ctx.Err()
@@ -63,7 +65,9 @@ func WaitForProcessToExist(
6365
// WaitForPidToExit queries running processes every `queryInterval` duration, blocking until the provided pid is found
6466
// to not exist or the context is canceled.
6567
func WaitForPidToExit(ctx context.Context, queryInterval time.Duration, pid int32) error {
66-
for range time.NewTicker(queryInterval).C {
68+
ticker := time.NewTicker(queryInterval)
69+
defer ticker.Stop()
70+
for range ticker.C {
6771
select {
6872
case <-ctx.Done():
6973
return ctx.Err()
@@ -138,7 +142,9 @@ func sampleCPUTimes(ctx context.Context, sampleInterval time.Duration) <-chan *c
138142
defer close(returnCh)
139143

140144
var index int
141-
for range time.NewTicker(sampleInterval).C {
145+
ticker := time.NewTicker(sampleInterval)
146+
defer ticker.Stop()
147+
for range ticker.C {
142148
select {
143149
case <-ctx.Done():
144150
return

internal/vm/vsock.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ const (
3838
// path and port. It will retry connect attempts if a temporary error is encountered (up
3939
// to a fixed timeout) or the provided request is canceled.
4040
func VSockDial(ctx context.Context, logger *logrus.Entry, udsPath string, port uint32) (net.Conn, error) {
41-
tickerCh := time.NewTicker(vsockRetryInterval).C
41+
ticker := time.NewTicker(vsockRetryInterval)
42+
defer ticker.Stop()
43+
tickerCh := ticker.C
4244
var attemptCount int
4345
for {
4446
attemptCount++
@@ -92,7 +94,9 @@ func (l vsockListener) Accept() (net.Conn, error) {
9294
defer cancel()
9395

9496
var attemptCount int
95-
tickerCh := time.NewTicker(vsockRetryInterval).C
97+
ticker := time.NewTicker(vsockRetryInterval)
98+
defer ticker.Stop()
99+
tickerCh := ticker.C
96100
for {
97101
attemptCount++
98102
logger := l.logger.WithField("attempt", attemptCount)

0 commit comments

Comments
 (0)