Skip to content

affected/package: os/exec panic with CommandContext #58828

Closed as not planned
Closed as not planned
@futurist

Description

@futurist

What version of Go are you using (go version)?

$ go version
go version go1.20.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN="/Users/jm/go/bin"
GOCACHE="/Users/jm/Library/Caches/go-build"
GOENV="/Users/jm/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/jm/go/pkg/mod"
GOOS="darwin"
GOPATH="/Users/jm/go"
GOROOT="/Users/jm/.gobrew/versions/1.20.1/go"
GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOTOOLDIR="/Users/jm/.gobrew/versions/1.20.1/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ml/wcc43cv538gg10sbkqd_z7980000gp/T/go-build2586553286=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run below command with go1.20.1:

package main

import (
	"context"
	"os/exec"
	"time"
)

type Command struct {
	exec.Cmd
	Cancel context.CancelFunc
}

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	go func() {
		time.Sleep(time.Millisecond * 100)
		cancel()
	}()
	c := New("sleep 1; echo ok")
	go func() {
		<-ctx.Done()
		c.Cancel()
	}()
	c.Run()
}

func New(command string) *Command {
	ctx, cancel := context.WithCancel(context.Background())
	cmd := exec.CommandContext(ctx, "bash", "-c", command)
	c := &Command{
		Cmd:    *cmd,
		Cancel: cancel,
	}
	return c
}

What did you expect to see?

Command should run without any panic.

What did you see instead?

Panic with below error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x107baf9]

goroutine 20 [running]:
os.(*Process).signal(0x0?, {0x10bbfd0?, 0x111e448?})
        /Users/jm/.gobrew/versions/1.20.1/go/src/os/exec_unix.go:63 +0x39
os.(*Process).Signal(...)
        /Users/jm/.gobrew/versions/1.20.1/go/src/os/exec.go:138
os.(*Process).kill(...)
        /Users/jm/.gobrew/versions/1.20.1/go/src/os/exec_posix.go:67
os.(*Process).Kill(...)
        /Users/jm/.gobrew/versions/1.20.1/go/src/os/exec.go:123
os/exec.CommandContext.func1()
        /Users/jm/.gobrew/versions/1.20.1/go/src/os/exec/exec.go:449 +0x33
os/exec.(*Cmd).watchCtx(0xc000134000, 0xc000102120)
        /Users/jm/.gobrew/versions/1.20.1/go/src/os/exec/exec.go:765 +0xf3
created by os/exec.(*Cmd).Start
        /Users/jm/.gobrew/versions/1.20.1/go/src/os/exec/exec.go:743 +0x9f3
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions