Skip to content

Commit f282329

Browse files
authored
Merge pull request #1587 from ydb-platform/issue-1585
fixed bug with multiple closing driver
2 parents 636ca2f + 4703bb7 commit f282329

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fixed panic on multiple closing driver
2+
13
## v3.95.1
24
* Added alias from `ydb.WithFakeTx(ydb.ScriptingQueryMode)` to `ydb.WithFakeTx(ydb.QueryExecuteQueryMode)` for compatibility with legacy code
35

driver.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"sync"
9+
"sync/atomic"
910

1011
"google.golang.org/grpc"
1112

@@ -103,6 +104,7 @@ type (
103104
children map[uint64]*Driver
104105
childrenMtx xsync.Mutex
105106
onClose []func(c *Driver)
107+
closed atomic.Bool
106108

107109
panicCallback func(e interface{})
108110
}
@@ -149,6 +151,11 @@ func (d *Driver) Close(ctx context.Context) (finalErr error) {
149151
defer func() {
150152
onDone(finalErr)
151153
}()
154+
155+
if !d.closed.CompareAndSwap(false, true) {
156+
return nil
157+
}
158+
152159
d.ctxCancel()
153160

154161
d.mtx.Lock()

internal/query/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ func (c *Client) ExecuteScript(
188188
}
189189

190190
func (c *Client) Close(ctx context.Context) error {
191+
if c == nil {
192+
return xerrors.WithStackTrace(errNilClient)
193+
}
194+
191195
close(c.done)
192196

193197
if err := c.pool.Close(ctx); err != nil {

internal/query/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
var (
10+
errNilClient = xerrors.Wrap(errors.New("table client is not initialized"))
1011
ErrTransactionRollingBack = xerrors.Wrap(errors.New("ydb: the transaction is rolling back"))
1112
errWrongNextResultSetIndex = errors.New("wrong result set index")
1213
errWrongResultSetIndex = errors.New("critical violation of the logic - wrong result set index")

tests/integration/driver_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,22 @@ func TestClusterDiscoveryRetry(t *testing.T) {
651651
}
652652
require.Greater(t, counter, 1)
653653
}
654+
655+
func TestMultipleClosingDriverIssue1585(t *testing.T) {
656+
ctx, cancel := context.WithCancel(context.Background())
657+
defer cancel()
658+
659+
db, err := ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"))
660+
require.NoError(t, err)
661+
662+
require.NotPanics(t, func() {
663+
err = db.Close(ctx)
664+
require.NoError(t, err)
665+
666+
err = db.Close(ctx)
667+
require.NoError(t, err)
668+
669+
err = db.Close(ctx)
670+
require.NoError(t, err)
671+
})
672+
}

0 commit comments

Comments
 (0)