Yes, Context.Value is thread/goroutine-safe. I can’t tell from this snippet of code what the issue is. Why are you redacting out one of the lines of the stack trace?
if data, ok := ctx.Value(appData).(*sync.Map); ok { //here is the panic happening
data.Range(func(key, value interface{}) bool {
if k, ok := key.(string); ok {
appData[k] = value
}
return true
})
}
This is not threadsafe/goroutine-safe on all machine architectures.
EDIT: This is wrong; I was thinking this was storing to a pointer, but I suspect it’s actually storing to the Request struct which is definitely not threadsafe.
Ranging over the data*sync.Map won’t access invalid memory, but it is non-deterministic, so if other goroutines are adding/removing elements, you don’t know what you’ll get.
It looks like appData is a (potentially global?) map and you’re writing into it. This is not threadsafe/goroutine-safe.