Skip to content

fix(store/cachemulti): clone traceContext to prevent concurrent map panic#25934

Open
djm07073 wants to merge 2 commits intocosmos:mainfrom
djm07073:fix/cachemulti-tracecontext-race
Open

fix(store/cachemulti): clone traceContext to prevent concurrent map panic#25934
djm07073 wants to merge 2 commits intocosmos:mainfrom
djm07073:fix/cachemulti-tracecontext-race

Conversation

@djm07073
Copy link
Copy Markdown

@djm07073 djm07073 commented Feb 14, 2026

Closes #25841

Summary

  • Clone traceContext before passing to child stores in CacheMultiStore() to prevent concurrent map panic

Root Cause

cachemulti.Store.CacheMultiStore() passes traceContext map by reference to child stores via NewFromParent(). When multiple goroutines concurrently modify their child stores' traceContext, they all write to the same underlying map, causing fatal error: concurrent map writes.

Solution

Clone traceContext before passing to NewFromParent():

var traceCtx types.TraceContext
if cms.traceContext != nil {
    traceCtx = cms.traceContext.Clone()
}
return NewFromParent(cms.getCacheWrapper, cms.traceWriter, traceCtx)

To reproduce the panic (on the first commit):

git checkout baa5519660
cd store && go test -race ./cachemulti/... -run TestCacheMultiStoreTraceConcurrency
# fatal error: concurrent map writes

Add tests to verify traceContext isolation between parent and child stores.
TestCacheMultiStoreTraceConcurrency will panic with "concurrent map writes"
when multiple goroutines modify their traceContext, demonstrating the bug
described in cosmos#25841.

Run `go test -race ./store/cachemulti/...` on this commit to reproduce the panic.
…anic

Clone traceContext before passing to child stores in CacheMultiStore()
to prevent "fatal error: concurrent map writes" when multiple goroutines
concurrently create and modify cache stores with tracing enabled.

Closes cosmos#25841
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days-before-close if no further activity occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

concurrent map panic in cachemulti.Store - traceContext shared by reference across goroutines

1 participant