Skip to content

Commit

Permalink
fix: panic occurring when the program execution fails (#60)
Browse files Browse the repository at this point in the history
* fix: panic occurring when the program execution fails
  • Loading branch information
alessio-perugini authored Dec 22, 2024
1 parent 4be81b6 commit 1de5e0f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ by passing the function name symbol and the binary args.

opts := captor.CaptureOptions{
CommandOutput: commandOutput,
CommandError: commandError,
LibbpfOutput: libbpfOutput,
Interval: dumpInterval,
}
Expand Down
11 changes: 7 additions & 4 deletions internal/executor/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package executor
import (
"bufio"
"fmt"
"os"
"os/exec"
"sync"
)
Expand All @@ -11,15 +12,19 @@ import (
// The cmdOutput argument is used to print the command output.
func Run(cmd []string, cmdOutput, cmdError bool, wg *sync.WaitGroup, outputCh, errorCh chan<- string) {
defer func() {
close(outputCh)
close(errorCh)
wg.Done()
}()

command := exec.Command(cmd[0], cmd[1:]...)
stdout, _ := command.StdoutPipe()
stderr, _ := command.StderrPipe()

//command.Wait()
command.Start()
if err := command.Start(); err != nil {
fmt.Fprintf(os.Stderr, "command execution error: %v\n", err)
return
}

go func() {
if cmdOutput {
Expand All @@ -43,8 +48,6 @@ func Run(cmd []string, cmdOutput, cmdError bool, wg *sync.WaitGroup, outputCh, e
}()

command.Wait()
close(outputCh)
close(errorCh)
}

func Build(packagePath, outputFile string) (string, error) {
Expand Down
6 changes: 6 additions & 0 deletions tests/integration.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ cmp profile.json expected-profile.json
exec harpoon capture -h
stdout 'Usage:'

# test it doesn't panic when the file is not executable
exec chmod -x bin/example-app
exec harpoon capture -e -c -f main.main -- ./bin/example-app
stderr 'command execution error: fork/exec .* permission denied'
exec chmod +x bin/example-app

# setting up test application
exists bin/example-app
# this must be done within the testscript environment
Expand Down

0 comments on commit 1de5e0f

Please sign in to comment.