Skip to content

cmd/go: run does not relay signals to child process #40467

@myitcv

Description

@myitcv

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

$ go version
go version devel +71218dbc40 Wed Jul 22 21:31:19 2020 +0000 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/myitcv/.cache/go-build"
GOENV="/home/myitcv/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/myitcv/gostuff/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/myitcv/gostuff"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/myitcv/gos"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build722565624=/tmp/go-build -gno-record-gcc-switches"

What did you do?

# This will correctly exit
exec go run parent.go build

# This will block
exec go run parent.go


-- parent.go --
package main

import (
	"bufio"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
)

func main() {
	var cmdArgs []string

	if len(os.Args) == 2 && os.Args[1] == "build" {
		td, err := ioutil.TempDir("", "")
		if err != nil {
			log.Fatalf("failed to create temp dir: %v", err)
		}
		defer os.RemoveAll(td)
		out := filepath.Join(td, "child")
		build := exec.Command("go", "build", "-o", out, "child.go")
		if out, err := build.CombinedOutput(); err != nil {
			log.Fatalf("failed to run [%v]: %v\n%s", build, err, out)
		}
		cmdArgs = []string{out}
	} else {
		cmdArgs = []string{"go", "run", "child.go"}
	}

	r, w := io.Pipe()

	done := make(chan struct{})
	child := exec.Command(cmdArgs[0], cmdArgs[1:]...)
	child.Stdout = w

	if err := child.Start(); err != nil {
		log.Fatalf("failed to start child [%v]: %v", child, err)
	}

	go func() {
		if err := child.Wait(); err != nil {
			log.Fatalf("child process [%v] failed: %v", child, err)
		}
		close(done)
	}()

	// Continue once we have read the first line
	if _, _, err := bufio.NewReader(r).ReadLine(); err != nil {
		log.Fatalf("failed to read start line from child: %v", err)
	}

	if err := child.Process.Signal(os.Interrupt); err != nil {
		log.Fatalf("failed to notify child process")
	}

	<-done
}
-- child.go --
package main

import (
	"fmt"
	"os"
	"os/signal"
)

func main() {
	sigint := make(chan os.Signal, 1)
	signal.Notify(sigint, os.Interrupt)
	fmt.Println("Started")
	<-sigint
}

What did you expect to see?

The interrupt signal to be passed to the child process by go run, I think?

What did you see instead?

The go run child process does not relay the interrupt signal to the child process.

cc @bcmills @jayconrod @matloob

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.help wanted

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions