From 331a63bdca9b7815a044294d9ceb15cb6363889e Mon Sep 17 00:00:00 2001 From: Alexander Jung Date: Mon, 30 Jan 2023 11:41:44 +0000 Subject: [PATCH] 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 --- tea.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tea.go b/tea.go index f5f452b..0c29a86 100644 --- a/tea.go +++ b/tea.go @@ -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()