Go bytes.Buffer
isn't thread-safe 。然而,当我阅读源代码时,我注意到 os/exec.CombinedOutput()
c.Stdout
和 c.Stderr
使用相同的缓冲区。进一步阅读包的实现,看起来在写入 c.Stderr
/c.Stdout
here 时没有同步。 .
我是否遗漏了什么或者我是否发现了可能的同步问题? AFAIK stderr 和 stdout 可以由子进程同时写入。
最佳答案
如果 Stderr 和 Stdout 是 Cmd documentation 中所述的同一作者,则它们不会同时写入。 :
If Stdout and Stderr are the same writer, at most one goroutine at a time will call Write.
此功能在 Cmd.stderr 中实现功能。如果Stdout和Stderr相同,则将相同的fd传递给子进程stdout和stderr。如果 fd 是一个带有 goroutine 来泵送它的管道,则只有一个 goroutine 写入 Stdout/Stderr。
关于go - 为什么 os/exec.CombinedOutput() 没有竞争条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43138596/