lorenzodonini / ocpp-go

Open Charge Point Protocol implementation in Go
MIT License
262 stars 125 forks source link

Fix concurrent map writes in "ocpp1.6".centralSystem #56

Closed michaelbeaumont closed 3 years ago

michaelbeaumont commented 3 years ago

I've run into the following error:

fatal error: concurrent map writes

goroutine 36 [running]:
runtime.throw(0xa98670, 0x15)
        /usr/lib/go/src/runtime/panic.go:1117 +0x72 fp=0xc0003ad788 sp=0xc0003ad758 pc=0x43a2b2
runtime.mapdelete_faststr(0x9b7800, 0xc00023fe90, 0xc000290004, 0xd)
        /usr/lib/go/src/runtime/map_faststr.go:382 +0x3a8 fp=0xc0003ad7f0 sp=0xc0003ad788 pc=0x416b48
github.com/lorenzodonini/ocpp-go/ocpp1%2e6.(*centralSystem).handleIncomingConfirmation(0xc0002b8f50, 0xc000290004, 0xd, 0xb38300, 0xc00040e460, 0xc0004082f0, 0xa)
        /home/mike/projects/project/vendor/github.com/lorenzodonini/ocpp-go/ocpp1.6/central_system.go:488 +0xbc fp=0xc0003ad9a0 sp=0xc0003ad7f0 pc=0x91badc

[truncated]

gorilla/websocket is running these handlers concurrently and access to callbacks should thus be protected by a Mutex. This may also be an issue with the other packages, which I haven't looked at yet.

lorenzodonini commented 3 years ago

Actually, the panic may even be triggered by the application itself (regardless of the networking layer), if multiple goroutines attempt to send requests at the same time, so definitely worth having the mutex in place.

Nice catch, thanks!