fix: recover from nested panics in Sequence and Batch commands
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ayman Bagabas · committed by Ayman Bagabas · parent 0966c3a · Browse files · 2 files·+41 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+41 added−16 removed
- other:+41 added−16 removed
Provenance
- Commit
9e0e8f0- Tree
1acbb0c- Author
Ayman Bagabas <ayman.bagabas@gmail.com>- Committer
Ayman Bagabas <ayman.bagabas@gmail.com>- Parent
0966c3a
2 files changed+41 added−16 removed
modified:tea_test.go+10 added-9 removed
16 unmodified lines17 type incrementMsg struct{}18 19 type panicMsg struct{}20 21+func panicCmd() Msg {22+ panic("testing goroutine panic behavior")23+}24+25 type testModel struct {26 executed atomic.Value27 counter atomic.Value28 }408 unmodified lines437 }438 439 m := &testModel{}440 p := NewProgram(m, WithInput(&in), WithOutput(&buf))437- go p.Send(sequenceMsg{inc, Sequence(inc, inc), Quit})441+ go p.Send(sequenceMsg{inc, Sequence(inc, inc, Batch(inc, inc)), Quit})442 443 if _, err := p.Run(); err != nil {444 t.Fatal(err)445 }446 443- if m.counter.Load() != 3 {444- t.Fatalf("counter should be 3, got %d", m.counter.Load())447+ if m.counter.Load() != 5 {448+ t.Fatalf("counter should be 5, got %d", m.counter.Load())449 }450 }451 452 func TestTeaSend(t *testing.T) {52 unmodified lines505 func TestTeaGoroutinePanic(t *testing.T) {506 var buf bytes.Buffer507 var in bytes.Buffer508 505- panicCmd := func() Msg {506- panic("testing goroutine panic behavior")507- }508-509 m := &testModel{}510 p := NewProgram(m, WithInput(&in), WithOutput(&buf))511 go func() {512 for {513 time.Sleep(time.Millisecond)514 if m.executed.Load() != nil {515 batch := make(BatchMsg, 10)516- for i := range batch {517- batch[i] = panicCmd516+ for i := 0; i < len(batch); i += 2 {517+ batch[i] = Sequence(panicCmd)518+ batch[i+1] = Batch(panicCmd)519 }520 p.Send(batch)521 return522 }13 unmodified linesmodified:tea.go+31 added-7 removed
497 unmodified lines498 }499 }500 501 func (p *Program) execSequenceMsg(msg sequenceMsg) {502+ if !p.startupOptions.has(withoutCatchPanics) {503+ defer func() {504+ if r := recover(); r != nil {505+ p.recoverFromGoPanic(r)506+ }507+ }()508+ }509+510 // Execute commands one at a time, in order.511 for _, cmd := range msg {512 if cmd == nil {513 continue10 unmodified lines524 }525 }526 527 func (p *Program) execBatchMsg(msg BatchMsg) {528+ if !p.startupOptions.has(withoutCatchPanics) {529+ defer func() {530+ if r := recover(); r != nil {531+ p.recoverFromGoPanic(r)532+ }533+ }()534+ }535+536 // Execute commands one at a time.521- g, _ := errgroup.WithContext(p.ctx)537+ var wg sync.WaitGroup538 for _, cmd := range msg {523- cmd := cmd539 if cmd == nil {540 continue541 }527- g.Go(func() error {542+ wg.Add(1)543+ go func() {544+ defer wg.Done()545+546+ if !p.startupOptions.has(withoutCatchPanics) {547+ defer func() {548+ if r := recover(); r != nil {549+ p.recoverFromGoPanic(r)550+ }551+ }()552+ }553+554 msg := cmd()555 switch msg := msg.(type) {556 case BatchMsg:557 p.execBatchMsg(msg)1 unmodified line559 p.execSequenceMsg(msg)560 default:561 p.Send(msg)562 }537- return nil538- })563+ }()564 }565 541- //nolint:errcheck542- g.Wait() // wait for all commands from batch msg to finish566+ wg.Wait() // wait for all commands from batch msg to finish567 }568 569 // Run initializes the program and runs its event loops, blocking until it gets570 // terminated by either [Program.Quit], [Program.Kill], or its signal handler.365 unmodified lines