Skip to content
Myr

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

2 files+41 added−16 removed

modified:tea_test.go+10 added-9 removed
16 unmodified lines
17type incrementMsg struct{}1819type panicMsg struct{}2021func panicCmd() Msg {22 panic("testing goroutine panic behavior")23}2425type testModel struct {26 executed atomic.Value27 counter atomic.Value28}
408 unmodified lines
437 }438439 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})442443 if _, err := p.Run(); err != nil {444 t.Fatal(err)445 }446443 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}451452func TestTeaSend(t *testing.T) {
52 unmodified lines
505func TestTeaGoroutinePanic(t *testing.T) {506 var buf bytes.Buffer507 var in bytes.Buffer508505 panicCmd := func() Msg {506 panic("testing goroutine panic behavior")507 }508509 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 lines
modified:tea.go+31 added-7 removed
497 unmodified lines
498 }499}500501func (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 }509510 // Execute commands one at a time, in order.511 for _, cmd := range msg {512 if cmd == nil {513 continue
10 unmodified lines
524 }525}526527func (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 }535536 // 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()545546 if !p.startupOptions.has(withoutCatchPanics) {547 defer func() {548 if r := recover(); r != nil {549 p.recoverFromGoPanic(r)550 }551 }()552 }553554 msg := cmd()555 switch msg := msg.(type) {556 case BatchMsg:557 p.execBatchMsg(msg)
1 unmodified line
559 p.execSequenceMsg(msg)560 default:561 p.Send(msg)562 }537 return nil538 })563 }()564 }565541 //nolint:errcheck542 g.Wait() // wait for all commands from batch msg to finish566 wg.Wait() // wait for all commands from batch msg to finish567}568569// 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