1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Fix config reload race

Race condition is around the usage of `enabled`
Wrap access in a mutex to eliminate race
Fixes #422
This commit is contained in:
Sean Smith 2019-09-10 21:49:40 -04:00
parent bb3c24df73
commit 26ca1618ab

View File

@ -2,6 +2,7 @@ package view
import ( import (
"fmt" "fmt"
"sync"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
@ -19,6 +20,7 @@ type Base struct {
quitChan chan bool quitChan chan bool
refreshing bool refreshing bool
refreshInterval int refreshInterval int
enabledMutex *sync.Mutex
} }
func NewBase(app *tview.Application, commonSettings *cfg.Common, focusable bool) Base { func NewBase(app *tview.Application, commonSettings *cfg.Common, focusable bool) Base {
@ -33,6 +35,7 @@ func NewBase(app *tview.Application, commonSettings *cfg.Common, focusable bool)
quitChan: make(chan bool), quitChan: make(chan bool),
refreshInterval: commonSettings.RefreshInterval, refreshInterval: commonSettings.RefreshInterval,
refreshing: false, refreshing: false,
enabledMutex: &sync.Mutex{},
} }
return base return base
} }
@ -73,19 +76,30 @@ func (base *Base) ContextualTitle(defaultStr string) string {
} }
func (base *Base) Disable() { func (base *Base) Disable() {
base.enabledMutex.Lock()
base.enabled = false base.enabled = false
base.enabledMutex.Unlock()
} }
func (base *Base) Disabled() bool { func (base *Base) Disabled() bool {
return !base.enabled base.enabledMutex.Lock()
result := !base.enabled
base.enabledMutex.Unlock()
return result
} }
func (base *Base) Enabled() bool { func (base *Base) Enabled() bool {
return base.enabled base.enabledMutex.Lock()
result := base.enabled
base.enabledMutex.Unlock()
return result
} }
func (base *Base) Focusable() bool { func (base *Base) Focusable() bool {
return base.enabled && base.focusable base.enabledMutex.Lock()
result := base.enabled && base.focusable
base.enabledMutex.Unlock()
return result
} }
func (base *Base) FocusChar() string { func (base *Base) FocusChar() string {
@ -119,7 +133,9 @@ func (base *Base) SetFocusChar(char string) {
} }
func (base *Base) Stop() { func (base *Base) Stop() {
base.enabledMutex.Lock()
base.enabled = false base.enabled = false
base.enabledMutex.Unlock()
base.quitChan <- true base.quitChan <- true
} }