It panic error occasionally
SunDrizzle opened this issue · comments
Describe the bug
I use beaver to build a socket server.But sometimes it panics with an error.
The error is : fatal error: concurrent map writes
.
And the error's stacks info : github.com/clivern/beaver/internal/app/controller.(*Websocket).HandleConnections(0xc0001ca4d0, 0x7f033e218240, 0xc0004869a0, 0xc0000b1200, 0xc000233108, 0x24, 0xc00023312d, 0xbd, 0xc000029440, 0x24) /root/Beaver/internal/app/controller/socket.go:166 +0x505 fp=0xc000585688 sp=0xc0005854e8 pc=0xabd815
.
The code in 166 line is:delete(e.Clients, ID)
I google for the reason and find it's unsafe that using map in goruntine. So I think Websocket.Clients
should protected by mutex or use sync.Map
directly?
Development or production environment
- OS: [Ubuntu 16.04]
- Go 1.12.17
it is unsafe indeed since may be it got concurrent edits at same time, will check that on code
here is where the issue happen https://github.com/Clivern/Beaver/blob/master/internal/app/controller/socket.go#L166