Skip to content

Instantly share code, notes, and snippets.

@poundifdef
Last active March 10, 2024 16:15
Show Gist options
  • Save poundifdef/76377b75b15826baccab83cd501d0c85 to your computer and use it in GitHub Desktop.
Save poundifdef/76377b75b15826baccab83cd501d0c85 to your computer and use it in GitHub Desktop.
Named pipes with O_NONBLOCK still block
package main
// go version go1.22.1 darwin/arm64
// M1 2020 macbook with OS version 12.5 (21G72)
import (
"fmt"
"os"
"syscall"
"time"
)
func main() {
syscall.Mkfifo("p.pipe", 0666)
go func() {
pipe, _ := os.OpenFile("p.pipe", os.O_WRONLY|os.O_APPEND, os.ModeNamedPipe)
for range 5 {
pipe.WriteString("Hello\n")
// Removing this Sleep() DOES NOT cause this bug
time.Sleep(1000 * time.Millisecond)
}
err := pipe.Close()
fmt.Println("Writer closed pipe", err)
}()
pipe, err := os.OpenFile("p.pipe", os.O_RDONLY|syscall.O_NONBLOCK, os.ModeNamedPipe)
fmt.Println("Opening pipe", err)
buf := make([]byte, 1)
for {
// This should loop infinitely, even after the writer closes the pipe
fmt.Println("About to read...")
// After the pipe is closed, this call blocks BUT IT SHOULD NOT.
n, err := pipe.Read(buf)
fmt.Println("Read from pipe", n, err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment