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

3
tea.go
View File

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