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 cockroachdb#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 Jul 13, 2023
1 parent ad5f3a5 commit da9813f
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 @@ -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 {
Expand Down

0 comments on commit da9813f

Please sign in to comment.