From 14085868c1b02e9aad44d41a61c9bf8ea1479afe 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 5fdda1bfeefc..82933f2a6c21 100644 --- a/pkg/kv/kvserver/concurrency/lock_table_waiter.go +++ b/pkg/kv/kvserver/concurrency/lock_table_waiter.go @@ -951,8 +951,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 {