fix: handle nil Process when exec command doesn't exist#16
Merged
Conversation
Why: otel-cli exec crashes with nil pointer when command not found (issue #8) Approach: Check for nil Process and ProcessState, return exit 127 to match shell Learned: Shell returns 127 for "command not found", principle of least surprise Next: Create PR and close issue Changes: - Check child.Process != nil before accessing Pid - Check child.ProcessState != nil before getting ExitCode - Return exit code 127 when command fails to start (matches shell behavior) - Sends span with error status even when command doesn't exist Tests: - Added test for exec with non-existent command - Verifies exit code 127 (same as shell) - Verifies span is sent with error status - Verifies no panic/crash Fixes #8 🤖 Claude <claude@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a nil pointer dereference panic that occurred when otel-cli exec is invoked with a non-existent command. The fix adds proper nil checks for Process and ProcessState before dereferencing them, and returns exit code 127 (matching standard shell behavior for "command not found") when a command fails to start.
Key changes:
- Added nil check for
child.Processbefore accessingPid - Added nil check for
child.ProcessStatebefore callingExitCode(), with fallback to exit code 127 - Added integration test to verify the fix prevents panic and returns correct exit code
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| otelcli/exec.go | Added nil checks for Process and ProcessState to prevent panic, with exit code 127 fallback |
| data_for_test.go | Added integration test verifying non-existent command handling |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes crash when running
otel-cli execwith a non-existent command by properly handling nil Process and ProcessState.Problem
Running
otel-cli exec command-that-does-not-existresulted in a panic:The crash occurred because when a command fails to start (e.g., command not found),
child.Processandchild.ProcessStatearenil, but the code attempted to dereference them without checking.Solution
Added nil checks before dereferencing:
child.Process != nilbefore accessingPidchild.ProcessState != nilbefore gettingExitCodeBehavior
Principle of least surprise:
otel-cli execnow behaves like running the command directly:Testing
Added integration test that verifies:
Related
Fixes #8
🤖 Generated with Claude Code
Co-Authored-By: Claude claude@anthropic.com