fix(bonfire): claim the live view slot before building the program (IN-15)

This commit is contained in:
Jakub Zych
2026-10-22 03:53:29 +02:00
parent 48e13f9b57
commit 1ac75ad59a
3 changed files with 85 additions and 8 deletions

View File

@@ -341,9 +341,10 @@ var errLivePanic = errors.New("bonfire: live view work panicked")
// runLive runs model as a bubbletea program on out while work runs on the
// calling goroutine, with c as the owner of the view. It first claims the
// single live slot under liveMu; when another view holds it, runLive
// returns started false without running work, and the caller prints plain
// lines instead. work gets a send function for progress messages. When
// single live slot under liveMu and only then builds the program; when
// another view holds the slot, runLive returns started false without
// building a program or running work, and the caller prints plain lines
// instead. work gets a send function for progress messages. When
// work returns or panics, the view is unregistered (so PrintAbove stops
// sending to it), told to finish and waited for, all under liveMu; it
// never receives lines after that, and a panic goes on after the cleanup.
@@ -351,6 +352,14 @@ func runLive(ctx context.Context, out io.Writer, c *console, model tea.Model, wo
if ctx == nil {
ctx = context.Background()
}
// Claim the slot before building the program: a widget that loses it
// builds none, so it registers no child context on ctx and opens no
// TEA_TRACE file. NewProgram takes no bonfire lock and prints nothing.
liveMu.Lock()
if activeView != nil {
liveMu.Unlock()
return false, nil
}
prog := tea.NewProgram(model,
tea.WithInput(nil),
tea.WithOutput(out),
@@ -359,11 +368,6 @@ func runLive(ctx context.Context, out io.Writer, c *console, model tea.Model, wo
tea.WithColorProfile(c.profile()),
)
view := &liveView{owner: c, prog: prog}
liveMu.Lock()
if activeView != nil {
liveMu.Unlock()
return false, nil
}
activeView = view
liveMu.Unlock()