Skip to content

Commit 416d3aa

Browse files
committed
Fixed race with read/write pool conns on closing conn
1 parent a3ee6a5 commit 416d3aa

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.13.2
2+
* Fixed race with read/write pool conns on closing conn
3+
14
## 3.13.1
25
* Improved error messages
36
* Defended `cluster.balancer` with `sync.RWMutex` on `cluster.Insert`, `cluster.Update`, `cluster.Remove` and `cluster.Get`

internal/conn/pool.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package conn
22

33
import (
44
"context"
5+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
56
"sync"
67
"sync/atomic"
78
"time"
@@ -52,6 +53,10 @@ type pool struct {
5253

5354
func (p *pool) Pessimize(ctx context.Context, cc Conn, cause error) {
5455
e := cc.Endpoint().Copy()
56+
57+
p.mtx.RLock()
58+
defer p.mtx.RUnlock()
59+
5560
cc, ok := p.conns[e.Address()]
5661
if !ok {
5762
return
@@ -78,11 +83,15 @@ func (p *pool) Release(ctx context.Context) error {
7883

7984
close(p.done)
8085

81-
p.mtx.Lock()
82-
defer p.mtx.Unlock()
86+
p.mtx.RLock()
87+
conns := make([]closer.Closer, 0, len(p.conns))
88+
for _, c := range p.conns {
89+
conns = append(conns, c)
90+
}
91+
p.mtx.RUnlock()
8392

8493
var issues []error
85-
for _, c := range p.conns {
94+
for _, c := range conns {
8695
if err := c.Close(ctx); err != nil {
8796
issues = append(issues, err)
8897
}
@@ -105,7 +114,8 @@ func (p *pool) GetConn(e endpoint.Endpoint) Conn {
105114
e,
106115
p.config,
107116
withOnClose(func(c *conn) {
108-
// conn.Conn.Close() must called on under locked p.mtx
117+
p.mtx.Lock()
118+
defer p.mtx.Unlock()
109119
delete(p.conns, c.Endpoint().Address())
110120
}),
111121
)

internal/meta/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package meta
22

33
const (
4-
Version = "ydb-go-sdk/3.13.1"
4+
Version = "ydb-go-sdk/3.13.2"
55
)

0 commit comments

Comments
 (0)