I'm reading go exec source code. https://cs.opensource.google/go/go/ /refs/tags/go1.17.3:src/os/exec/exec.go
When Stdinpipe is called, the reader is added to an array closeAfterStart. When Start() is called, the reader is closed. I'm not sure to understand why they close the reader just after starting the process.
CodePudding user response:
To mirror what Penélope Stevens is saying, os.Pipe maps to an underlying os File pipe. By the time the *os.File returned by os.Pipe is closed it has already been passed to the new spawned process. The close will close the file descriptor in this process, but the spawned process can still read/write from that pipe.
The file descriptor is grabbed here: https://cs.opensource.google/go/go/ /refs/tags/go1.17.3:src/os/exec/exec.go;l=404-415;drc=refs/tags/go1.17.3
And then passed to the spawned process with ProcAttr: https://pkg.go.dev/os#ProcAttr