Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View() gets stuck rendering a lipgloss.AdaptiveColor style (in an oddly specific situation) #1036

Closed
matta opened this issue Jun 10, 2024 · 4 comments

Comments

@matta
Copy link

matta commented Jun 10, 2024

Describe the bug

I have isolated a bug in either bubbletea, lipgloss, or termenv where it may hang for many seconds, or even indefinitely, in a call to lipgloss style.Render() from the model's View(). This happens under specific situations:

  1. The first call to View() returns a string and does not use lipgloss at all.
  2. The second call to View(), which occurs after the first tea.WindowSizeMsg, does create a style with a lipgloss.AdaptiveColor and calls Render on it. This hangs in a call to output.HasDarkBackground() in termenv (I figured this out by adding logging in locally hacked packages)

If I change the program to use an adaptive color in the first call to View() the program works correctly.

Theory: sounds like a race condition, where the View() is interacting with the terminal and...maybe?...in parallel bubbletea is also reading/writing to the terminal in another goroutine?

Setup
Please complete the following information along with version numbers, if applicable.

  • OS: Debian 12 (bookworm), (current stable)
  • Shell zsh
  • Terminal Emulator alacritty, kitty, Gnome terminal, xterm all exhibit the problem
  • Terminal Multiplexer none (though running under tmux hides the problem reliably)

To Reproduce
Steps to reproduce the behavior:

  1. run https://github.com/matta/sift/blob/renderbugrepro/sift.go (see below)
  2. if it prints the correct screen size then the bug has not occurred
  3. otherwise, it will say the screen size is (0, 0) and hang, sometimes for a few seconds, sometimes forever.

Source Code

Run the following go program:

package main

import (
	"fmt"
	"log"
	"log/slog"
	"os"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"github.com/davecgh/go-spew/spew"
)

type model struct {
	width  int
	height int
}

// Init implements tea.Model.
func (outer model) Init() tea.Cmd {
	slog.Debug("teaModel.Init()")
	return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	prefix := spew.Sprintf("Update(%#v)", msg)
	slog.Debug(fmt.Sprintf("%s ENTER", prefix))
	defer slog.Debug(fmt.Sprintf("%s LEAVE", prefix))

	switch msg := msg.(type) {
	case tea.WindowSizeMsg:
		m.width = msg.Width
		m.height = msg.Height
	case tea.KeyMsg:
		return m, tea.Quit
	}

	return m, nil
}

func (m model) View() string {
	slog.Debug("View() ENTER")
	defer slog.Debug("View() LEAVE")

	out := fmt.Sprint("The screen has ", m.width,
		" columns and ", m.height, " rows\n")

	if m.width > 0 {
		style := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
			Light: "#DDDADA",
			Dark:  "#9C9C00",
		})
		out = style.Render(out)
	}

	return out
}

func setUpLogging() *os.File {
	file, err := tea.LogToFileWith(
		"sift.log", "sift", log.Default())
	if err != nil {
		fmt.Printf("Error logging to file: %v\n", err)
		os.Exit(1)
	}

	log.Default().SetFlags(log.LstdFlags | log.Lmicroseconds | log.Llongfile)
	slog.SetLogLoggerLevel(slog.LevelDebug)

	return file
}

func main() {
	logFile := setUpLogging()
	defer func() {
		if logFile != nil {
			_ = logFile.Close()
		}
	}()
	slog.Debug("program started")

	program := tea.NewProgram(model{}, tea.WithAltScreen())
	_, err := program.Run()
	if err != nil {
		slog.Error("Error running program: %v", err)
	}

	slog.Debug("program exiting")
}

Expected behavior

The program should quickly render the current screen size, then accept any key press to exit.

Screenshots

Picture of the program when hung:

image

Additional context

Here is a log of the program's behavior for a run where it exited after 5 seconds, after receiving a key press I did not perform:

sift 2024/06/09 19:40:44.479146 /home/matt/git/sift/sift.go:84: INFO program started
sift 2024/06/09 19:40:44.479198 /home/matt/git/sift/sift.go:21: DEBUG teaModel.Init()
sift 2024/06/09 19:40:44.479201 /home/matt/git/sift/sift.go:42: DEBUG View() ENTER
sift 2024/06/09 19:40:44.479204 /home/matt/git/sift/sift.go:56: DEBUG View() LEAVE
sift 2024/06/09 19:40:44.479516 /home/matt/git/sift/sift.go:27: DEBUG Update((tea.WindowSizeMsg){Width:(int)63 Height:(int)22}) ENTER
sift 2024/06/09 19:40:44.479528 /home/matt/git/sift/sift.go:38: DEBUG Update((tea.WindowSizeMsg){Width:(int)63 Height:(int)22}) LEAVE
sift 2024/06/09 19:40:44.479531 /home/matt/git/sift/sift.go:42: DEBUG View() ENTER
sift 2024/06/09 19:40:49.483461 /home/matt/git/sift/sift.go:56: DEBUG View() LEAVE
sift 2024/06/09 19:40:49.483598 /home/matt/git/sift/sift.go:27: DEBUG Update((tea.KeyMsg)alt+]) ENTER
sift 2024/06/09 19:40:49.483618 /home/matt/git/sift/sift.go:35: DEBUG Update((tea.KeyMsg)alt+]) LEAVE
sift 2024/06/09 19:40:49.483632 /home/matt/git/sift/sift.go:42: DEBUG View() ENTER
sift 2024/06/09 19:40:49.483695 /home/matt/git/sift/sift.go:56: DEBUG View() LEAVE
sift 2024/06/09 19:40:49.483726 /home/matt/git/sift/sift.go:42: DEBUG View() ENTER
sift 2024/06/09 19:40:49.483778 /home/matt/git/sift/sift.go:56: DEBUG View() LEAVE
sift 2024/06/09 19:40:49.495190 /home/matt/git/sift/sift.go:92: DEBUG program exiting

Key points:

  1. The first call to View() happens before the tea.WindowSizeMsg. In this case program's View() code returns a simple (no styling).
  2. The program then gets a tea.WindowSizeMsg followed by a call to View(). This call to View() renders a lipgloss.AdaptiveColor and hangs for five seconds.
  3. The program receives an alt+] key press (which I didn't send) and this causes the program to exit.
@matta
Copy link
Author

matta commented Jun 10, 2024

I wonder if #1010 will address this?

@meowgorithm
Copy link
Member

meowgorithm commented Jun 10, 2024

Indeed it will. What's happening, and the main problem #1010 exists to solve, is that currently Bubble Tea and Lip Gloss are both jumping on, and fighting over, stdout at the same time. That proposal essentially brokers queries to input and output allowing them to work together in harmony.

You can try out the proposal now with:

go get github.com/charmbracelet/bubbletea@beta
go get github.com/charmbracelet/lipgloss@beta

Here's the diff:

diff --git a/main.go b/main.go
index 28529ba..36916d4 100644
--- a/main.go
+++ b/main.go
@@ -17,12 +17,12 @@ type model struct {
 }
 
 // Init implements tea.Model.
-func (outer model) Init() tea.Cmd {
+func (outer model) Init(ctx tea.Context) (tea.Model, tea.Cmd) {
 	slog.Debug("teaModel.Init()")
-	return nil
+	return model{}, nil
 }
 
-func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+func (m model) Update(ctx tea.Context, msg tea.Msg) (tea.Model, tea.Cmd) {
 	prefix := spew.Sprintf("Update(%#v)", msg)
 	slog.Debug(fmt.Sprintf("%s ENTER", prefix))
 	defer slog.Debug(fmt.Sprintf("%s LEAVE", prefix))
@@ -38,7 +38,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	return m, nil
 }
 
-func (m model) View() string {
+func (m model) View(ctx tea.Context) string {
 	slog.Debug("View() ENTER")
 	defer slog.Debug("View() LEAVE")

Note that this is still a proposal and the API may change a bit.

Full source here.

@meowgorithm
Copy link
Member

Also note that, for now, you can simply force Lip Gloss to query for background color prior to running Bubble Tea. This, of course, is not ideal, but will also work for now.

diff --git a/main.go b/main.go
index 0c6907a..d873c2b 100644
--- a/main.go
+++ b/main.go
@@ -80,6 +80,8 @@ func main() {
 	}()
 	slog.Debug("program started")
 
+	_ = lipgloss.HasDarkBackground()
+
 	program := tea.NewProgram(model{}, tea.WithAltScreen())
 	_, err := program.Run()
 	if err != nil {

@matta
Copy link
Author

matta commented Jun 10, 2024

Thanks, great to have that confirmation. Since the issue is known and tracked in #1010 I'll close this.

@matta matta closed this as completed Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants