diff options
author | Ian Lance Taylor <iant@golang.org> | 2023-05-15 21:50:51 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-05-18 19:37:57 +0000 |
commit | 9270e3be8fe709983cac9536eb34adf1cd17c4a8 (patch) | |
tree | 75f606c244c747c1183ed7bd83095843df3a1b3f | |
parent | 600636e931f3ae4b31f6a94fc56392b80e884f0a (diff) | |
download | go-9270e3be8fe709983cac9536eb34adf1cd17c4a8.tar.gz go-9270e3be8fe709983cac9536eb34adf1cd17c4a8.zip |
[release-branch.go1.20] os: if descriptor is non-blocking, retain that in Fd method
For #58408
For #60211
Fixes #60217
Change-Id: I30f5678b46e15121865b19d1c0f82698493fad4e
Reviewed-on: https://go-review.googlesource.com/c/go/+/495079
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
(cherry picked from commit f777726ff073f8066c017649b572bd8c40940a42)
Reviewed-on: https://go-review.googlesource.com/c/go/+/496015
Reviewed-by: Heschi Kreinick <heschi@google.com>
Auto-Submit: Heschi Kreinick <heschi@google.com>
-rw-r--r-- | src/internal/syscall/unix/nonblocking.go | 4 | ||||
-rw-r--r-- | src/internal/syscall/unix/nonblocking_js.go | 4 | ||||
-rw-r--r-- | src/internal/syscall/unix/nonblocking_libc.go | 4 | ||||
-rw-r--r-- | src/os/fifo_test.go | 50 | ||||
-rw-r--r-- | src/os/file_unix.go | 11 |
5 files changed, 71 insertions, 2 deletions
diff --git a/src/internal/syscall/unix/nonblocking.go b/src/internal/syscall/unix/nonblocking.go index a0becd1e01..6c6f0674d6 100644 --- a/src/internal/syscall/unix/nonblocking.go +++ b/src/internal/syscall/unix/nonblocking.go @@ -19,3 +19,7 @@ func IsNonblock(fd int) (nonblocking bool, err error) { } return flag&syscall.O_NONBLOCK != 0, nil } + +func HasNonblockFlag(flag int) bool { + return flag&syscall.O_NONBLOCK != 0 +} diff --git a/src/internal/syscall/unix/nonblocking_js.go b/src/internal/syscall/unix/nonblocking_js.go index 8ed40f3f91..cfe78c58d8 100644 --- a/src/internal/syscall/unix/nonblocking_js.go +++ b/src/internal/syscall/unix/nonblocking_js.go @@ -9,3 +9,7 @@ package unix func IsNonblock(fd int) (nonblocking bool, err error) { return false, nil } + +func HasNonblockFlag(flag int) bool { + return false +} diff --git a/src/internal/syscall/unix/nonblocking_libc.go b/src/internal/syscall/unix/nonblocking_libc.go index bff6684962..1310dbf8ce 100644 --- a/src/internal/syscall/unix/nonblocking_libc.go +++ b/src/internal/syscall/unix/nonblocking_libc.go @@ -19,6 +19,10 @@ func IsNonblock(fd int) (nonblocking bool, err error) { return flag&syscall.O_NONBLOCK != 0, nil } +func HasNonblockFlag(flag int) bool { + return flag&syscall.O_NONBLOCK != 0 +} + // Implemented in the syscall package. // //go:linkname fcntl syscall.fcntl diff --git a/src/os/fifo_test.go b/src/os/fifo_test.go index de70927961..ca68e82f05 100644 --- a/src/os/fifo_test.go +++ b/src/os/fifo_test.go @@ -10,6 +10,7 @@ import ( "bufio" "bytes" "fmt" + "internal/syscall/unix" "io" "os" "path/filepath" @@ -101,3 +102,52 @@ func TestFifoEOF(t *testing.T) { r.Close() } } + +// Issue 60211. +func TestOpenFileNonBlocking(t *testing.T) { + exe, err := os.Executable() + if err != nil { + t.Skipf("can't find executable: %v", err) + } + f, err := os.OpenFile(exe, os.O_RDONLY|syscall.O_NONBLOCK, 0666) + if err != nil { + t.Fatal(err) + } + defer f.Close() + nonblock, err := unix.IsNonblock(int(f.Fd())) + if err != nil { + t.Fatal(err) + } + if !nonblock { + t.Errorf("file opened with O_NONBLOCK but in blocking mode") + } +} + +func TestNewFileNonBlocking(t *testing.T) { + var p [2]int + if err := syscall.Pipe(p[:]); err != nil { + t.Fatal(err) + } + if err := syscall.SetNonblock(p[0], true); err != nil { + t.Fatal(err) + } + f := os.NewFile(uintptr(p[0]), "pipe") + nonblock, err := unix.IsNonblock(p[0]) + if err != nil { + t.Fatal(err) + } + if !nonblock { + t.Error("pipe blocking after NewFile") + } + fd := f.Fd() + if fd != uintptr(p[0]) { + t.Errorf("Fd returned %d, want %d", fd, p[0]) + } + nonblock, err = unix.IsNonblock(p[0]) + if err != nil { + t.Fatal(err) + } + if !nonblock { + t.Error("pipe blocking after Fd") + } +} diff --git a/src/os/file_unix.go b/src/os/file_unix.go index 1833c26531..8f531d9a10 100644 --- a/src/os/file_unix.go +++ b/src/os/file_unix.go @@ -171,7 +171,9 @@ func newFile(fd uintptr, name string, kind newFileKind) *File { clearNonBlock := false if pollable { if kind == kindNonBlock { - f.nonblock = true + // The descriptor is already in non-blocking mode. + // We only set f.nonblock if we put the file into + // non-blocking mode. } else if err := syscall.SetNonblock(fdi, true); err == nil { f.nonblock = true clearNonBlock = true @@ -247,7 +249,12 @@ func openFileNolog(name string, flag int, perm FileMode) (*File, error) { syscall.CloseOnExec(r) } - return newFile(uintptr(r), name, kindOpenFile), nil + kind := kindOpenFile + if unix.HasNonblockFlag(flag) { + kind = kindNonBlock + } + + return newFile(uintptr(r), name, kind), nil } func (file *file) close() error { |