From 314b50c7b452fd737d28582ae9d27c04ea725001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=B2=E6=8D=97=E3=82=BC=E3=83=9F?= <47470998+wolfmagnate@users.noreply.github.com> Date: Mon, 23 Oct 2023 16:16:20 +0900 Subject: [PATCH 1/4] feat: properly call nested sequenceMsg and batchMsg --- tea.go | 71 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/tea.go b/tea.go index 34fb883e..b283f807 100644 --- a/tea.go +++ b/tea.go @@ -361,32 +361,7 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) { continue case sequenceMsg: - go func() { - // Execute commands one at a time, in order. - for _, cmd := range msg { - if cmd == nil { - continue - } - - msg := cmd() - if batchMsg, ok := msg.(BatchMsg); ok { - g, _ := errgroup.WithContext(p.ctx) - for _, cmd := range batchMsg { - cmd := cmd - g.Go(func() error { - p.Send(cmd()) - return nil - }) - } - - //nolint:errcheck - g.Wait() // wait for all commands from batch msg to finish - continue - } - - p.Send(msg) - } - }() + go p.execSequenceMsg(msg) } // Process internal messages for the renderer. @@ -402,6 +377,50 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) { } } +func (p *Program) execSequenceMsg(msg sequenceMsg) { + // Execute commands one at a time, in order. + for _, cmd := range msg { + if cmd == nil { + continue + } + msg := cmd() + switch msg := msg.(type) { + case BatchMsg: + p.execBatchMsg(msg) + case sequenceMsg: + p.execSequenceMsg(msg) + default: + p.Send(msg) + } + } +} + +func (p *Program) execBatchMsg(msg BatchMsg) { + + g, _ := errgroup.WithContext(p.ctx) + for _, cmd := range msg { + cmd := cmd + if cmd == nil { + continue + } + g.Go(func() error { + msg := cmd() + switch msg := msg.(type) { + case BatchMsg: + p.execBatchMsg(msg) + case sequenceMsg: + p.execSequenceMsg(msg) + default: + p.Send(msg) + } + return nil + }) + } + + //nolint:errcheck + g.Wait() // wait for all commands from batch msg to finish +} + // Run initializes the program and runs its event loops, blocking until it gets // terminated by either [Program.Quit], [Program.Kill], or its signal handler. // Returns the final model. From 6e1282a76358cb680de9d4de7520f9f99c9e2903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=B2=E6=8D=97=E3=82=BC=E3=83=9F?= <47470998+wolfmagnate@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:52:14 +0900 Subject: [PATCH 2/4] add example for the nested Sequence and Batch --- examples/sequence/main.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/examples/sequence/main.go b/examples/sequence/main.go index ea46dcc6..380ad7df 100644 --- a/examples/sequence/main.go +++ b/examples/sequence/main.go @@ -5,6 +5,7 @@ package main import ( "fmt" "os" + "time" tea "github.com/charmbracelet/bubbletea" ) @@ -14,15 +15,39 @@ type model struct{} func (m model) Init() tea.Cmd { return tea.Sequence( tea.Batch( - tea.Println("A"), - tea.Println("B"), - tea.Println("C"), + tea.Sequence( + SleepPrintln("1-1-1", 1000), + SleepPrintln("1-1-2", 1000), + ), + tea.Batch( + SleepPrintln("1-2-1", 1500), + SleepPrintln("1-2-2", 1250), + ), + ), + tea.Println("2"), + tea.Sequence( + tea.Batch( + SleepPrintln("3-1-1", 500), + SleepPrintln("3-1-2", 1000), + ), + tea.Sequence( + SleepPrintln("3-2-1", 750), + SleepPrintln("3-2-2", 500), + ), ), - tea.Println("Z"), tea.Quit, ) } +// print string after stopping for a certain period of time +func SleepPrintln(s string, milisecond int) tea.Cmd { + printCmd := tea.Println(s) + return func() tea.Msg { + time.Sleep(time.Duration(milisecond) * time.Millisecond) + return printCmd() + } +} + func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg.(type) { case tea.KeyMsg: From 0290af4a499ee6a3e22822cebe1e74fdeac313be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=B2=E6=8D=97=E3=82=BC=E3=83=9F?= <47470998+wolfmagnate@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:12:35 +0900 Subject: [PATCH 3/4] simplify case for BatchMsg --- tea.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tea.go b/tea.go index b283f807..95953a98 100644 --- a/tea.go +++ b/tea.go @@ -355,13 +355,12 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) { p.exec(msg.cmd, msg.fn) case BatchMsg: - for _, cmd := range msg { - cmds <- cmd - } + go p.execBatchMsg(msg) continue case sequenceMsg: go p.execSequenceMsg(msg) + continue } // Process internal messages for the renderer. @@ -396,7 +395,7 @@ func (p *Program) execSequenceMsg(msg sequenceMsg) { } func (p *Program) execBatchMsg(msg BatchMsg) { - + // Execute commands one at a time. g, _ := errgroup.WithContext(p.ctx) for _, cmd := range msg { cmd := cmd From 9e0e8f0df1c55044ed04bd17f4b460e01e94dc9c Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 11 Sep 2025 14:46:06 -0400 Subject: [PATCH 4/4] fix: recover from nested panics in Sequence and Batch commands --- tea.go | 38 +++++++++++++++++++++++++++++++------- tea_test.go | 19 ++++++++++--------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/tea.go b/tea.go index 2054db08..aa2999fe 100644 --- a/tea.go +++ b/tea.go @@ -499,6 +499,14 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) { } func (p *Program) execSequenceMsg(msg sequenceMsg) { + if !p.startupOptions.has(withoutCatchPanics) { + defer func() { + if r := recover(); r != nil { + p.recoverFromGoPanic(r) + } + }() + } + // Execute commands one at a time, in order. for _, cmd := range msg { if cmd == nil { @@ -517,14 +525,32 @@ func (p *Program) execSequenceMsg(msg sequenceMsg) { } func (p *Program) execBatchMsg(msg BatchMsg) { + if !p.startupOptions.has(withoutCatchPanics) { + defer func() { + if r := recover(); r != nil { + p.recoverFromGoPanic(r) + } + }() + } + // Execute commands one at a time. - g, _ := errgroup.WithContext(p.ctx) + var wg sync.WaitGroup for _, cmd := range msg { - cmd := cmd if cmd == nil { continue } - g.Go(func() error { + wg.Add(1) + go func() { + defer wg.Done() + + if !p.startupOptions.has(withoutCatchPanics) { + defer func() { + if r := recover(); r != nil { + p.recoverFromGoPanic(r) + } + }() + } + msg := cmd() switch msg := msg.(type) { case BatchMsg: @@ -534,12 +560,10 @@ func (p *Program) execBatchMsg(msg BatchMsg) { default: p.Send(msg) } - return nil - }) + }() } - //nolint:errcheck - g.Wait() // wait for all commands from batch msg to finish + wg.Wait() // wait for all commands from batch msg to finish } // Run initializes the program and runs its event loops, blocking until it gets diff --git a/tea_test.go b/tea_test.go index b45d80aa..dfd9a3dc 100644 --- a/tea_test.go +++ b/tea_test.go @@ -18,6 +18,10 @@ type incrementMsg struct{} type panicMsg struct{} +func panicCmd() Msg { + panic("testing goroutine panic behavior") +} + type testModel struct { executed atomic.Value counter atomic.Value @@ -434,14 +438,14 @@ func TestTeaNestedSequenceMsg(t *testing.T) { m := &testModel{} p := NewProgram(m, WithInput(&in), WithOutput(&buf)) - go p.Send(sequenceMsg{inc, Sequence(inc, inc), Quit}) + go p.Send(sequenceMsg{inc, Sequence(inc, inc, Batch(inc, inc)), Quit}) if _, err := p.Run(); err != nil { t.Fatal(err) } - if m.counter.Load() != 3 { - t.Fatalf("counter should be 3, got %d", m.counter.Load()) + if m.counter.Load() != 5 { + t.Fatalf("counter should be 5, got %d", m.counter.Load()) } } @@ -502,10 +506,6 @@ func TestTeaGoroutinePanic(t *testing.T) { var buf bytes.Buffer var in bytes.Buffer - panicCmd := func() Msg { - panic("testing goroutine panic behavior") - } - m := &testModel{} p := NewProgram(m, WithInput(&in), WithOutput(&buf)) go func() { @@ -513,8 +513,9 @@ func TestTeaGoroutinePanic(t *testing.T) { time.Sleep(time.Millisecond) if m.executed.Load() != nil { batch := make(BatchMsg, 10) - for i := range batch { - batch[i] = panicCmd + for i := 0; i < len(batch); i += 2 { + batch[i] = Sequence(panicCmd) + batch[i+1] = Batch(panicCmd) } p.Send(batch) return