Hello,
I believe there is a bug where, on Mac, reading a named pipe with O_NONBLOCK is blocking on Read(). I am not able to reproduce this on Linux.
This example is on go version go1.22.1 darwin/arm64, and I am running an M1 2020 macbook with OS version 12.5 (21G72).
I believe there is some race condition involved, as I'm only able to reproduce this when invoking time.Sleep() between writes. Here is my writer:
go func() {
pipe, _ := os.OpenFile("p.pipe", os.O_WRONLY|os.O_APPEND, os.ModeNamedPipe)
for range 5 {
pipe.WriteString("Hello\n")
time.Sleep(1000 * time.Millisecond)
}
err := pipe.Close()
}()
And here is my reader:
pipe, _ := os.OpenFile("p.pipe", os.O_RDONLY|syscall.O_NONBLOCK, os.ModeNamedPipe)
buf := make([]byte, 1)
for {
n, err := pipe.Read(buf)
fmt.Println(n, err)
}
After pipe.Close() finishes, then pipe.Read() blocks indefinitely. I would expect the for loop to run infinitely with an EOF error.
Curiously, when I remove the time.Sleep(), then the code behaves as expected.
The actual problem I'm solving is to pipe the output of one process, which writes to a named pipe, to an HTTP writer. But this is the simplest example I can find to reproduce the issue.
Thank you,
Jay Goel