From 5564b1ea1f85590d0b4f9dceb1e752fd6f182f84 Mon Sep 17 00:00:00 2001 From: zikaeroh Date: Wed, 5 Feb 2020 00:08:27 -0800 Subject: [PATCH] internal/version: ignore signals intended for the child process Since the underlying toolchain is run as a child process (and not exec'd), signals that are sent to this process (for example, an interrupt sent by a shell) will be handled by both the parent and the child. This poses a problem for signals like SIGQUIT, where both processes' runtimes attempt to write out information to the same output. Before running the final command, start a signal handler that will ignore a signal sent to the parent, emulating the behavior of go run. This code is a modified version of cmd/go/internal/base. Fixes golang/go#36976. Change-Id: I29aa699dc059fd448aa9478f729ed8d90379616b Reviewed-on: https://go-review.googlesource.com/c/dl/+/217765 Trust: Emmanuel Odeke Trust: Dmitri Shuralyov Trust: Filippo Valsorda Run-TryBot: Emmanuel Odeke TryBot-Result: Go Bot Reviewed-by: Filippo Valsorda --- internal/version/signal_notunix.go | 14 ++++++++++++++ internal/version/signal_unix.go | 15 +++++++++++++++ internal/version/version.go | 10 ++++++++++ 3 files changed, 39 insertions(+) create mode 100644 internal/version/signal_notunix.go create mode 100644 internal/version/signal_unix.go diff --git a/internal/version/signal_notunix.go b/internal/version/signal_notunix.go new file mode 100644 index 00000000..506655d1 --- /dev/null +++ b/internal/version/signal_notunix.go @@ -0,0 +1,14 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build plan9 || windows +// +build plan9 windows + +package version + +import ( + "os" +) + +var signalsToIgnore = []os.Signal{os.Interrupt} diff --git a/internal/version/signal_unix.go b/internal/version/signal_unix.go new file mode 100644 index 00000000..e9b9fb9a --- /dev/null +++ b/internal/version/signal_unix.go @@ -0,0 +1,15 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build aix || darwin || dragonfly || freebsd || js || linux || netbsd || openbsd || solaris +// +build aix darwin dragonfly freebsd js linux netbsd openbsd solaris + +package version + +import ( + "os" + "syscall" +) + +var signalsToIgnore = []os.Signal{os.Interrupt, syscall.SIGQUIT} diff --git a/internal/version/version.go b/internal/version/version.go index 866e4adc..98e5d4a9 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -18,6 +18,7 @@ import ( "net/http" "os" "os/exec" + "os/signal" "os/user" "path" "path/filepath" @@ -64,6 +65,9 @@ func runGo(root string) { newPath += string(filepath.ListSeparator) + p } cmd.Env = dedupEnv(caseInsensitiveEnv, append(os.Environ(), "GOROOT="+root, "PATH="+newPath)) + + handleSignals() + if err := cmd.Run(); err != nil { // TODO: return the same exit status maybe. os.Exit(1) @@ -500,3 +504,9 @@ func dedupEnv(caseInsensitive bool, env []string) []string { } return out } + +func handleSignals() { + // Ensure that signals intended for the child process are not handled by + // this process' runtime (e.g. SIGQUIT). See issue #36976. + signal.Notify(make(chan os.Signal), signalsToIgnore...) +}