Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bdb07d

Browse files
committed
fix reportstats
1 parent 4e08a11 commit 3bdb07d

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

codersdk/agentsdk/agentsdk.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ func (c *Client) AuthAzureInstanceIdentity(ctx context.Context) (AuthenticateRes
371371
func (c *Client) ReportStats(ctx context.Context, log slog.Logger, statsChan <-chan *Stats, setInterval func(time.Duration)) (io.Closer, error) {
372372
var interval time.Duration
373373
ctx, cancel := context.WithCancel(ctx)
374+
exited := make(chan struct{})
374375

375376
postStat := func(stat *Stats) {
376377
var nextInterval time.Duration
@@ -387,7 +388,7 @@ func (c *Client) ReportStats(ctx context.Context, log slog.Logger, statsChan <-c
387388
break
388389
}
389390

390-
if interval != nextInterval {
391+
if nextInterval != 0 && interval != nextInterval {
391392
setInterval(nextInterval)
392393
}
393394
interval = nextInterval
@@ -397,6 +398,8 @@ func (c *Client) ReportStats(ctx context.Context, log slog.Logger, statsChan <-c
397398
postStat(&Stats{ConnsByProto: map[string]int64{}})
398399

399400
go func() {
401+
defer close(exited)
402+
400403
for {
401404
select {
402405
case <-ctx.Done():
@@ -413,6 +416,7 @@ func (c *Client) ReportStats(ctx context.Context, log slog.Logger, statsChan <-c
413416

414417
return closeFunc(func() error {
415418
cancel()
419+
<-exited
416420
return nil
417421
}), nil
418422
}

codersdk/workspaceagents_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,6 @@ func TestAgentReportStats(t *testing.T) {
9191
func() bool { return numReports.Load() >= 3 },
9292
testutil.WaitMedium, testutil.IntervalFast,
9393
)
94+
closeStream.Close()
9495
require.Equal(t, int64(1), numIntervalCalls.Load())
9596
}

0 commit comments

Comments
 (0)