Partially lock setVehicle to void deadlock in applyAction (#3857)

This commit is contained in:
andig 2022-07-15 11:16:58 +02:00 • committed by GitHub
parent 7d12728ee6
commit 76a400ef1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -786,6 +786,9 @@ func (lp *LoadPoint) selectVehicleByID(id string) api.Vehicle {
// setActiveVehicle assigns currently active vehicle and configures soc estimator
func (lp *LoadPoint) setActiveVehicle(vehicle api.Vehicle) {
lp.Lock()
defer lp.Unlock()
if lp.vehicle == vehicle {
return
}
@ -809,6 +812,9 @@ func (lp *LoadPoint) setActiveVehicle(vehicle api.Vehicle) {
lp.publish("vehicleTitle", lp.vehicle.Title())
lp.publish("vehicleCapacity", lp.vehicle.Capacity())
// release lock to unblock api
lp.Unlock()
// publish odometer once
if vs, ok := lp.vehicle.(api.VehicleOdometer); ok {
if odo, err := vs.Odometer(); err == nil {
@ -821,6 +827,9 @@ func (lp *LoadPoint) setActiveVehicle(vehicle api.Vehicle) {
lp.applyAction(vehicle.OnIdentified())
// re-apply lock to match defer above
lp.Lock()
lp.progress.Reset()
} else {
lp.socEstimator = nil

View file

@ -266,12 +266,15 @@ func (lp *LoadPoint) GetVehicles() []api.Vehicle {
// SetVehicle sets the active vehicle
func (lp *LoadPoint) SetVehicle(vehicle api.Vehicle) {
lp.Lock()
defer lp.Unlock()
// TODO develop universal locking approach
// setActiveVehicle is protected by lock, hence no locking here
// set desired vehicle
lp.setActiveVehicle(vehicle)
lp.Lock()
defer lp.Unlock()
// disable auto-detect
lp.stopVehicleDetection()
}