koanf icon indicating copy to clipboard operation
koanf copied to clipboard

Add thread safety to all koanf public methods

Open musicpulpite opened this issue 11 months ago • 2 comments

This PR addresses the concerns brought up in Issue #355 and previously in Issue #153.

Since the koanf.confMap is a shared resource that could be accessed concurrently from different goroutines for reading/updating config values, the solution is to acquire and release a sync.RWMutex in each public method that accesses the confMap property.

A more robust and performant solution would be to refactor the existing koanf.confMap map property and all methods in "github.com/knadh/koanf/maps" to use the inherently thread safe sync.Map structure from the standard library. This approach would remove the overhead of adding RWMutex locking to any new public methods and provide more granular read locking in the case of many concurrent read operations where the current approach locks the entire map structure for each read. The obvious downside is that it would require a much more intensive refactor.

musicpulpite avatar Feb 04 '25 15:02 musicpulpite

Looks like the mutex is causing a deadlock: https://github.com/knadh/koanf/actions/runs/13138967660/job/36969992725?pr=339

knadh avatar Feb 14 '25 12:02 knadh

Looks like the mutex is causing a deadlock: https://github.com/knadh/koanf/actions/runs/13138967660/job/36969992725?pr=339

Good call. I'll dig into this to see where I messed up and will be sure to write some unit tests to prove that it works as intended (will try to use Go's Data Race Detector) before officially opening the PR.

musicpulpite avatar Feb 18 '25 16:02 musicpulpite

Closing this in favour of #377

knadh avatar Aug 25 '25 18:08 knadh