Skip to content

Commit

Permalink
kv: fix data race when updating pending txn in txnStatusCache
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nvanbenschoten committed Jun 23, 2023
1 parent 98050cf commit 6017bce
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/kv/kvserver/concurrency/lock_table_waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,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 {
Expand Down

0 comments on commit 6017bce

Please sign in to comment.