Skip to content

os/exec: cmd.Start got a deadlock when set cmd.Stdin #47061

@Ace-Tang

Description

@Ace-Tang

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

$ go version
go version go1.15.11 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="off"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ace/.cache/go-build"
GOENV="/home/ace/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE="gitlab.alibaba-inc.com"
GOMODCACHE="/home/ace/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ace/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/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-build205131986=/tmp/go-build -gno-record-gcc-switches"

What did you do?

There is a deadlock in cmd.Start, this is only happen when command set cmd.Stdin

the calling stack: cmd.Start -> cmd set stdin, add stdin io copy in a goroutine after os.StartProcess successfully -> begin os.StartProcess -> finally in forkExec() -> fail in readlen (donnot know why) -> deal with the fail, begin to call wait4, but child process has run successfully, child wait goroutine to copy io, it was hang, and wait4 never success

image

use the follow code to reproduce

parent

package main                                                                                                                                                                                  
                                                                                                                                                                                              
import (                                                                                                                                                                                      
        "bufio"                                                                                                                                                                               
        "bytes"                                                                                                                                                                               
        "encoding/json"                                                                                                                                                                       
        "fmt"                                                                                                                                                                                 
        "io"                                                                                                                                                                                  
        "os"                                                                                                                                                                                  
        "os/exec"                                                                                                                                                                             
        "time"                                                                                                                                                                                
)                                                                                                                                                                                             
                                                                                                                                                                                              
func forwardLog(fd io.Reader) {                                                                                                                                                               
        reader := bufio.NewReader(fd)                                                                                                                                                         
        for {                                                                                                                                                                                 
                line, err := reader.ReadBytes('\n')                                                                                                                                           
                if err == io.EOF {                                                                                                                                                            
                        fmt.Println("end of read child log")                                                                                                                                  
                        return                                                                                                                                                                
                                                                                                                                                                                              
                }                                                                                                                                                                             
                                                                                                                                                                                              
                if err != nil {                                                                                                                                                               
                        fmt.Printf("failed to read child log: %s\n", err)
                        return
                }

                fmt.Printf("child log: %s\n", line)

        }
}

func main() {
        r, w, err := os.Pipe()
        go forwardLog(r)
        data := "hello"
        raw, err := json.Marshal(data)
        if err != nil {
                panic(err)
        }

        cmd := exec.Command("/home/ace/go/src/test/cmd-start-hang/read")
        cmd.ExtraFiles = append(cmd.ExtraFiles, w)
        cmd.Stdin = bytes.NewReader(raw)

        if err := cmd.Start(); err != nil {
                panic(err)

        }

        fmt.Printf("proc %+v\n", cmd.Process)

        if err := cmd.Wait(); err != nil {
                panic(err)
        }

        time.Sleep(10 * time.Second)

}

child

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"time"
)

func getData(r io.Reader) ([]byte, error) {
	raw, err := ioutil.ReadAll(r)
	return raw, err
}

func main() {
	log := os.NewFile(uintptr(3), "")

	fmt.Fprintf(log, "child run\n")
	raw, err := getData(os.Stdin)
	if err != nil {
		fmt.Fprintf(log, "%s", err)
	}

	fmt.Fprintf(log, "read: %s\n", raw)

	time.Sleep(10 * time.Second)
	os.Exit(0)
}

Since I donnot know why readline fail, so I modify source code, create a error after readlen, make it fails

image

What did you expect to see?

cmd.Start got a error return

What did you see instead?

Both parent and child process were hang forever

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions