Skip to content

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

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

Closed
futurist opened this issue Mar 2, 2023 · 1 comment
Closed

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

futurist opened this issue Mar 2, 2023 · 1 comment

Comments

@futurist
Copy link
Contributor

futurist commented Mar 2, 2023

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
@futurist futurist changed the title affected/package: os/exec affected/package: os/exec panic with CommandContext Mar 2, 2023
@seankhliao
Copy link
Member

The bug is in your code, Cmd shouldn't be copied.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2023
@golang golang locked and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants