updateDiffSync(): fix potential race
When updateDiffSync() is called asynchronously, it should lock the line array when calling Bytes(), to prevent race if the line array is being modified by the main goroutine in the meantime.
This commit is contained in:
parent
bca35a5939
commit
5a159ce444
1 changed files with 13 additions and 4 deletions
|
@ -1280,7 +1280,7 @@ func (b *Buffer) Write(bytes []byte) (n int, err error) {
|
||||||
return len(bytes), nil
|
return len(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) updateDiffSync() {
|
func (b *Buffer) updateDiff(synchronous bool) {
|
||||||
b.diffLock.Lock()
|
b.diffLock.Lock()
|
||||||
defer b.diffLock.Unlock()
|
defer b.diffLock.Unlock()
|
||||||
|
|
||||||
|
@ -1291,7 +1291,16 @@ func (b *Buffer) updateDiffSync() {
|
||||||
}
|
}
|
||||||
|
|
||||||
differ := dmp.New()
|
differ := dmp.New()
|
||||||
baseRunes, bufferRunes, _ := differ.DiffLinesToRunes(string(b.diffBase), string(b.Bytes()))
|
|
||||||
|
if !synchronous {
|
||||||
|
b.Lock()
|
||||||
|
}
|
||||||
|
bytes := b.Bytes()
|
||||||
|
if !synchronous {
|
||||||
|
b.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
baseRunes, bufferRunes, _ := differ.DiffLinesToRunes(string(b.diffBase), string(bytes))
|
||||||
diffs := differ.DiffMainRunes(baseRunes, bufferRunes, false)
|
diffs := differ.DiffMainRunes(baseRunes, bufferRunes, false)
|
||||||
lineN := 0
|
lineN := 0
|
||||||
|
|
||||||
|
@ -1333,11 +1342,11 @@ func (b *Buffer) UpdateDiff() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if lineCount < 1000 {
|
if lineCount < 1000 {
|
||||||
b.updateDiffSync()
|
b.updateDiff(true)
|
||||||
} else if lineCount < 30000 {
|
} else if lineCount < 30000 {
|
||||||
b.updateDiffTimer = time.AfterFunc(500*time.Millisecond, func() {
|
b.updateDiffTimer = time.AfterFunc(500*time.Millisecond, func() {
|
||||||
b.updateDiffTimer = nil
|
b.updateDiffTimer = nil
|
||||||
b.updateDiffSync()
|
b.updateDiff(false)
|
||||||
screen.Redraw()
|
screen.Redraw()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue