summary refs log tree commit diff stats
path: root/widgets/dirlist.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/dirlist.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/dirlist.go')
-rw-r--r--widgets/dirlist.go10
1 files changed, 2 insertions, 8 deletions
diff --git a/widgets/dirlist.go b/widgets/dirlist.go
index eb79bc4..374d142 100644
--- a/widgets/dirlist.go
+++ b/widgets/dirlist.go
@@ -12,10 +12,10 @@ import (
 )
 
 type DirectoryList struct {
+	ui.Invalidatable
 	conf         *config.AccountConfig
 	dirs         []string
 	logger       *log.Logger
-	onInvalidate func(d ui.Drawable)
 	selecting    string
 	selected     string
 	spinner      *Spinner
@@ -77,14 +77,8 @@ func (dirlist *DirectoryList) Selected() string {
 	return dirlist.selected
 }
 
-func (dirlist *DirectoryList) OnInvalidate(onInvalidate func(d ui.Drawable)) {
-	dirlist.onInvalidate = onInvalidate
-}
-
 func (dirlist *DirectoryList) Invalidate() {
-	if dirlist.onInvalidate != nil {
-		dirlist.onInvalidate(dirlist)
-	}
+	dirlist.DoInvalidate(dirlist)
 }
 
 func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
n211' href='#n211'>211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403