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

Loadpoint: fix race condition accessing soc estimator (#16180) #16194

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions core/loadpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,10 @@ func (lp *Loadpoint) publishSocAndRange() {
soc, err := lp.chargerSoc()

// guard for socEstimator removed by api
if lp.socEstimator == nil || (!lp.vehicleHasSoc() && err != nil) {
// also keep a local copy in order to avoid race conditions
// https://github.com/evcc-io/evcc/issues/16180
socEstimator := lp.socEstimator
if socEstimator == nil || (!lp.vehicleHasSoc() && err != nil) {
// This is a workaround for heaters. Without vehicle, the soc estimator is not initialized.
// We need to check if the charger can provide soc and use it if available.
if err == nil {
Expand All @@ -1533,7 +1536,7 @@ func (lp *Loadpoint) publishSocAndRange() {
if err == nil || lp.chargerHasFeature(api.IntegratedDevice) || lp.vehicleSocPollAllowed() {
lp.socUpdated = lp.clock.Now()

f, err := lp.socEstimator.Soc(lp.getChargedEnergy())
f, err := socEstimator.Soc(lp.getChargedEnergy())
if err != nil {
if loadpoint.AcceptableError(err) {
lp.socUpdated = time.Time{}
Expand Down Expand Up @@ -1567,11 +1570,11 @@ func (lp *Loadpoint) publishSocAndRange() {

var d time.Duration
if lp.charging() {
d = lp.socEstimator.RemainingChargeDuration(limitSoc, lp.chargePower)
d = socEstimator.RemainingChargeDuration(limitSoc, lp.chargePower)
}
lp.SetRemainingDuration(d)

lp.SetRemainingEnergy(1e3 * lp.socEstimator.RemainingChargeEnergy(limitSoc))
lp.SetRemainingEnergy(1e3 * socEstimator.RemainingChargeEnergy(limitSoc))

// range
if vs, ok := lp.GetVehicle().(api.VehicleRange); ok {
Expand Down