Skip to content

Conversation

@tobert
Copy link
Owner

@tobert tobert commented Nov 10, 2025

Summary

Fixes crash when running otel-cli exec with a non-existent command by properly handling nil Process and ProcessState.

Problem

Running otel-cli exec command-that-does-not-exist resulted in a panic:

panic: runtime error: invalid memory address or nil pointer dereference

The crash occurred because when a command fails to start (e.g., command not found), child.Process and child.ProcessState are nil, but the code attempted to dereference them without checking.

Solution

Added nil checks before dereferencing:

  • 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 for "command not found")
  • Still sends span with error status

Behavior

Principle of least surprise: otel-cli exec now behaves like running the command directly:

$ command-that-does-not-exist
bash: command-that-does-not-exist: command not found
$ echo $?
127

$ otel-cli exec command-that-does-not-exist
$ echo $?
127  # ✅ matches shell behavior

Testing

Added integration test that verifies:

  • ✅ No panic/crash
  • ✅ Exit code 127 (matches shell)
  • ✅ Span is sent with error status
  • ✅ Error message contains "exec command failed"

Related

Fixes #8

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

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 <[email protected]>
Copilot AI review requested due to automatic review settings November 10, 2025 02:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.Process before accessing Pid
  • Added nil check for child.ProcessState before calling ExitCode(), 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.

@tobert tobert merged commit 0b5188f into main Nov 10, 2025
1 check passed
@tobert tobert deleted the fix-exec-nonexistent-command branch November 10, 2025 02:29
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.

Null exception when invoking with non existant application

2 participants