gocarina / gocsv

The GoCSV package aims to provide easy CSV serialization and deserialization to the golang programming language
MIT License
1.99k stars 245 forks source link

Race condition if multiple threads are parsing CSV and somebody calls SetHeaderNormalizer #237

Open brianV opened 1 year ago

brianV commented 1 year ago

Hi all.

We've noted a race condition with SetHeaderNormalizer():

func SetHeaderNormalizer(f Normalizer) {
    normalizeName = f
    // Need to clear the cache hen the header normalizer changes.
    structInfoCache = sync.Map{}
}

If we have threads currently working and performing operations on structInfoCache, our copying of a blank map into structInfoCache leads to various errors including:

fatal error: sync: unlock of unlocked mutex

structInfoCache should be defined as a *sync.Map{}. When we need to clear the cache, we then should run:

structInfoCache = &sync.Map{}

In-progress calls to the old structInfoCache would complete, and new calls will operate against the new one.