Named pipes blocking with O_NONBLOCK on Mac

184 views
Skip to first unread message

Jay Goel

unread,
Mar 11, 2024, 10:04:19 AM3/11/24
to golang-nuts
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

Kwaku Biney

unread,
Mar 11, 2024, 10:19:39 AM3/11/24
to golang-nuts
I think you put this issue on the Go issue board? https://github.com/golang/go/issues/66239
Seems like the bug has been confirmed or at least it's being looked at

Reply all
Reply to author
Forward
0 new messages