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

os/exec: undocumented breaking change in exec.Command behavior on Windows between Go versions 1.16.8 and 1.17.1 #48393

Open
mbushkov opened this issue Sep 14, 2021 · 2 comments

Comments

@mbushkov
Copy link

@mbushkov mbushkov commented Sep 14, 2021

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

$ go version
go version go1.17.1 windows/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
go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\mbushkov\AppData\Local\go-build
set GOENV=C:\Users\mbushkov\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\mbushkov\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\mbushkov\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.1
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\mbushkov\AppData\Local\Temp\2\go-build3022852068=/tmp/go-build -gno-record-gcc-switches

What did you do?

On Windows, when using exec.Command and relying on inheriting handles and using inherited handles in the child process, code that worked when compiled Go 1.16.8 no longer works when compiled with Go 1.17.1.

The culprit seems to be the AdditionalInheritedHandles attribute introduced in the context of #44011, and particularly the following breaking change: 2d76081#diff-ec673c10a75fe2d2faa9c788e03823294b263c68cc3de50f4a0865a53e28f3a3R340. In Go 1.16, on Windows, child processes inherit all handles marked as inherited. In Go 1.17 the handles to be inherited have to be explicitly added to the AdditionalInheritedHandles list.

Sample program:

package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"syscall"
)

func main() {
	var in_handle *os.File = nil

	for _, e := range os.Environ() {
		pair := strings.SplitN(e, "=", 2)
		if pair[0] == "INHERITED_FD" {
			fd, err := strconv.Atoi(pair[1])
			if err != nil {
				log.Fatalf("Can't parse INHERITED_FD")
			}
			in_handle = os.NewFile(uintptr(fd), "file")
		}
	}

	if in_handle == nil {
		self_exe := os.Args[0]
		fmt.Printf("Haven't seen INHERITED_FD: assuming being a parent process, starting child with %s\n", self_exe)

		pr, pw, err := os.Pipe()
		if err != nil {
			log.Fatalf("Can't create pipe")
		}

		cmd := exec.Command(self_exe)

		fd := pr.Fd()
		syscall.SetHandleInformation(syscall.Handle(fd), syscall.HANDLE_FLAG_INHERIT, 1)
		cmd.SysProcAttr = &syscall.SysProcAttr{}
		// NOTE: The code fails without this line on 17.1:
		// cmd.SysProcAttr.AdditionalInheritedHandles = append(cmd.SysProcAttr.AdditionalInheritedHandles, syscall.Handle(fd))

		cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%d", "INHERITED_FD", fd))

		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		if err := cmd.Start(); err != nil {
			log.Fatalf("Couldn't start: %v", err)
		}

		n, err := pw.Write([]byte("blah"))
		if err != nil {
			log.Fatalf("Couldn't write to pipe")
		}
		fmt.Printf("Written %d bytes\n", n)
		pw.Close()

		if err := cmd.Wait(); err != nil {
			log.Fatalf("Waiting for process failed: %v", err)
		}
	} else {
		fmt.Printf("Found INHERITED_FD (%v), assuming being a child process\n", in_handle.Fd())
		data := make([]byte, 4)
		n, err := in_handle.Read(data)
		if err != nil {
			log.Fatalf("Can't read data from inherited fd")
		}
		fmt.Printf("Read %d bytes: %v\n", n, string(data))
	}
}

Running this on different versions of Go produces different results:

$ go1.16.8 build inherited_attrs.go

$ inherited_attrs.exe
Haven't seen INHERITED_FD: assuming being a parent process, starting child with inherited_attrs.exe
Written 4 bytes
Found INHERITED_FD (312), assuming being a child process
Read 4 bytes: blah

$ go1.17.1 build inherited_attrs.go

$ inherited_attrs.exe
Haven't seen INHERITED_FD: assuming being a parent process, starting child with inherited_attrs.exe
Written 4 bytes
Found INHERITED_FD (320), assuming being a child process
2021/09/14 22:03:08 Can't read data from inherited fd
2021/09/14 22:03:08 Waiting for process failed: exit status 1

What did you expect to see?

I expected one of the following:

  • Either that the output of the code compiled with Go 1.16.8 and Go 1.17.1 to be the same
  • Or that the breaking change in exec.Command's behavior on Windows to be clearly marked as such in release notes.

What did you see instead?

Same code compiled with Go 1.16.8 and Go 1.17.1 exhibits different behavior.

The underlying breaking change in Go's behavior is not clearly marked as such in Go 1.17 release notes. The current document states:

The Windows version of SysProcAttr has two new fields. AdditionalInheritedHandles is a list of additional handles to be inherited by the new child process. ParentProcess permits specifying the parent process of the new process.

This is misleading and seems to actually be incorrect, as only the handles list in AdditionalInheritedHandles are going to be inherited by the new child process.

@ALTree ALTree changed the title Undocumented breaking change in exec.Command behavior on Windows between Go versions 1.16.8 and 1.17.1 os/exec: undocumented breaking change in exec.Command behavior on Windows between Go versions 1.16.8 and 1.17.1 Sep 14, 2021
@ALTree ALTree added this to the Go1.18 milestone Sep 14, 2021
@cespare
Copy link
Contributor

@cespare cespare commented Sep 14, 2021

@bcmills
Copy link
Member

@bcmills bcmills commented Sep 15, 2021

Is this a duplicate of (or related to) #48040?

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
5 participants