Go bytes.Buffer
isn't thread-safe。然而,当我阅读源代码时,我注意到os/exec.CombinedOutput()
对c.Stdout
和c.Stderr
使用相同的缓冲区。进一步阅读包的实现,在写入c.Stderr
/ c.Stdout
here时看起来没有同步。
我是否遗漏了某些内容或者是否发现了可能的同步问题?子进程可以同时写入AFAIK stderr和stdout。
答案 0 :(得分:2)
如果Stdout和Stderr是同一个作家,一次最多只有一个goroutine会调用Write。
此功能在Cmd.stderr功能中实现。如果Stdout和Stderr相同,那么相同的fd将传递子进程stdout和stderr。如果fd是带有goroutine泵的管道,那么只有一个goroutine写给Stdout / Stderr。