fix(renderer): prevent emitting stale frame on resize (#1856)

Co-authored-by: Drew Smirnoff <drew@charm.land>
Signed-off-by: Gurdeep <gurdeepdalal05@gmail.com>
Signed-off-by: Drew Smirnoff <drew@charm.land>
This commit is contained in:
Gurdeep 2026-11-12 19:09:48 +05:30 committed by GitHub
parent 18d6fa3b34
commit 94d45c807d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 0 deletions

View file

@ -34,6 +34,7 @@ type cursedRenderer struct {
syncdUpdates bool // whether to use synchronized output mode for updates
starting bool // indicates whether the renderer is starting after being stopped
pendingErase bool // an scr.Erase() is pending and hasn't been drained by flush yet
viewStale bool // a resize occurred and we are waiting for render() to supply the new View
noInput bool // whether input is disabled, in which case keyboard enhancement queries are pointless
}
@ -297,6 +298,10 @@ func (s *cursedRenderer) flush(closing bool) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
if !closing && s.viewStale {
return false, nil
}
view := s.view
frameArea := uv.Rect(0, 0, s.width, s.height)
if len(view.Content) == 0 {
@ -644,6 +649,7 @@ func (s *cursedRenderer) render(v View) {
defer s.mu.Unlock()
s.view = v
s.viewStale = false
}
// reset implements renderer.
@ -691,6 +697,7 @@ func (s *cursedRenderer) resize(w, h int) {
s.width, s.height = w, h
s.scr.Resize(s.width, s.height)
s.pendingErase = true
s.viewStale = true
s.mu.Unlock()
}

View file

@ -202,6 +202,51 @@ func TestCursedRenderer_updatesKittyKeyboardFlagsInPlace(t *testing.T) {
}
}
// Fixes: https://github.com/charmbracelet/bubbletea/issues/1780
func TestCursedRenderer_resizeStaleFrame(t *testing.T) {
t.Parallel()
var out bytes.Buffer
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
// Render the initial frame at 80x24.
initialView := NewView("initial 80x24 content")
initialView.AltScreen = true
r.render(initialView)
if _, err := r.flush(false); err != nil {
t.Fatal(err)
}
out.Reset()
// Simulate window resize: resize() is called first, updating dimensions.
r.resize(100, 30)
// In the race window, the 60Hz ticker fires flush() BEFORE render() provides the new View.
// flush() must NOT output the stale initialView at the new 100x30 size.
if _, err := r.flush(false); err != nil {
t.Fatal(err)
}
if out.Len() > 0 {
t.Fatalf("expected no output during stale resize race window, got %q", out.String())
}
// Now render() provides the new View matching the new 100x30 size.
newView := NewView("resized 100x30 content")
newView.AltScreen = true
r.render(newView)
// The subsequent flush() must draw the new view.
if _, err := r.flush(false); err != nil {
t.Fatal(err)
}
if !strings.Contains(out.String(), "resized 100x30 content") {
t.Fatalf("expected output to contain resized view, got %q", out.String())
}
}
// Fixes: https://github.com/charmbracelet/bubbletea/issues/1802
//
// insertAbove must measure wrapped lines with the active screen buffer's