From b970df81d899bbc1a7560bb9a2a18cb3b37db109 Mon Sep 17 00:00:00 2001 From: andig Date: Thu, 6 Aug 2020 12:49:14 +0200 Subject: [PATCH] Fix potential deadlock --- core/loadpoint.go | 12 ++++++++++-- core/site.go | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/core/loadpoint.go b/core/loadpoint.go index 7ad3eac06..f3060553d 100644 --- a/core/loadpoint.go +++ b/core/loadpoint.go @@ -171,7 +171,7 @@ func (lp *LoadPoint) SetMode(mode api.ChargeMode) { if lp.Mode != mode { lp.Mode = mode lp.publish("mode", mode) - lp.lpChan <- lp // request loadpoint update + lp.requestUpdate() } } @@ -193,7 +193,15 @@ func (lp *LoadPoint) SetTargetSoC(targetSoC int) { if lp.TargetSoC != targetSoC { lp.TargetSoC = targetSoC lp.publish("targetSoC", targetSoC) - lp.lpChan <- lp // request loadpoint update + lp.requestUpdate() + } +} + +// requestUpdate requests site to update this loadpoint +func (lp *LoadPoint) requestUpdate() { + select { + case lp.lpChan <- lp: // request loadpoint update + default: } } diff --git a/core/site.go b/core/site.go index 471350abc..b67cf6ce4 100644 --- a/core/site.go +++ b/core/site.go @@ -314,7 +314,7 @@ func (site *Site) update(lp Updater) { // Prepare attaches communication channels to site and loadpoints func (site *Site) Prepare(uiChan chan<- util.Param, pushChan chan<- push.Event) { site.uiChan = uiChan - site.lpUpdateChan = make(chan *LoadPoint) + site.lpUpdateChan = make(chan *LoadPoint, 1) // 1 capacity to avoid deadlock for id, lp := range site.loadpoints { lpUIChan := make(chan util.Param)