From dd47ef73547d23a158237538ec657dcd0f801a63 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 12 Jul 2025 17:25:27 +0200 Subject: [PATCH] Make prepareConversionArrays a little more concurrency safe I have seen cases where during a rebase (two nonModelItems) all entries in viewIndicesByModelIndex beyond the second nonModelItem were off by 4 rather than 2 as I would expect. The only explanation I have for this is that the function was called concurrently. Improve this by working on a local variable and only assign to self at the end; this is not a real fix for the concurrency issue of course, but it makes it much less likely to be a problem in practice. --- pkg/gui/context/list_renderer.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/gui/context/list_renderer.go b/pkg/gui/context/list_renderer.go index 9bde5fb4b..e863045e0 100644 --- a/pkg/gui/context/list_renderer.go +++ b/pkg/gui/context/list_renderer.go @@ -99,17 +99,19 @@ func (self *ListRenderer) renderLines(startIdx int, endIdx int) string { func (self *ListRenderer) prepareConversionArrays(nonModelItems []*NonModelItem) { self.numNonModelItems = len(nonModelItems) - self.viewIndicesByModelIndex = lo.Range(self.list.Len() + 1) - self.modelIndicesByViewIndex = lo.Range(self.list.Len() + 1) + viewIndicesByModelIndex := lo.Range(self.list.Len() + 1) + modelIndicesByViewIndex := lo.Range(self.list.Len() + 1) offset := 0 for _, item := range nonModelItems { for i := item.Index; i <= self.list.Len(); i++ { - self.viewIndicesByModelIndex[i]++ + viewIndicesByModelIndex[i]++ } - self.modelIndicesByViewIndex = slices.Insert( - self.modelIndicesByViewIndex, item.Index+offset, self.modelIndicesByViewIndex[item.Index+offset]) + modelIndicesByViewIndex = slices.Insert( + modelIndicesByViewIndex, item.Index+offset, modelIndicesByViewIndex[item.Index+offset]) offset++ } + self.viewIndicesByModelIndex = viewIndicesByModelIndex + self.modelIndicesByViewIndex = modelIndicesByViewIndex } func (self *ListRenderer) insertNonModelItems(