Skip to content

Commit 92b2556

Browse files
committed
fixed possible multiply calls notifiers about stop transaction
1 parent f68315d commit 92b2556

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

internal/query/transaction.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package query
22

33
import (
44
"context"
5+
"slices"
6+
"sync"
57
"sync/atomic"
68

79
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
@@ -24,10 +26,12 @@ var (
2426
type Transaction struct {
2527
tx.Identifier
2628

27-
s *Session
28-
onCompleted []tx.OnTransactionCompletedFunc
29-
29+
s *Session
3030
rollbackStarted atomic.Bool
31+
32+
m sync.Mutex
33+
onCompletedCalled bool
34+
onCompleted []tx.OnTransactionCompletedFunc
3135
}
3236

3337
func (tx *Transaction) SessionID() string {
@@ -211,11 +215,24 @@ func (tx *Transaction) Rollback(ctx context.Context) error {
211215
}
212216

213217
func (tx *Transaction) OnCompleted(f tx.OnTransactionCompletedFunc) {
218+
tx.m.Lock()
219+
defer tx.m.Unlock()
220+
214221
tx.onCompleted = append(tx.onCompleted, f)
215222
}
216223

217224
func (tx *Transaction) notifyOnCompleted(err error) {
218-
for _, f := range tx.onCompleted {
225+
tx.m.Lock()
226+
notifyCalled := tx.onCompletedCalled
227+
tx.onCompletedCalled = true
228+
onCompletedFunctions := slices.Clone(tx.onCompleted)
229+
tx.m.Unlock()
230+
231+
if notifyCalled {
232+
return
233+
}
234+
235+
for _, f := range onCompletedFunctions {
219236
f(err)
220237
}
221238
}

0 commit comments

Comments
 (0)