Skip to content

os/exec: sometimes Run doesn't return even though the process has finished #59055

@ghost

Description

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

$ go version
go version go1.20.2 linux/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=""
GOCACHE="/home/xxx/.cache/go-build"
GOENV="/home/xxx/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/xxx/gocode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/xxx/gocode"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/home/xxx/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/home/xxx/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1084134332=/tmp/go-build -gno-record-gcc-switches"

What did you do?

a.go:

package main

import (
	"fmt"
	"io"
	"os"
	"os/exec"
	"time"
)

func main() {
	cmd := exec.Command("./b")
	pr, pw := io.Pipe()
	_ = pr
	cmd.Stdout = pw
	cmd.Stderr = os.Stderr
	go func() {
		<-time.After(5 * time.Second)
		if err := cmd.Process.Signal(os.Interrupt); err != nil {
			fmt.Println("Signal:", err)
		}
		time.Sleep(time.Second)
		if err := cmd.Process.Kill(); err != nil {
			fmt.Println("Kill:", err)
		}
	}()
	if err := cmd.Run(); err != nil {
		fmt.Println("Run[a]:", err)
	}
	fmt.Println("Run returned.")
}

b.go:

package main

import (
	"fmt"
	"io"
	"os"
	"os/exec"
)

func main() {
	cmd := exec.Command("tail", "-f", "/dev/null")
	pr, pw := io.Pipe()
	_ = pw
	cmd.Stdin = pr
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Println("Run[b]:", err)
	}
}

After building the binaries and putting them in the same directory, I run ./a.

What did you expect to see?

The program prints Run returned. in 5 seconds and exits.

What did you see instead?

Kill: os: process already finished

The program doesn't exit until I press Ctrl+C.

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