Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Central-system 1.6 example - "fatal error: concurrent map writes" when connecting multiple chargers rapidly #239

Open
mbbirkel opened this issue Oct 22, 2023 · 1 comment
Assignees
Labels
docs Related to documentation or examples

Comments

@mbbirkel
Copy link

mbbirkel commented Oct 22, 2023

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:

	// Add handlers for dis/connection of charge points
	centralSystem.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:

var mux sync.Mutex         
        
   // ...

	// Add handlers for dis/connection of charge points
	centralSystem.SetNewChargePointHandler(func(chargePoint ocpp16.ChargePointConnection) {
		mux.Lock()
		defer mux.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! 😄

@mbbirkel 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 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
@lorenzodonini lorenzodonini self-assigned this Oct 30, 2023
@lorenzodonini lorenzodonini added the docs Related to documentation or examples label Oct 30, 2023
@lorenzodonini
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Related to documentation or examples
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants