Skip to content

Commit dd47ef7

Browse files
committed
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.
1 parent 228d442 commit dd47ef7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

pkg/gui/context/list_renderer.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,19 @@ func (self *ListRenderer) renderLines(startIdx int, endIdx int) string {
9999

100100
func (self *ListRenderer) prepareConversionArrays(nonModelItems []*NonModelItem) {
101101
self.numNonModelItems = len(nonModelItems)
102-
self.viewIndicesByModelIndex = lo.Range(self.list.Len() + 1)
103-
self.modelIndicesByViewIndex = lo.Range(self.list.Len() + 1)
102+
viewIndicesByModelIndex := lo.Range(self.list.Len() + 1)
103+
modelIndicesByViewIndex := lo.Range(self.list.Len() + 1)
104104
offset := 0
105105
for _, item := range nonModelItems {
106106
for i := item.Index; i <= self.list.Len(); i++ {
107-
self.viewIndicesByModelIndex[i]++
107+
viewIndicesByModelIndex[i]++
108108
}
109-
self.modelIndicesByViewIndex = slices.Insert(
110-
self.modelIndicesByViewIndex, item.Index+offset, self.modelIndicesByViewIndex[item.Index+offset])
109+
modelIndicesByViewIndex = slices.Insert(
110+
modelIndicesByViewIndex, item.Index+offset, modelIndicesByViewIndex[item.Index+offset])
111111
offset++
112112
}
113+
self.viewIndicesByModelIndex = viewIndicesByModelIndex
114+
self.modelIndicesByViewIndex = modelIndicesByViewIndex
113115
}
114116

115117
func (self *ListRenderer) insertNonModelItems(

0 commit comments

Comments
 (0)