From 9253b8cbd15bc0f043f11a627d30bc98a41da01d Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Mon, 17 Feb 2025 11:05:14 +0000 Subject: [PATCH] Fix a potential connection pool leak. Signed-off-by: Arthur Schreiber --- go/pools/smartconnpool/stack.go | 1 + go/pools/smartconnpool/stack_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 go/pools/smartconnpool/stack_test.go diff --git a/go/pools/smartconnpool/stack.go b/go/pools/smartconnpool/stack.go index ea7ae50201e..ba38e31cecf 100644 --- a/go/pools/smartconnpool/stack.go +++ b/go/pools/smartconnpool/stack.go @@ -48,6 +48,7 @@ func (s *connStack[C]) Pop() (*Pooled[C], bool) { newHead := oldHead.next.Load() if s.top.CompareAndSwap(oldHead, popCount, newHead, popCount+1) { + oldHead.next.Store(nil) return oldHead, true } runtime.Gosched() diff --git a/go/pools/smartconnpool/stack_test.go b/go/pools/smartconnpool/stack_test.go new file mode 100644 index 00000000000..a8150bd8186 --- /dev/null +++ b/go/pools/smartconnpool/stack_test.go @@ -0,0 +1,26 @@ +package smartconnpool + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type testPooled struct { + Connection +} + +func TestStackPop(t *testing.T) { + s := &connStack[testPooled]{} + + first := &Pooled[testPooled]{} + s.Push(first) + + second := &Pooled[testPooled]{} + s.Push(second) + + c, ok := s.Pop() + assert.True(t, ok) + + assert.Nil(t, c.next.Load()) +}