Skip to content

fix: resolve nil pointer dereference panic in shell tool #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PierrunoYT
Copy link

@PierrunoYT PierrunoYT commented Jun 8, 2025

Problem

Fixed a critical nil pointer dereference panic that was occurring in the shell tool:

Panic in agent.Run: runtime error: invalid memory address or nil pointer dereference

The panic was happening in PersistentShell.Exec when trying to access s.commandQueue on a nil or improperly initialized shell instance.

Fixes #199

Root Cause

The issue occurred because:

  1. newPersistentShell could return nil if shell creation failed (e.g., cmd.StdinPipe() or cmd.Start() errors)
  2. GetPersistentShell didn't properly handle the case where newPersistentShell returned nil
  3. The Exec method didn't have nil safety checks

Solution

Enhanced Error Handling

  • GetPersistentShell: Added proper nil checks and returns a disabled shell instance instead of nil when shell creation fails
  • Exec Method: Added comprehensive nil safety checks for shell instance, commandQueue, and stdin
  • Close Method: Added nil safety checks to prevent cleanup-related panics

Improved Logging

  • Added error logging in newPersistentShell when shell creation fails
  • Better visibility into why shell creation might fail

Comprehensive Testing

  • Created shell_test.go with tests for all nil pointer scenarios
  • Tests verify that nil operations return errors instead of panicking
  • All edge cases covered and tested

Changes Made

  • ✅ Add nil safety checks in PersistentShell.Exec method
  • ✅ Add nil safety checks in PersistentShell.Close method
  • ✅ Improve error handling in GetPersistentShell when shell creation fails
  • ✅ Add better error logging in newPersistentShell
  • ✅ Add comprehensive tests for nil pointer safety
  • ✅ Ensure shell instance returns disabled shell instead of nil when creation fails

Testing

go test ./internal/llm/tools/shell/ -v

All tests pass successfully, including:

  • Nil shell instance handling
  • Nil commandQueue handling
  • Shell not alive scenarios
  • File helper functions
  • Shell quoting functionality

Impact

  • Before: Application would crash with nil pointer dereference panic (as described in Shell tool crashes with nil pointer dereference #199)
  • After: Shell tool gracefully handles failure scenarios and returns appropriate error messages
  • Backward Compatible: No breaking changes to existing functionality

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

runtime error: invalid memory address or nil pointer dereference
Location: internal/llm/tools/shell.(*PersistentShell).Exec line 272

✅ Non-Interactive Mode Crash Fixed

Error: agent processing failed: panic while running the agent
Command: ./opencode.exe -p "test prompt" -d

With this fix, shell commands should now execute without crashing the application on Windows 10 with PowerShell.

- Add nil safety checks in PersistentShell.Exec method
- Add nil safety checks in PersistentShell.Close method
- Improve error handling in GetPersistentShell when shell creation fails
- Add better error logging in newPersistentShell
- Add comprehensive tests for nil pointer safety
- Ensure shell instance returns disabled shell instead of nil when creation fails

Fixes panic: runtime error: invalid memory address or nil pointer dereference
in agent.Run when shell.Exec is called on a nil or improperly initialized shell instance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shell tool crashes with nil pointer dereference
1 participant