fix: don't panic on uncomparable color.Color values (#1852)
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> Co-authored-by: Drew Smirnoff <drew@charm.land> Signed-off-by: Drew Smirnoff <drew@charm.land>
This commit is contained in:
parent
38ddf5f58c
commit
7ab4335dac
4 changed files with 189 additions and 7 deletions
|
|
@ -491,7 +491,7 @@ func (s *cursedRenderer) flush(closing bool) (bool, error) {
|
|||
{newColor: view.ForegroundColor, oldColor: lfg, reset: ansi.ResetForegroundColor, setter: ansi.SetForegroundColor},
|
||||
{newColor: view.BackgroundColor, oldColor: lbg, reset: ansi.ResetBackgroundColor, setter: ansi.SetBackgroundColor},
|
||||
} {
|
||||
if c.newColor != c.oldColor {
|
||||
if !colorEqual(c.newColor, c.oldColor) {
|
||||
if c.newColor == nil {
|
||||
// Reset the color if it was set to nil.
|
||||
_, _ = s.scr.WriteString(c.reset)
|
||||
|
|
@ -928,6 +928,20 @@ func setProgramStatus(s *cursedRenderer, prev, cur *ProgramStatus) {
|
|||
}
|
||||
}
|
||||
|
||||
// colorEqual reports whether two colors are equal. Colors are compared by
|
||||
// their RGBA values, which is all the terminal ever sees, rather than with ==,
|
||||
// which panics on [color.Color] implementations whose dynamic type is
|
||||
// uncomparable (e.g. a struct containing a slice). A nil color stays distinct
|
||||
// from any other color so that resetting a color still works.
|
||||
func colorEqual(a, b color.Color) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
ar, ag, ab, aa := a.RGBA()
|
||||
br, bg, bb, ba := b.RGBA()
|
||||
return ar == br && ag == bg && ab == bb && aa == ba
|
||||
}
|
||||
|
||||
func viewEquals(a, b *View) bool {
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
|
|
@ -943,8 +957,8 @@ func viewEquals(a, b *View) bool {
|
|||
a.ReportFocus != b.ReportFocus ||
|
||||
a.MouseMode != b.MouseMode ||
|
||||
a.WindowTitle != b.WindowTitle ||
|
||||
a.ForegroundColor != b.ForegroundColor ||
|
||||
a.BackgroundColor != b.BackgroundColor ||
|
||||
!colorEqual(a.ForegroundColor, b.ForegroundColor) ||
|
||||
!colorEqual(a.BackgroundColor, b.BackgroundColor) ||
|
||||
a.KeyboardEnhancements != b.KeyboardEnhancements {
|
||||
return false
|
||||
}
|
||||
|
|
@ -957,7 +971,7 @@ func viewEquals(a, b *View) bool {
|
|||
a.Cursor.Y != b.Cursor.Y ||
|
||||
a.Cursor.Shape != b.Cursor.Shape ||
|
||||
a.Cursor.Blink != b.Cursor.Blink ||
|
||||
a.Cursor.Color != b.Cursor.Color {
|
||||
!colorEqual(a.Cursor.Color, b.Cursor.Color) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package tea
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
|
@ -227,6 +228,103 @@ func TestCursedRenderer_mouseVsFlush(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// uncomparableColor is a color.Color implementation whose dynamic type is
|
||||
// uncomparable (it contains a slice), so comparing two values with == panics.
|
||||
type uncomparableColor struct {
|
||||
rgba color.RGBA
|
||||
pad []int
|
||||
}
|
||||
|
||||
func (c uncomparableColor) RGBA() (r, g, b, a uint32) {
|
||||
return c.rgba.RGBA()
|
||||
}
|
||||
|
||||
// Fixes: https://github.com/charmbracelet/bubbletea/issues/1848
|
||||
func TestCursedRenderer_uncomparableViewColors(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var out bytes.Buffer
|
||||
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
|
||||
r.start()
|
||||
|
||||
red := color.RGBA{R: 0xff, A: 0xff}
|
||||
view := NewView("hello")
|
||||
view.AltScreen = true
|
||||
view.ForegroundColor = uncomparableColor{rgba: red, pad: []int{1}}
|
||||
view.BackgroundColor = uncomparableColor{rgba: red, pad: []int{2}}
|
||||
view.Cursor = &Cursor{Color: uncomparableColor{rgba: red, pad: []int{3}}}
|
||||
|
||||
// Render the same view twice. The second flush takes the viewEquals
|
||||
// early-return path, which compares the color.Color interface values.
|
||||
for range 2 {
|
||||
r.render(view)
|
||||
if _, err := r.flush(false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Colors are compared by value: the same color, whatever its type, doesn't
|
||||
// redraw the view, while a different color or a reset does.
|
||||
func TestCursedRenderer_viewColorChanges(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
red := color.RGBA{R: 0xff, A: 0xff}
|
||||
blue := color.RGBA{B: 0xff, A: 0xff}
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
set func(*View, color.Color)
|
||||
code string
|
||||
reset string
|
||||
}{
|
||||
{"foreground", func(v *View, c color.Color) { v.ForegroundColor = c }, "10", "110"},
|
||||
{"background", func(v *View, c color.Color) { v.BackgroundColor = c }, "11", "111"},
|
||||
{"cursor", func(v *View, c color.Color) { v.Cursor = NewCursor(0, 0); v.Cursor.Color = c }, "12", "112"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var out bytes.Buffer
|
||||
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
|
||||
render := func(c color.Color) bool {
|
||||
t.Helper()
|
||||
view := NewView("hello")
|
||||
tt.set(&view, c)
|
||||
r.render(view)
|
||||
drew, err := r.flush(false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return drew
|
||||
}
|
||||
|
||||
render(uncomparableColor{rgba: red, pad: []int{1}})
|
||||
if want := "\x1b]" + tt.code + ";#ff0000\a"; !strings.Contains(out.String(), want) {
|
||||
t.Fatalf("missing red color sequence %q in %q", want, out.String())
|
||||
}
|
||||
|
||||
out.Reset()
|
||||
if render(uncomparableColor{rgba: red, pad: []int{2}}) || out.Len() != 0 {
|
||||
t.Fatalf("unchanged color redrew the view: %q", out.String())
|
||||
}
|
||||
if render(red) || out.Len() != 0 {
|
||||
t.Fatalf("the same color of another type redrew the view: %q", out.String())
|
||||
}
|
||||
|
||||
render(uncomparableColor{rgba: blue})
|
||||
if want := "\x1b]" + tt.code + ";#0000ff\a"; !strings.Contains(out.String(), want) {
|
||||
t.Fatalf("missing blue color sequence %q in %q", want, out.String())
|
||||
}
|
||||
|
||||
out.Reset()
|
||||
render(nil)
|
||||
if want := "\x1b]" + tt.reset + "\a"; !strings.Contains(out.String(), want) {
|
||||
t.Fatalf("missing color reset %q in %q", want, out.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertInOrder(t *testing.T, got string, wants ...string) {
|
||||
t.Helper()
|
||||
rest := got
|
||||
|
|
|
|||
38
tea.go
38
tea.go
|
|
@ -1418,6 +1418,13 @@ func (p *Program) recoverFromPanic(r interface{}) {
|
|||
// recoverFromGoPanic recovers from a goroutine panic, prints a stack trace,
|
||||
// restores the terminal to a usable state, and signals the program to be killed.
|
||||
func (p *Program) recoverFromGoPanic(r interface{}) {
|
||||
p.recoverFromGoPanicWithStack(r, debug.Stack())
|
||||
}
|
||||
|
||||
// recoverFromGoPanicWithStack is [Program.recoverFromGoPanic] with the stack
|
||||
// captured at the panic site. The renderer ticker goroutine uses this because
|
||||
// it must not block in [Program.shutdown].
|
||||
func (p *Program) recoverFromGoPanicWithStack(r interface{}, trace []byte) {
|
||||
select {
|
||||
case p.errs <- ErrProgramPanic:
|
||||
default:
|
||||
|
|
@ -1427,7 +1434,7 @@ func (p *Program) recoverFromGoPanic(r interface{}) {
|
|||
// terminal does not work or when raw mode is still active.
|
||||
rec := strings.ReplaceAll(fmt.Sprintf("%s", r), "\n", "\r\n")
|
||||
fmt.Fprintf(os.Stderr, "Caught panic:\r\n\r\n%s\r\n\r\nRestoring terminal...\r\n\r\n", rec)
|
||||
stack := strings.ReplaceAll(fmt.Sprintf("%s\n", debug.Stack()), "\n", "\r\n")
|
||||
stack := strings.ReplaceAll(fmt.Sprintf("%s\n", trace), "\n", "\r\n")
|
||||
fmt.Fprint(os.Stderr, stack)
|
||||
if v, err := strconv.ParseBool(os.Getenv("TEA_DEBUG")); err == nil && v {
|
||||
f, err := os.Create(fmt.Sprintf("bubbletea-panic-%d.log", time.Now().Unix()))
|
||||
|
|
@ -1561,8 +1568,14 @@ func (p *Program) startRenderer() {
|
|||
realign = false
|
||||
ticker.Reset(framerate)
|
||||
}
|
||||
_ = p.flush()
|
||||
if drew, _ := p.renderer.flush(false); drew {
|
||||
drew, ok := p.flushFrame()
|
||||
if !ok {
|
||||
// The renderer panicked and the program is shutting
|
||||
// down. Stop drawing and wait for stopRenderer.
|
||||
<-p.rendererDone
|
||||
return
|
||||
}
|
||||
if drew {
|
||||
idle = 0
|
||||
} else if idle++; idle >= idleFrames {
|
||||
// Nothing is changing. Stop waking up every frame until
|
||||
|
|
@ -1575,6 +1588,25 @@ func (p *Program) startRenderer() {
|
|||
}()
|
||||
}
|
||||
|
||||
// flushFrame writes the queued output and the current frame, and reports
|
||||
// whether a frame was drawn. If the renderer panics, ok is false and the panic
|
||||
// is recovered so the terminal still gets restored. The recovery runs on
|
||||
// another goroutine: shutting down sends on rendererDone, which only the
|
||||
// renderer goroutine calling flushFrame receives.
|
||||
func (p *Program) flushFrame() (drew, ok bool) {
|
||||
if !p.disableCatchPanics {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ok = false
|
||||
go p.recoverFromGoPanicWithStack(r, debug.Stack())
|
||||
}
|
||||
}()
|
||||
}
|
||||
_ = p.flush()
|
||||
drew, _ = p.renderer.flush(false)
|
||||
return drew, true
|
||||
}
|
||||
|
||||
// stopRenderer stops the renderer.
|
||||
// If kill is true, the renderer will be stopped immediately without flushing
|
||||
// the last frame.
|
||||
|
|
|
|||
38
tea_test.go
38
tea_test.go
|
|
@ -775,6 +775,44 @@ func TestTeaGoroutinePanic(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// flushPanicRenderer panics on every non-closing flush, simulating a renderer
|
||||
// crash inside the ticker goroutine started by startRenderer.
|
||||
type flushPanicRenderer struct {
|
||||
nilRenderer
|
||||
}
|
||||
|
||||
func (flushPanicRenderer) flush(closing bool) (bool, error) {
|
||||
if !closing {
|
||||
panic("flushPanicRenderer")
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Fixes: https://github.com/charmbracelet/bubbletea/issues/1848
|
||||
func TestTeaRendererFlushPanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
var buf bytes.Buffer
|
||||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m,
|
||||
WithInput(&in),
|
||||
WithOutput(&buf),
|
||||
WithoutSignals(),
|
||||
)
|
||||
p.renderer = flushPanicRenderer{}
|
||||
|
||||
_, err := p.Run()
|
||||
|
||||
if !errors.Is(err, ErrProgramPanic) {
|
||||
t.Fatalf("Expected %v, got %v", ErrProgramPanic, err)
|
||||
}
|
||||
|
||||
if !errors.Is(err, ErrProgramKilled) {
|
||||
t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)
|
||||
}
|
||||
}
|
||||
|
||||
type benchModel struct {
|
||||
t testing.TB
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue