Skip to content
Myr

Avoid race on renderer.buf in flush vs. write

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix race by acquiring the mutex before accessing renderer.buf in flush. WARNING: DATA RACE Read at 0x00c0000ee0a0 by goroutine 11: bytes.(*Buffer).Len() /home/chlunde/opt/go/src/bytes/buffer.go:73 +0x64 github.com/charmbracelet/bubbletea.(*renderer).flush() /home/chlunde/src/bubbletea/renderer.go:91 +0x45 github.com/charmbracelet/bubbletea.(*renderer).listen() /home/chlunde/src/bubbletea/renderer.go:76 +0x185 Previous write at 0x00c0000ee0a0 by main goroutine: bytes.(*Buffer).Reset() /home/chlunde/opt/go/src/bytes/buffer.go:98 +0xf5 github.com/charmbracelet/bubbletea.(*renderer).write() /home/chlunde/src/bubbletea/renderer.go:195 +0x13c github.com/charmbracelet/bubbletea.(*Program).Start() /home/chlunde/src/bubbletea/tea.go:330 +0xbbb main.main() /home/chlunde/src/bubbletea/examples/spinner/main.go:27 +0x229 Fixes #54

authored by Carl Henrik Lunde · committed by Christian Rocha · parent 92a7952 · Browse files · 1 file+3 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+3 added−3 removed

  • other:+3 added−3 removed

1 file+3 added−3 removed

modified:renderer.go+3 added-3 removed
86 unmodified lines
87}8889// flush renders the buffer.90func (r *renderer) flush() {91 r.mtx.Lock()92 defer r.mtx.Unlock()9394 if r.buf.Len() == 0 || r.buf.String() == r.lastRender {95 // Nothing to do96 return97 }
16 unmodified lines
114 // the place to do that.115116 out := new(bytes.Buffer)117115 r.mtx.Lock()116 defer r.mtx.Unlock()117118 // Clear any lines we painted in the last render.119 if r.linesRendered > 0 {120 for i := r.linesRendered - 1; i > 0; i-- {121 // Check if we should skip rendering for this line. Clearing the
286 unmodified lines