Skip to content

Fix nil pointer dereference in GetPersistentShell #84

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

Merged

Conversation

AvicennaJr
Copy link
Contributor

Issue

The application was crashing with a "runtime error: invalid memory address or nil pointer dereference" in the shell tool. This occurred because the GetPersistentShell function was attempting to access properties of a potentially nil shellInstance. Here is the exact panic log:

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

Time: 2025-04-26T22:48:32+03:00

Stack Trace:
goroutine 139 [running]:
runtime/debug.Stack()
        /home/avi/go/pkg/mod/golang.org/[email protected]/src/runtime/debug/stack.go:26 +0x5e
github.com/opencode-ai/opencode/internal/logging.RecoverPanic({0x1b5d7c8, 0x9}, 0xc00005df90)
        /home/avi/go/pkg/mod/github.com/opencode-ai/[email protected]/internal/logging/logger.go:68 +0x325
panic({0x18426c0?, 0x2fa2350?})
        /home/avi/go/pkg/mod/golang.org/[email protected]/src/runtime/panic.go:792 +0x132
github.com/opencode-ai/opencode/internal/llm/tools/shell.GetPersistentShell({0x7ffe16419c39?, 0xc003d8a7b0?})
        /home/avi/go/pkg/mod/github.com/opencode-ai/[email protected]/internal/llm/tools/shell/shell.go:50 +0x4f
github.com/opencode-ai/opencode/internal/llm/tools.(*bashTool).Run(0xc00185a000, {0x217ff68, 0xc003d8a7b0}, {{0xc0007ba0e4, 0x1e}, {0xc0007ba126, 0x4}, {0xc0027ee468, 0x11}})
        /home/avi/go/pkg/mod/github.com/opencode-ai/[email protected]/internal/llm/tools/bash.go:287 +0x812
github.com/opencode-ai/opencode/internal/llm/agent.(*agent).streamAndHandleEvents(0xc0001cd8c0, {0x217ffa0, 0xc00059e9b0}, {0xc0006885a0, 0x24}, {0xc003d1cb40?, 0x23c?, 0x1730160?})
        /home/avi/go/pkg/mod/github.com/opencode-ai/[email protected]/internal/llm/agent/agent.go:313 +0xcad
github.com/opencode-ai/opencode/internal/llm/agent.(*agent).processGeneration(0xc0001cd8c0, {0x217ffa0, 0xc00059e9b0}, {0xc0006885a0, 0x24}, {0x1bd16c8, 0x23c})
        /home/avi/go/pkg/mod/github.com/opencode-ai/[email protected]/internal/llm/agent/agent.go:221 +0x645
github.com/opencode-ai/opencode/internal/llm/agent.(*agent).Run.func1()
        /home/avi/go/pkg/mod/github.com/opencode-ai/[email protected]/internal/llm/agent/agent.go:175 +0x1a5
created by github.com/opencode-ai/opencode/internal/llm/agent.(*agent).Run in goroutine 1
        /home/avi/go/pkg/mod/github.com/opencode-ai/[email protected]/internal/llm/agent/agent.go:169 +0x196

Root Cause

The newPersistentShell function can return nil in error cases (when failing to create stdin pipe or when cmd.Start() fails), but GetPersistentShell wasn't checking for this possibility before accessing shellInstance.isAlive.

Solution

Added a nil check in GetPersistentShell before accessing shellInstance.isAlive to safely handle cases where shell initialization fails. This prevents the panic and ensures the code attempts to create a new shell instance when needed.

Added nil check in GetPersistentShell before accessing
shellInstance.isAlive
to prevent panic when newPersistentShell returns nil due to shell
startup
errors. This resolves the "invalid memory address or nil pointer
dereference"
error that was occurring in the shell tool.
@@ -47,7 +47,7 @@ func GetPersistentShell(workingDir string) *PersistentShell {
shellInstance = newPersistentShell(workingDir)
})

if !shellInstance.isAlive {
if shellInstance == nil || !shellInstance.isAlive {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if shellInstance == nil || !shellInstance.isAlive {
if shellInstance != nil && !shellInstance.isAlive {

Is this what we should do instead ?

Copy link
Contributor Author

@AvicennaJr AvicennaJr Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we want to create a shell instance when it doesn't exist (ie when its nil) or when its not alive

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, but we need to change the next line also, we are trying to get .cwd from shellInstance and that shellInstance could be nil in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny thing is I was getting that nil error, but adding that nil check did do the fix. Not sure why it didn't panic on the next line 😆

Will find out the reason and modify it if needed

Copy link
Contributor Author

@AvicennaJr AvicennaJr Apr 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kujtimiihoxha I've updated the line to use the provided workingDir if shellInstance is nil, otherwise it will use cwd

@AvicennaJr AvicennaJr force-pushed the fh/fix-shell-nil-pointer-dereference branch from 330287a to a3dc97f Compare April 27, 2025 04:28
@kujtimiihoxha
Copy link
Collaborator

Thanks @AvicennaJr

@kujtimiihoxha kujtimiihoxha merged commit 8a4d415 into opencode-ai:main Apr 27, 2025
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.

2 participants