From da9813f9e704306bd8b39b5d51c8a1dd461f9c01 Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Fri, 23 Jun 2023 16:09:06 -0700 Subject: [PATCH] kv: fix data race when updating pending txn in txnStatusCache Fixes #105244. This commit avoids a data race by treating *roachpb.Transaction objects as immutable, and simply choosing the right transaction to keep in the cache when there is a choice to be made. Release note: None --- pkg/kv/kvserver/concurrency/lock_table_waiter.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/kv/kvserver/concurrency/lock_table_waiter.go b/pkg/kv/kvserver/concurrency/lock_table_waiter.go index b440a85c4ef5..34370bf585c4 100644 --- a/pkg/kv/kvserver/concurrency/lock_table_waiter.go +++ b/pkg/kv/kvserver/concurrency/lock_table_waiter.go @@ -958,8 +958,10 @@ func (c *txnCache) add(txn *roachpb.Transaction) { c.mu.Lock() defer c.mu.Unlock() if idx := c.getIdxLocked(txn.ID); idx >= 0 { - if !txn.Status.IsFinalized() { - txn.Update(c.txns[idx]) + if curTxn := c.txns[idx]; txn.WriteTimestamp.Less(curTxn.WriteTimestamp) { + // If the new txn has a lower write timestamp than the cached txn, + // just move the cached txn to the front of the LRU cache. + txn = curTxn } c.moveFrontLocked(txn, idx) } else {