fix: Check if program cancelReader is is nil before invoking

This commit fixes an issue where a user may provider a nil input via
`tea.WithInput(nil)`.  This option method does not check if the input
is nil and sets the `withCustomInput` attribute with a nil input.
This logic is sound since a Tea program may not necessarily want to
handle any inputs from users (such as those in non-TTY environments).

However, a nil pointer exception is thrown during `tea.Run` because a
`cancelReader` is always invoked after the main renderer.  However,
its instantiation is variable and dependent on whether an input is
provided. To mitigate against this, this commit checks if a
`cancelReader` is non-nil.

Signed-off-by: Alexander Jung <alex@nderjung.net>
This commit is contained in:
Alexander Jung 2023-01-30 11:41:44 +00:00 committed by Christian Muehlhaeuser
parent 1ad9f9c15c
commit 331a63bdca
1 changed files with 7 additions and 4 deletions

11
tea.go
View File

@ -474,11 +474,14 @@ func (p *Program) Run() (Model, error) {
// Tear down.
p.cancel()
// Wait for input loop to finish.
if p.cancelReader.Cancel() {
p.waitForReadLoop()
// Check if the cancel reader has been setup before waiting and closing.
if p.cancelReader != nil {
// Wait for input loop to finish.
if p.cancelReader.Cancel() {
p.waitForReadLoop()
}
_ = p.cancelReader.Close()
}
_ = p.cancelReader.Close()
// Wait for all handlers to finish.
handlers.shutdown()