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

affected/package: os/exec, context not work. #53700

Closed
lixd opened this issue Jul 6, 2022 · 1 comment
Closed

affected/package: os/exec, context not work. #53700

lixd opened this issue Jul 6, 2022 · 1 comment

Comments

@lixd
Copy link

lixd commented Jul 6, 2022

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

$ go version
go version go1.18 darwin/arm64

Does this issue reproduce with the latest release?

reproduce in go1.18.3

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/lixueduan/Library/Caches/go-build"
GOENV="/Users/lixueduan/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/lixueduan/17x/dev/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/lixueduan/17x/dev/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn"
GOROOT="/Users/lixueduan/17x/env/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/lixueduan/17x/env/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/lixueduan/17x/99cloud/kubeclipper/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/r5/vby20fm56t3g3bhydvcn897h0000gn/T/go-build3766380095=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	cmd := exec.CommandContext(ctx, "bash","/root/sleep.sh")
	out, err := cmd.CombinedOutput()
	fmt.Printf("ctx.Err : [%v]\n", ctx.Err())
	fmt.Printf("error   : [%v]\n", err)
	fmt.Printf("out     : [%s]\n", string(out))
}

and /root/sleep.sh:

#!/bin/bash
sleep 1200

run this

go run main.go

What did you expect to see?

when timeout, the program exits.

What did you see instead?

go run main.go will create a bash process and a sleep process, and when timeout, the bash process will be killed, and sleep process will hosting by process which pid=1.
but cmd.CombinedOutput() not exit.

Others

image

after timeout

image

// os/exec/exec.go
func (c *Cmd) Start() error {
         // ...
	if len(c.goroutine) > 0 {
		c.errch = make(chan error, len(c.goroutine))
		for _, fn := range c.goroutine {
			go func(fn func() error) {
				// will block in fn()
				c.errch <- fn()
			}(fn)
		}
	}

	if c.ctx != nil {
		c.waitDone = make(chan struct{})
		go func() {
			select {
			case <-c.ctx.Done():
				// fmt.Println("ctx.done kill pid %s", c.Process.Pid)
				c.Process.Kill()
				// c.closeDescriptors(c.closeAfterWait)
			case <-c.waitDone:
			}
		}()
	}
      //...

will block at fn(),so there is no error send to c.errch
and the fn is thie:

func (c *Cmd) writerDescriptor(w io.Writer) (f *os.File, err error) {
	if w == nil {
		f, err = os.OpenFile(os.DevNull, os.O_WRONLY, 0)
		if err != nil {
			return
		}
		c.closeAfterStart = append(c.closeAfterStart, f)
		return
	}

	if f, ok := w.(*os.File); ok {
		return f, nil
	}

	pr, pw, err := os.Pipe()
	if err != nil {
		return
	}

	c.closeAfterStart = append(c.closeAfterStart, pw)
	c.closeAfterWait = append(c.closeAfterWait, pr)
	c.goroutine = append(c.goroutine, func() error {
                // will block at here because pr never close.
		_, err := io.Copy(w, pr)
		pr.Close() // in case io.Copy stopped due to write error
		return err
	})
	return pw, nil
}

block at io.Copy(w, pr),because the read pipe pr never close。

func (c *Cmd) Wait() error {
	if c.Process == nil {
		return errors.New("exec: not started")
	}
	if c.finished {
		return errors.New("exec: Wait was already called")
	}
	c.finished = true

	state, err := c.Process.Wait()
	if c.waitDone != nil {
		close(c.waitDone)
	}
	c.ProcessState = state

	var copyError error
	for range c.goroutine {
                 // so we can recv from c.errch, there will block
		if err := <-c.errch; err != nil && copyError == nil {
			copyError = err
		}
	}

	c.closeDescriptors(c.closeAfterWait)

	if err != nil {
		return err
	} else if !state.Success() {
		return &ExitError{ProcessState: state}
	}

	return copyError
}

will block at err := <-c.errch,because in start method,we blocked in fn,and don't send error to c.errch.so the program don't exit after timeout.

// os/exec/exec.go
func (c *Cmd) Start() error {
         // ...
	if c.ctx != nil {
		c.waitDone = make(chan struct{})
		go func() {
			select {
			case <-c.ctx.Done():
				c.Process.Kill()
				c.closeDescriptors(c.closeAfterWait)
			case <-c.waitDone:
			}
		}()
	}
      //...

maybe we can close read pipe c.closeDescriptors(c.closeAfterWait) in case <-c.ctx.Done() .
I tried this, the bash process will be killed after timeout, and the program will exit, the sleep process hosting by the process which pid=1.

@lixd lixd changed the title affected/package: os/exec, timeout notwork. affected/package: os/exec, context not work. Jul 6, 2022
@seankhliao
Copy link
Member

Duplicate of #23019

@seankhliao seankhliao marked this as a duplicate of #23019 Jul 6, 2022
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 6, 2022
@golang golang locked and limited conversation to collaborators Jul 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants