--- /dev/null
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build darwin dragonfly freebsd linux netbsd openbsd
+
+package os_test
+
+import (
+ "bufio"
+ "bytes"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "runtime"
+ "sync"
+ "syscall"
+ "testing"
+ "time"
+)
+
+// Issue 24164.
+func TestFifoEOF(t *testing.T) {
+ if runtime.GOOS == "openbsd" {
+ // On OpenBSD 6.2 this test just hangs for some reason.
+ t.Skip("skipping on OpenBSD; issue 25877")
+ }
+
+ dir, err := ioutil.TempDir("", "TestFifoEOF")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(dir)
+
+ fifoName := filepath.Join(dir, "fifo")
+ if err := syscall.Mkfifo(fifoName, 0600); err != nil {
+ t.Fatal(err)
+ }
+
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+
+ w, err := os.OpenFile(fifoName, os.O_WRONLY, 0)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ defer func() {
+ if err := w.Close(); err != nil {
+ t.Errorf("error closing writer: %v", err)
+ }
+ }()
+
+ for i := 0; i < 3; i++ {
+ time.Sleep(10 * time.Millisecond)
+ _, err := fmt.Fprintf(w, "line %d\n", i)
+ if err != nil {
+ t.Errorf("error writing to fifo: %v", err)
+ return
+ }
+ }
+ time.Sleep(10 * time.Millisecond)
+ }()
+
+ defer wg.Wait()
+
+ r, err := os.Open(fifoName)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ done := make(chan bool)
+ go func() {
+ defer close(done)
+
+ defer func() {
+ if err := r.Close(); err != nil {
+ t.Errorf("error closing reader: %v", err)
+ }
+ }()
+
+ rbuf := bufio.NewReader(r)
+ for {
+ b, err := rbuf.ReadBytes('\n')
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ t.Logf("%s\n", bytes.TrimSpace(b))
+ }
+ }()
+
+ select {
+ case <-done:
+ // Test succeeded.
+ case <-time.After(time.Second):
+ t.Error("timed out waiting for read")
+ // Close the reader to force the read to complete.
+ r.Close()
+ }
+}
stdoutOrErr: fdi == 1 || fdi == 2,
}}
+ pollable := kind == kindOpenFile || kind == kindPipe || kind == kindNonBlock
+
// Don't try to use kqueue with regular files on FreeBSD.
// It crashes the system unpredictably while running all.bash.
// Issue 19093.
// we assume they know what they are doing so we allow it to be
// used with kqueue.
if runtime.GOOS == "freebsd" && kind == kindOpenFile {
- kind = kindNewFile
+ pollable = false
+ }
+
+ // On Darwin, kqueue does not work properly with fifos:
+ // closing the last writer does not cause a kqueue event
+ // for any readers. See issue #24164.
+ if runtime.GOOS == "darwin" && kind == kindOpenFile {
+ var st syscall.Stat_t
+ if err := syscall.Fstat(fdi, &st); err == nil && st.Mode&syscall.S_IFMT == syscall.S_IFIFO {
+ pollable = false
+ }
}
- pollable := kind == kindOpenFile || kind == kindPipe || kind == kindNonBlock
if err := f.pfd.Init("file", pollable); err != nil {
// An error here indicates a failure to register
// with the netpoll system. That can happen for
package os_test
import (
+ "bufio"
+ "bytes"
"fmt"
"internal/testenv"
"io"
wg.Wait()
}
+
+// Issue 24164, for pipes.
+func TestPipeEOF(t *testing.T) {
+ r, w, err := os.Pipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+
+ defer func() {
+ if err := w.Close(); err != nil {
+ t.Errorf("error closing writer: %v", err)
+ }
+ }()
+
+ for i := 0; i < 3; i++ {
+ time.Sleep(10 * time.Millisecond)
+ _, err := fmt.Fprintf(w, "line %d\n", i)
+ if err != nil {
+ t.Errorf("error writing to fifo: %v", err)
+ return
+ }
+ }
+ time.Sleep(10 * time.Millisecond)
+ }()
+
+ defer wg.Wait()
+
+ done := make(chan bool)
+ go func() {
+ defer close(done)
+
+ defer func() {
+ if err := r.Close(); err != nil {
+ t.Errorf("error closing reader: %v", err)
+ }
+ }()
+
+ rbuf := bufio.NewReader(r)
+ for {
+ b, err := rbuf.ReadBytes('\n')
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ t.Logf("%s\n", bytes.TrimSpace(b))
+ }
+ }()
+
+ select {
+ case <-done:
+ // Test succeeded.
+ case <-time.After(time.Second):
+ t.Error("timed out waiting for read")
+ // Close the reader to force the read to complete.
+ r.Close()
+ }
+}