fix: allow Program.Run to be called again after the program exits (#1890)

Signed-off-by: simpleqt <2015900818@qq.com>
This commit is contained in:
陈志谦 2026-11-12 21:21:35 +08:00 committed by GitHub
parent 5982a61ef5
commit 55ba0f6de3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 85 additions and 0 deletions

17
tea.go
View file

@ -526,6 +526,10 @@ type Program struct {
runMu sync.Mutex
shutdownOnce sync.Once
// ranOnce reports whether [Program.Run] has been called before, so its
// per-run state can be reinitialized on subsequent runs.
ranOnce bool
profile *colorprofile.Profile // the terminal color profile
// where to send output, this will usually be os.Stdout.
@ -1051,6 +1055,19 @@ func (p *Program) Run() (returnModel Model, returnErr error) {
}()
// Initialize context and teardown channel.
//
// When a previous run already tore down its per-run state, recreate it so
// the Program can be run again. The context is only recreated after an
// actual run: replacing it unconditionally would swallow a
// [Program.Kill] issued before the program started, which works by
// cancelling the context created in NewProgram.
if p.ranOnce {
p.ctx, p.cancel = context.WithCancel(p.externalCtx)
p.errs = make(chan error, 1)
p.shutdownOnce = sync.Once{}
}
p.ranOnce = true
p.handlers = channelHandlers{}
cmds := make(chan Cmd)

View file

@ -572,6 +572,74 @@ func TestTeaNoRun(t *testing.T) {
)
}
// runTwiceModel waits for a WindowSizeMsg before quitting, and reports whether
// that message ever reached Update, so we can tell a real run from a
// torn-down one.
type runTwiceModel struct {
updated atomic.Bool
}
// Init returns no command on purpose: the program exits from Update once it
// has seen a WindowSizeMsg, so the quit is ordered after the observation
// rather than racing it.
func (m *runTwiceModel) Init() Cmd { return nil }
func (m *runTwiceModel) Update(msg Msg) (Model, Cmd) {
if _, ok := msg.(WindowSizeMsg); ok {
m.updated.Store(true)
return m, Quit
}
return m, nil
}
func (m *runTwiceModel) View() View { return NewView("hello") }
// TestTeaRunTwice is a regression test for running a Program a second time
// after the first Run returned. Run resets its per-run state (handlers,
// finished channel) at the top of every call, but before the fix the run
// context, error channel, and shutdown guard stayed behind: the second run
// tore down immediately with ErrProgramKilled and its shutdown was skipped,
// leaving the terminal state it changed unrestored.
func TestTeaRunTwice(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
var in bytes.Buffer
m := &runTwiceModel{}
p := NewProgram(m,
WithInput(&in),
WithOutput(&buf),
)
// The program quits from Update once it has observed a WindowSizeMsg, so
// every run returns on its own; a bounded wait keeps a genuinely broken
// run from hanging the test instead of failing it.
run := func(which string) {
t.Helper()
done := make(chan error, 1)
go func() {
_, err := p.Run()
done <- err
}()
select {
case err := <-done:
if err != nil {
t.Fatalf("%s run: unexpected error: %v", which, err)
}
case <-time.After(30 * time.Second):
t.Fatalf("%s run: Run did not return; the program never observed a WindowSizeMsg", which)
}
}
run("first")
m.updated.Store(false)
run("second")
if !m.updated.Load() {
t.Error("second run never delivered messages to Update")
}
}
func TestTeaPanic(t *testing.T) {
t.Parallel()
var buf bytes.Buffer