From 4c3565653ab504166f9b8ecc1a44f6aa17f9f6e6 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Sun, 25 Sep 2022 14:38:48 -0500 Subject: [PATCH] textinput: prevent data race from debounce function Protect access to fields in textinput. Concurrent access can happen in the main event loop and the completion debounce function. Signed-off-by: Tim Culverhouse Acked-by: Robin Jarry --- lib/ui/textinput.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go index 70dcb3f..ce8ccc5 100644 --- a/lib/ui/textinput.go +++ b/lib/ui/textinput.go @@ -3,6 +3,7 @@ package ui import ( "math" "strings" + "sync" "time" "github.com/gdamore/tcell/v2" @@ -17,6 +18,7 @@ import ( type TextInput struct { Invalidatable + sync.Mutex cells int ctx *Context focus bool @@ -294,7 +296,9 @@ func (ti *TextInput) updateCompletions() { if ti.completeDebouncer == nil { ti.completeDebouncer = time.AfterFunc(ti.completeDelay, func() { defer logging.PanicHandler() + ti.Lock() ti.showCompletions() + ti.Unlock() }) } else { ti.completeDebouncer.Stop() @@ -321,6 +325,8 @@ func (ti *TextInput) OnFocusLost(onFocusLost func(ti *TextInput)) { } func (ti *TextInput) Event(event tcell.Event) bool { + ti.Lock() + defer ti.Unlock() if event, ok := event.(*tcell.EventKey); ok { switch event.Key() { case tcell.KeyBackspace, tcell.KeyBackspace2: