Skip to content

os/exec: clarify requirements for stdin/stdout #6008

@dvyukov

Description

@dvyukov
Here is code from golang-nuts:
        cmd := exec.Command("cat")
        in, _ := cmd.StdinPipe()
        out, _ := cmd.StdoutPipe()
        cmd.Start()
        go func() { defer os.Stdout.Close(); io.Copy(os.Stdout, out) }()
        go func() { defer in.Close(); io.Copy(in, os.Stdin) }()
        cmd.Wait()
https://groups.google.com/forum/#!topic/golang-nuts/gjkbC6M4cAU

This has 2 data races:
1. Both cmd and user close 'in'.
2. Cmd closes 'out', while user reads from it.

I've seen similar races in several projects.

os/exec documentation must clearly explain:
1. When it is allowed to close stdin/stdout (only after Wait?)
2. How to stream stdout (it's easy to race with stdout.Close in Wait)
3. How to close stdin to notify the process about end of input data (e.g. cat)
4. How/when to use thread-safe Reader/Writer

It all is basically impossible to get right first time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions