You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While testing the OCPP 1.6 central-system using the provided example code, i sometimes get the error fatal error: concurrent map writes when connecting multiple mock-chargers within a short timeframe (around 1ms or lower).
// Add handlers for dis/connection of charge pointscentralSystem.SetNewChargePointHandler(func(chargePoint ocpp16.ChargePointConnection) {
handler.chargePoints[chargePoint.ID()] =&ChargePointState{connectors: map[int]*ConnectorInfo{}, transactions: map[int]*TransactionInfo{}}
log.WithField("client", chargePoint.ID()).Info("new charge point connected")
})
However, this code will have errors with concurrent write to handler.chargePoints, like described.
We can fix this issue by adding a mutex like so:
varmux sync.Mutex// ...// Add handlers for dis/connection of charge pointscentralSystem.SetNewChargePointHandler(func(chargePoint ocpp16.ChargePointConnection) {
mux.Lock()
defermux.Unlock()
handler.chargePoints[chargePoint.ID()] =&ChargePointState{connectors: map[int]*ConnectorInfo{}, transactions: map[int]*TransactionInfo{}}
log.WithField("client", chargePoint.ID()).Info("new charge point connected")
})
Should thread-safety of the chargePoints map be the responsibility of the library, or should the function that is passed to SetChargePointDisconnectedHandler be responsible for thread safety? If it is not the responsibility of the library, I think at least the example for central-system 1.6 should be updated with a mux like I showed here! 😄
The text was updated successfully, but these errors were encountered:
mbbirkel
changed the title
OCPP 1.6 example - "fatal error: concurrent map writes" when connecting multiple chargers at the same time
Central-system 1.6 example - "fatal error: concurrent map writes" when connecting multiple chargers at the same time
Oct 22, 2023
mbbirkel
changed the title
Central-system 1.6 example - "fatal error: concurrent map writes" when connecting multiple chargers at the same time
Central-system 1.6 example - "fatal error: concurrent map writes" when connecting multiple chargers rapidly
Oct 22, 2023
Hey @mbbirkel, incoming callbacks from chargers (both on new connection and on messages) are fully concurrent, hence they need to be synchronized by the application.
I will update the examples to include the mux on the server side, hopefully making it clearer. Thanks for pointing it out.
While testing the OCPP 1.6 central-system using the provided example code, i sometimes get the error
fatal error: concurrent map writes
when connecting multiple mock-chargers within a short timeframe (around 1ms or lower).I used similar code for handling connections to the 1.6 central-system example:
However, this code will have errors with concurrent write to
handler.chargePoints
, like described.We can fix this issue by adding a mutex like so:
Should thread-safety of the
chargePoints
map be the responsibility of the library, or should the function that is passed toSetChargePointDisconnectedHandler
be responsible for thread safety? If it is not the responsibility of the library, I think at least the example for central-system 1.6 should be updated with a mux like I showed here! 😄The text was updated successfully, but these errors were encountered: