From cd8ea26df9101680c4ab55e8cca6b92f5e4fbc25 Mon Sep 17 00:00:00 2001 From: Gianguido Sora Date: Tue, 8 Feb 2022 12:04:10 +0100 Subject: [PATCH] fix: add concurrency fence on traceContext to avoid data races (#11117) See #11114 for more info. ## Description Closes: #11114 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 4addb7368c021617818065a89811ed6e28c621c3) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 5 ++++ store/rootmulti/store.go | 30 +++++++++++++++++---- store/rootmulti/store_test.go | 51 +++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91bede3a4349..28c8d4325dd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,11 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +<<<<<<< HEAD +======= +* (store) [\#11117](https://github.com/cosmos/cosmos-sdk/pull/11117) Fix data race in store trace component +* (cli) [\#11065](https://github.com/cosmos/cosmos-sdk/pull/11065) Ensure the `tendermint-validator-set` query command respects the `-o` output flag. +>>>>>>> 4addb7368 (fix: add concurrency fence on traceContext to avoid data races (#11117)) * (grpc) [\#10985](https://github.com/cosmos/cosmos-sdk/pull/10992) The `/cosmos/tx/v1beta1/txs/{hash}` endpoint returns a 404 when a tx does not exist. * (std/codec) [/#10595](https://github.com/cosmos/cosmos-sdk/pull/10595) Add evidence to std/codec to be able to decode evidence in client interactions. * [#10725](https://github.com/cosmos/cosmos-sdk/pull/10725) populate `ctx.ConsensusParams` for begin/end blockers. diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 07dd864de1e3..0789a0bfdf34 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -9,6 +9,7 @@ import ( "math" "sort" "strings" + "sync" iavltree "github.com/cosmos/iavl" protoio "github.com/gogo/protobuf/io" @@ -55,8 +56,9 @@ type Store struct { pruneHeights []int64 initialVersion int64 - traceWriter io.Writer - traceContext types.TraceContext + traceWriter io.Writer + traceContext types.TraceContext + traceContextMutex sync.Mutex interBlockCache types.MultiStorePersistentCache @@ -315,6 +317,8 @@ func (rs *Store) SetTracer(w io.Writer) types.MultiStore { // be overwritten. It is implied that the caller should update the context when // necessary between tracing operations. It returns a modified MultiStore. func (rs *Store) SetTracingContext(tc types.TraceContext) types.MultiStore { + rs.traceContextMutex.Lock() + defer rs.traceContextMutex.Unlock() if rs.traceContext != nil { for k, v := range tc { rs.traceContext[k] = v @@ -326,6 +330,22 @@ func (rs *Store) SetTracingContext(tc types.TraceContext) types.MultiStore { return rs } +func (rs *Store) getTracingContext() types.TraceContext { + rs.traceContextMutex.Lock() + defer rs.traceContextMutex.Unlock() + + if rs.traceContext == nil { + return nil + } + + ctx := types.TraceContext{} + for k, v := range rs.traceContext { + ctx[k] = v + } + + return ctx +} + // TracingEnabled returns if tracing is enabled for the MultiStore. func (rs *Store) TracingEnabled() bool { return rs.traceWriter != nil @@ -452,7 +472,7 @@ func (rs *Store) CacheMultiStore() types.CacheMultiStore { for k, v := range rs.stores { stores[k] = v } - return cachemulti.NewStore(rs.db, stores, rs.keysByName, rs.traceWriter, rs.traceContext, rs.listeners) + return cachemulti.NewStore(rs.db, stores, rs.keysByName, rs.traceWriter, rs.getTracingContext(), rs.listeners) } // CacheMultiStoreWithVersion is analogous to CacheMultiStore except that it @@ -482,7 +502,7 @@ func (rs *Store) CacheMultiStoreWithVersion(version int64) (types.CacheMultiStor } } - return cachemulti.NewStore(rs.db, cachedStores, rs.keysByName, rs.traceWriter, rs.traceContext, rs.listeners), nil + return cachemulti.NewStore(rs.db, cachedStores, rs.keysByName, rs.traceWriter, rs.getTracingContext(), rs.listeners), nil } // GetStore returns a mounted Store for a given StoreKey. If the StoreKey does @@ -514,7 +534,7 @@ func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore { store := s.(types.KVStore) if rs.TracingEnabled() { - store = tracekv.NewStore(store, rs.traceWriter, rs.traceContext) + store = tracekv.NewStore(store, rs.traceWriter, rs.getTracingContext()) } if rs.ListeningEnabled(key) { store = listenkv.NewStore(store, key, rs.listeners[key]) diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index cf7653b076cb..afe59a6b6a3a 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -11,6 +11,7 @@ import ( "io/ioutil" "math/rand" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -804,6 +805,56 @@ func TestCacheWraps(t *testing.T) { require.IsType(t, cachemulti.Store{}, cacheWrappedWithListeners) } +func TestTraceConcurrency(t *testing.T) { + db := dbm.NewMemDB() + multi := newMultiStoreWithMounts(db, types.PruneNothing) + err := multi.LoadLatestVersion() + require.NoError(t, err) + + b := &bytes.Buffer{} + key := multi.keysByName["store1"] + tc := types.TraceContext(map[string]interface{}{"blockHeight": 64}) + + multi.SetTracer(b) + multi.SetTracingContext(tc) + + cms := multi.CacheMultiStore() + store1 := cms.GetKVStore(key) + cw := store1.CacheWrapWithTrace(b, tc) + _ = cw + require.NotNil(t, store1) + + stop := make(chan struct{}) + stopW := make(chan struct{}) + + go func(stop chan struct{}) { + for { + select { + case <-stop: + return + default: + store1.Set([]byte{1}, []byte{1}) + cms.Write() + } + } + }(stop) + + go func(stop chan struct{}) { + for { + select { + case <-stop: + return + default: + multi.SetTracingContext(tc) + } + } + }(stopW) + + time.Sleep(3 * time.Second) + stop <- struct{}{} + stopW <- struct{}{} +} + func BenchmarkMultistoreSnapshot100K(b *testing.B) { benchmarkMultistoreSnapshot(b, 10, 10000) }