Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion go/pools/refresh_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type (
refreshTicker *time.Ticker
refreshStop chan struct{}
refreshWg sync.WaitGroup
mu sync.Mutex

pool refreshPool
}
Expand Down Expand Up @@ -64,6 +65,9 @@ func (pr *poolRefresh) startRefreshTicker() {
if pr == nil {
return
}
pr.mu.Lock()
defer pr.mu.Unlock()

Copy link

Copilot AI Dec 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The startRefreshTicker() method doesn't check if a ticker is already running before creating a new one. This can lead to resource leaks (goroutines and tickers not being cleaned up) if startRefreshTicker() is called multiple times. Consider adding a check after acquiring the lock:

pr.mu.Lock()
defer pr.mu.Unlock()

if pr.refreshTicker != nil {
    return // ticker already running
}

pr.refreshTicker = time.NewTicker(pr.refreshInterval)
...
Suggested change
if pr.refreshTicker != nil {
return // ticker already running
}

Copilot uses AI. Check for mistakes.
pr.refreshTicker = time.NewTicker(pr.refreshInterval)
pr.refreshStop = make(chan struct{})
pr.refreshWg.Add(1)
Expand All @@ -88,10 +92,16 @@ func (pr *poolRefresh) startRefreshTicker() {
}

func (pr *poolRefresh) stop() {
if pr == nil || pr.refreshTicker == nil {
if pr == nil {
return
}
pr.mu.Lock()
if pr.refreshTicker == nil {
pr.mu.Unlock()
return
}
pr.refreshTicker.Stop()
close(pr.refreshStop)
pr.mu.Unlock()
pr.refreshWg.Wait()
}
6 changes: 4 additions & 2 deletions go/pools/resource_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
)

var (
lastID, count, closeCount, resetCount atomic.Int64
waitStarts []time.Time
lastID, count, closeCount atomic.Int64
waitStarts []time.Time
)

type TestResource struct {
Expand Down Expand Up @@ -178,6 +178,7 @@ func TestShrinking(t *testing.T) {
waitStarts = waitStarts[:0]

p := NewResourcePool(PoolFactory, 5, 5, time.Second, 0, logWait, nil, 0)
defer p.Close()
var resources [10]Resource
// Leave one empty slot in the pool
for i := 0; i < 4; i++ {
Expand Down Expand Up @@ -365,6 +366,7 @@ func TestReopen(t *testing.T) {
assert.Equal(t, expected, stats)
assert.EqualValues(t, 5, lastID.Load())
assert.EqualValues(t, 0, count.Load())
p.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a defer p.Close() right after the pool gets initialized? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored it a bit to align with my other changes.

}

func TestIdleTimeout(t *testing.T) {
Expand Down
Loading