Skip to content
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

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

Open
myitcv opened this issue Jul 29, 2020 · 0 comments
Open

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

myitcv opened this issue Jul 29, 2020 · 0 comments

Comments

@myitcv
Copy link
Member

@myitcv myitcv commented Jul 29, 2020

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

@bcmills bcmills added this to the Backlog milestone Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.