dirlist: fix race condition in directory selection

This replaces a channel that is used like a context with a context.

Signed-off-by: Moritz Poldrack <git@moritz.sh>
Acked-by: Koni Marti <koni.marti@gmail.com>
This commit is contained in:
Moritz Poldrack 2022-06-25 20:47:24 +02:00 committed by Robin Jarry
parent 6a10123f4a
commit 964b362ceb

View file

@ -1,6 +1,7 @@
package widgets
import (
"context"
"fmt"
"log"
"math"
@ -57,12 +58,15 @@ type DirectoryList struct {
selected string
spinner *Spinner
worker *types.Worker
skipSelect chan bool
skipSelect context.Context
skipSelectCancel context.CancelFunc
connected bool
}
func NewDirectoryList(conf *config.AercConfig, acctConf *config.AccountConfig,
logger *log.Logger, worker *types.Worker) DirectoryLister {
logger *log.Logger, worker *types.Worker,
) DirectoryLister {
ctx, cancel := context.WithCancel(context.Background())
dirlist := &DirectoryList{
aercConf: conf,
@ -70,7 +74,8 @@ func NewDirectoryList(conf *config.AercConfig, acctConf *config.AccountConfig,
logger: logger,
store: lib.NewDirStore(),
worker: worker,
skipSelect: make(chan bool),
skipSelect: ctx,
skipSelectCancel: cancel,
}
uiConf := dirlist.UiConfig()
dirlist.spinner = NewSpinner(&uiConf)
@ -135,10 +140,12 @@ func (dirlist *DirectoryList) ExpandFolder() {
func (dirlist *DirectoryList) Select(name string) {
dirlist.selecting = name
close(dirlist.skipSelect)
dirlist.skipSelect = make(chan bool)
dirlist.skipSelectCancel()
ctx, cancel := context.WithCancel(context.Background())
dirlist.skipSelect = ctx
dirlist.skipSelectCancel = cancel
go func() {
go func(ctx context.Context) {
defer logging.PanicHandler()
select {
@ -179,11 +186,11 @@ func (dirlist *DirectoryList) Select(name string) {
dirlist.Invalidate()
})
dirlist.Invalidate()
case <-dirlist.skipSelect:
case <-ctx.Done():
dirlist.logger.Println("dirlist: skip", name)
return
}
}()
}(ctx)
}
func (dirlist *DirectoryList) Selected() string {