fix: avoid blocking prints after program exit (#1758)

Co-authored-by: Ricardo-M-L <XuthodoreveraynWX61@elvisfan.com>
Co-authored-by: Drew Smirnoff <drew@charm.land>
This commit is contained in:
Ricardo-M-L 2026-11-12 21:23:24 +08:00 committed by GitHub
parent 418cada888
commit 7a446b73f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 4 deletions

10
tea.go
View file

@ -1474,10 +1474,11 @@ func (p *Program) RestoreTerminal() error {
// and will persist across renders by the Program.
//
// If the altscreen is active no output will be printed.
// If the program has already exited this is a no-op.
func (p *Program) Println(args ...any) {
p.msgs <- printLineMessage{
p.Send(printLineMessage{
messageBody: fmt.Sprint(args...),
}
})
}
// Printf prints above the Program. It takes a format template followed by
@ -1488,10 +1489,11 @@ func (p *Program) Println(args ...any) {
// its own line.
//
// If the altscreen is active no output will be printed.
// If the program has already exited this is a no-op.
func (p *Program) Printf(template string, args ...any) {
p.msgs <- printLineMessage{
p.Send(printLineMessage{
messageBody: fmt.Sprintf(template, args...),
}
})
}
// startRenderer starts the renderer.

View file

@ -560,6 +560,49 @@ func TestTeaSend(t *testing.T) {
p.Send(Quit())
}
func TestProgramPrintAfterExit(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
var in bytes.Buffer
p := NewProgram(&testModel{},
WithInput(&in),
WithOutput(&buf),
)
go p.Send(Quit())
if _, err := p.Run(); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
print func()
}{
{"Println", func() { p.Println("after exit") }},
{"Printf", func() { p.Printf("after %s", "exit") }},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
done := make(chan struct{})
go func() {
tt.print()
close(done)
}()
select {
case <-done:
case <-time.After(100 * time.Millisecond):
// Receive only after detecting the block. This releases the
// direct channel send used before this regression was fixed,
// so the test does not leak a goroutine against the old code.
<-p.msgs
<-done
t.Fatal("print blocked after the program exited")
}
})
}
}
func TestTeaNoRun(t *testing.T) {
t.Parallel()
var buf bytes.Buffer