fatal error: concurrent map read and map write
goroutine 5065809 [running]:
runtime.throw(0x6b4281, 0x21)
/usr/local/go/src/runtime/panic.go:566 +0x95 fp=0xc420c05670
sp=0xc420c05650
runtime.mapaccess1_faststr(0x65ea20, 0xc420015020, 0xc42178ea8e, 0x16,
0x0)
/usr/local/go/src/runtime/hashmap_fast.go:201 +0x4f3
fp=0xc420c056d0 sp=0xc420c05670
collider.(*Collider).httpHandler(0xc420013120, 0x7c4dc0, 0xc4209dcdd0,
0xc420a954a0)
/home/ec2-user/goWorkspace/src/collider/collider.go:176 +0x7dd
fp=0xc420c05c48 sp=0xc420c056d0
and the code is
query := r.URL.Query()
if(query["q"] == nil){
c.httpError("must have a question",w)
return
}
m := query["q"][0] //this is line 176
It happens about once per day and after it happens the whole http server is down. I don't know if the http get do some concurrent writes in map. How to solve this?
You are probably using a global variable for the map. Global variables are accessible by every request. With bad luck two requests try to access the same map at the same time, hence your error.
The easiest fix would be to use a Map
from the package sync
. But this could be more of a workaround according to your use case. But this slows down your program.
An alternative would be to define the map inside the handler if you only need it there.
If you could post more of your file, it would be easier to give a better answer.