经过多次迭代后,在Goroutine中执行多个命令会失败吗? [重复]

I have some long running goroutines that exec shell commands. This fails after an arbitrary number of commands have been run (288 times on my Linux box for this example, and 400 for my Mac). If I run the exact same code outside a goroutine, it continues as expected without stopping or errors. I've tried debugging by setting breakpoints, but to no avail. Any help would be greatly appreciated.

Does not work:

func main() {
    go doAlot()
    for {
    }
}

func doAlot() {
    i := 0
    for {
        time.Sleep(time.Millisecond * 100)
        cmd := exec.Command("echo", "hello")
        var stdout, stderr bytes.Buffer
        cmd.Stdout = &stdout
        cmd.Stderr = &stderr
        err := cmd.Run()
        if err != nil {
            log.Fatalf("cmd.Run() failed with %s
", err)
        }
        outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
        fmt.Printf("%d 
out: %serr: %s
", i, outStr, errStr)
        i++
    }
}

Works:

func main() {
    i := 0
    for {
        time.Sleep(time.Millisecond * 100)
        cmd := exec.Command("echo", "hello")
        var stdout, stderr bytes.Buffer
        cmd.Stdout = &stdout
        cmd.Stderr = &stderr
        err := cmd.Run()
        if err != nil {
            log.Fatalf("cmd.Run() failed with %s
", err)
        }
        outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
        fmt.Printf("%d
out: %serr: %s
", i, outStr, errStr)
        i++
    }
}

My only (uneducated/wild) guess, is that it relates to garbage collection and buffers, but somehow connected specifically to running inside a goroutine.

</div>