fix: resolve nil pointer dereference panic in shell tool #215
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Fixed a critical nil pointer dereference panic that was occurring in the shell tool:
The panic was happening in
PersistentShell.Exec
when trying to accesss.commandQueue
on a nil or improperly initialized shell instance.Fixes #199
Root Cause
The issue occurred because:
newPersistentShell
could returnnil
if shell creation failed (e.g.,cmd.StdinPipe()
orcmd.Start()
errors)GetPersistentShell
didn't properly handle the case wherenewPersistentShell
returnednil
Exec
method didn't have nil safety checksSolution
Enhanced Error Handling
GetPersistentShell
: Added proper nil checks and returns a disabled shell instance instead of nil when shell creation failsExec
Method: Added comprehensive nil safety checks for shell instance, commandQueue, and stdinClose
Method: Added nil safety checks to prevent cleanup-related panicsImproved Logging
newPersistentShell
when shell creation failsComprehensive Testing
shell_test.go
with tests for all nil pointer scenariosChanges Made
PersistentShell.Exec
methodPersistentShell.Close
methodGetPersistentShell
when shell creation failsnewPersistentShell
Testing
go test ./internal/llm/tools/shell/ -v
All tests pass successfully, including:
Impact
This fix ensures the application remains stable even when shell initialization fails, improving overall reliability and user experience.
Addresses Issue #199 Scenarios
This PR specifically addresses the crash scenarios mentioned in #199:
✅ Shell Command Execution Crash Fixed
✅ Non-Interactive Mode Crash Fixed
With this fix, shell commands should now execute without crashing the application on Windows 10 with PowerShell.