From 01d412e6b93053cff2c47c52fecec3dad7af55e7 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Sun, 22 Oct 2023 09:16:28 +0100 Subject: [PATCH] resolve the data race on accessing tx stats Refer to https://github.com/etcd-io/bbolt/issues/213 Signed-off-by: Benjamin Wang --- bucket.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bucket.go b/bucket.go index 604b138ac..d32252563 100644 --- a/bucket.go +++ b/bucket.go @@ -73,7 +73,12 @@ func (b *Bucket) Writable() bool { // Do not use a cursor after the transaction is closed. func (b *Bucket) Cursor() *Cursor { // Update transaction statistics. + // Note that `Cursor()` may be called by multiple readonly + // transactions concurrently, so we need to hold the lock + // `statlock` when updating the stats. + b.tx.db.statlock.Lock() b.tx.stats.CursorCount++ + b.tx.db.statlock.Unlock() // Allocate and return a cursor. return &Cursor{