Last active
March 10, 2024 16:15
-
-
Save poundifdef/76377b75b15826baccab83cd501d0c85 to your computer and use it in GitHub Desktop.
Named pipes with O_NONBLOCK still block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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