about summary refs log tree commit diff stats
path: root/widgets/msglist.go
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2019-04-27 16:47:59 +0000
committerDrew DeVault <sir@cmpwn.com>2019-04-27 14:30:28 -0400
commit5685a176747711d0330f1251ad297b13e0621a37 (patch)
treed338db2be97f5ac412a427776167d21b04abdf99 /widgets/msglist.go
parent9ef2a57b51601cc0e18824bbfe8026891da1a38d (diff)
downloadaerc-5685a176747711d0330f1251ad297b13e0621a37.tar.gz
lib/ui: introduce Invalidatable
Many Drawable implementations have their own Invalidate and OnInvalidate
functions, with an unexported onInvalidate field. However OnInvalidate and
Invalidate are usually not called in the same goroutine. This results in a race
on this field, e.g.:

    Read at 0x00c000094748 by goroutine 7:
      git.sr.ht/~sircmpwn/aerc2/widgets.NewDirectoryList.func1()
          /home/simon/src/aerc2/widgets/dirlist.go:85 +0x56
      git.sr.ht/~sircmpwn/aerc2/widgets.(*Spinner).Start.func1()
          /home/simon/src/aerc2/widgets/spinner.go:93 +0x1bb

    Previous write at 0x00c000094748 by main goroutine:
      [failed to restore the stack]

    Goroutine 7 (running) created at:
      git.sr.ht/~sircmpwn/aerc2/widgets.(*Spinner).Start()
          /home/simon/src/aerc2/widgets/spinner.go:46 +0x8f
      git.sr.ht/~sircmpwn/aerc2/widgets.NewDirectoryList()
          /home/simon/src/aerc2/widgets/dirlist.go:37 +0x286
      git.sr.ht/~sircmpwn/aerc2/widgets.NewAccountView()
          /home/simon/src/aerc2/widgets/account.go:50 +0x5ca
      git.sr.ht/~sircmpwn/aerc2/widgets.NewAerc()
          /home/simon/src/aerc2/widgets/aerc.go:60 +0x800
      main.main()
          /home/simon/src/aerc2/aerc.go:65 +0x33e

To fix this, introduce a new type, Invalidatable, which protects the field.
Unfortunately the Drawable must be passed to the callback function in
Invalidate, so we still need to re-implement this in each Invalidatable user.
Diffstat (limited to 'widgets/msglist.go')
-rw-r--r--widgets/msglist.go24
1 files changed, 9 insertions, 15 deletions
diff --git a/widgets/msglist.go b/widgets/msglist.go
index ea9b245..c4b5d82 100644
--- a/widgets/msglist.go
+++ b/widgets/msglist.go
@@ -12,14 +12,14 @@ import (
 )
 
 type MessageList struct {
-	conf         *config.AercConfig
-	logger       *log.Logger
-	height       int
-	onInvalidate func(d ui.Drawable)
-	scroll       int
-	selected     int
-	spinner      *Spinner
-	store        *lib.MessageStore
+	ui.Invalidatable
+	conf     *config.AercConfig
+	logger   *log.Logger
+	height   int
+	scroll   int
+	selected int
+	spinner  *Spinner
+	store    *lib.MessageStore
 }
 
 // TODO: fish in config
@@ -37,14 +37,8 @@ func NewMessageList(logger *log.Logger) *MessageList {
 	return ml
 }
 
-func (ml *MessageList) OnInvalidate(onInvalidate func(d ui.Drawable)) {
-	ml.onInvalidate = onInvalidate
-}
-
 func (ml *MessageList) Invalidate() {
-	if ml.onInvalidate != nil {
-		ml.onInvalidate(ml)
-	}
+	ml.DoInvalidate(ml)
 }
 
 func (ml *MessageList) Draw(ctx *ui.Context) {