ClosePrompt nil check closes #4788 (#4827)

* Add test demonstrating bug #4788 - ClosePrompt panics with nil

Adds TestClosePromptNilCancelPanic that reproduces the bug where
ClosePrompt() panics with a nil pointer dereference when cancelPrompt
is nil. This can happen if ClosePrompt is called before the prompt is
fully initialized.

Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix #4788: Add nil check before calling cancelPrompt

Prevents panic in ClosePrompt() when cancelPrompt is nil.
This can happen if ClosePrompt is called before the prompt
is fully initialized.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Nathan Wallace
2025-11-15 12:38:56 -05:00
committed by GitHub
parent b6aa7fc7d5
commit a202100395
2 changed files with 29 additions and 1 deletions

View File

@@ -220,6 +220,31 @@ func TestClosePrompt(t *testing.T) {
}
}
// TestClosePromptNilCancelPanic tests that ClosePrompt doesn't panic
// when cancelPrompt is nil.
//
// This can happen if ClosePrompt is called before the prompt is fully
// initialized or after manual nil assignment.
//
// Bug: #4788
func TestClosePromptNilCancelPanic(t *testing.T) {
// Create an InteractiveClient with nil cancelPrompt
c := &InteractiveClient{
cancelPrompt: nil,
}
// This should not panic
defer func() {
if r := recover(); r != nil {
t.Errorf("ClosePrompt() panicked with nil cancelPrompt: %v", r)
}
}()
// Call ClosePrompt with nil cancelPrompt
// This will panic without the fix
c.ClosePrompt(AfterPromptCloseExit)
}
// TestContextCancellationPropagation tests that parent context cancellation propagates
func TestContextCancellationPropagation(t *testing.T) {
c := &InteractiveClient{}

View File

@@ -169,7 +169,10 @@ func (c *InteractiveClient) InteractivePrompt(parentContext context.Context) {
// ClosePrompt cancels the running prompt, setting the action to take after close
func (c *InteractiveClient) ClosePrompt(afterClose AfterPromptCloseAction) {
c.afterClose = afterClose
c.cancelPrompt()
// only call cancelPrompt if it is not nil (to prevent panic)
if c.cancelPrompt != nil {
c.cancelPrompt()
}
}
// retrieve both the raw query result and a sanitised version in list form