Skip to content

Commit

Permalink
*: fix panic when reusing OptimisticTxnContextProvider (#38952)
Browse files Browse the repository at this point in the history
close #38951
  • Loading branch information
lcwangchao authored Nov 7, 2022
1 parent f79e67f commit 32248e2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions session/txnmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type txnManager struct {
ctxProvider sessiontxn.TxnContextProvider

// We always reuse the same OptimisticTxnContextProvider in one session to reduce memory allocation cost for every new txn.
reservedOptimisticProvider isolation.OptimisticTxnContextProvider
reservedOptimisticProviders [2]isolation.OptimisticTxnContextProvider
}

func newTxnManager(sctx sessionctx.Context) *txnManager {
Expand Down Expand Up @@ -241,8 +241,13 @@ func (m *txnManager) newProviderWithRequest(r *sessiontxn.EnterNewTxnRequest) (s
switch txnMode {
case "", ast.Optimistic:
// When txnMode is 'OPTIMISTIC' or '', the transaction should be optimistic
m.reservedOptimisticProvider.ResetForNewTxn(m.sctx, r.CausalConsistencyOnly)
return &m.reservedOptimisticProvider, nil
provider := &m.reservedOptimisticProviders[0]
if old, ok := m.ctxProvider.(*isolation.OptimisticTxnContextProvider); ok && old == provider {
// We should make sure the new provider is not the same with the old one
provider = &m.reservedOptimisticProviders[1]
}
provider.ResetForNewTxn(m.sctx, r.CausalConsistencyOnly)
return provider, nil
case ast.Pessimistic:
// When txnMode is 'PESSIMISTIC', the provider should be determined by the isolation level
switch sessVars.IsolationLevelForNewTxn() {
Expand Down

0 comments on commit 32248e2

Please sign in to comment.