From 916bca33ea6cf2530117071fdd9b7b15e00e2f29 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Wed, 17 May 2023 16:13:43 +0200 Subject: ui: fix deadlocks in message channel There are several ways the ui message channel can fill up leading to deadlocks. 1) Invalidate() changes the value of uiState to DIRTY. The following call sequence: QueueRedraw() Invalidate() QueueRedraw() Leads to multiple nil messages being queued in the message channel whereas one could assume that the second QueueRedraw() would do nothing. This is caused by the tri-state nature of uiState. 2) We use the same channel to convey state change, keyboard events and redraw requests. Since a keyboard event almost always triggers a redraw, we end up trying to append a redraw message in the same goroutine that reads from the channel. This triggers a deadlock when there are more than 50 pending messages. Solve the issue by using multiple channels, one per type of message that needs to be sent to the main ui thread. Remove QueueRedraw() and merge its functionality in Invalidate(). Only use a DIRTY/CLEAN state to determine if something needs to be queued in the redraw channel. Use a channel for quitting instead of an atomic. Restructure some code functions to have a cleaner API. Use a for loop in the main thread and select from all channels. Signed-off-by: Robin Jarry Tested-by: Koni Marti Tested-by: Maarten van Gompel --- lib/ui/textinput.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/ui/textinput.go') diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go index d76e9349..b2ba33f6 100644 --- a/lib/ui/textinput.go +++ b/lib/ui/textinput.go @@ -329,7 +329,7 @@ func (ti *TextInput) showCompletions() { } ti.completions, ti.prefix = ti.tabcomplete(ti.StringLeft()) ti.completeIndex = -1 - QueueRedraw() + Invalidate() } func (ti *TextInput) OnChange(onChange func(ti *TextInput)) { -- cgit