return uintptr(file.pfd.Sysfd)
}
+// newFileKind describes the kind of file to newFile.
+type newFileKind int
+
+const (
+ // kindNewFile means that the descriptor was passed to us via NewFile.
+ kindNewFile newFileKind = iota
+ // kindOpenFile means that the descriptor was opened using
+ // Open, Create, or OpenFile.
+ kindOpenFile
+ // kindPipe means that the descriptor was opened using Pipe.
+ kindPipe
+ // kindSock means that the descriptor is a network file descriptor
+ // that was created from net package and was opened using net_newUnixFile.
+ kindSock
+ // kindConsole means that the descriptor is a console handle.
+ kindConsole
+)
+
// newFile returns a new File with the given file handle and name.
// Unlike NewFile, it does not check that h is syscall.InvalidHandle.
// If nonBlocking is true, it tries to add the file to the runtime poller.
-func newFile(h syscall.Handle, name string, kind string, nonBlocking bool) *File {
- if kind == "file" {
+func newFile(h syscall.Handle, name string, kind newFileKind, nonBlocking bool) *File {
+ typ := "file"
+ switch kind {
+ case kindNewFile, kindOpenFile:
t, err := syscall.GetFileType(h)
if err != nil || t == syscall.FILE_TYPE_CHAR {
var m uint32
if syscall.GetConsoleMode(h, &m) == nil {
- kind = "console"
+ typ = "console"
+ // Console handles are always blocking.
+ break
}
} else if t == syscall.FILE_TYPE_PIPE {
- kind = "pipe"
+ typ = "pipe"
}
+ // NewFile doesn't know if a handle is blocking or non-blocking,
+ // so we try to detect that here. This call may block/ if the handle
+ // is blocking and there is an outstanding I/O operation.
+ //
+ // Avoid doing this for Stdin, which is almost always blocking and might
+ // be in use by other process when the "os" package is initializing.
+ // See go.dev/issue/75949 and go.dev/issue/76391.
+ if kind == kindNewFile && h != syscall.Stdin {
+ nonBlocking, _ = windows.IsNonblock(h)
+ }
+ case kindPipe:
+ typ = "pipe"
+ case kindSock:
+ typ = "file+net"
+ case kindConsole:
+ typ = "console"
+ default:
+ panic("newFile with unknown kind")
}
f := &File{&file{
// Ignore initialization errors.
// Assume any problems will show up in later I/O.
- f.pfd.Init(kind, nonBlocking)
+ f.pfd.Init(typ, nonBlocking)
return f
}
// newConsoleFile creates new File that will be used as console.
func newConsoleFile(h syscall.Handle, name string) *File {
- return newFile(h, name, "console", false)
+ return newFile(h, name, kindConsole, false)
}
// newFileFromNewFile is called by [NewFile].
if h == syscall.InvalidHandle {
return nil
}
- nonBlocking, _ := windows.IsNonblock(syscall.Handle(fd))
- return newFile(h, name, "file", nonBlocking)
+ return newFile(h, name, kindNewFile, false)
}
// net_newWindowsFile is a hidden entry point called by net.conn.File.
if h == syscall.InvalidHandle {
panic("invalid FD")
}
- return newFile(h, name, "file+net", true)
+ return newFile(h, name, kindSock, true)
}
func epipecheck(file *File, e error) {
return nil, &PathError{Op: "open", Path: name, Err: err}
}
nonblocking := flag&windows.O_FILE_FLAG_OVERLAPPED != 0
- return newFile(r, name, "file", nonblocking), nil
+ return newFile(r, name, kindOpenFile, nonblocking), nil
}
func openDirNolog(name string) (*File, error) {
return nil, nil, NewSyscallError("pipe", e)
}
// syscall.Pipe always returns a non-blocking handle.
- return newFile(p[0], "|0", "pipe", false), newFile(p[1], "|1", "pipe", false), nil
+ return newFile(p[0], "|0", kindPipe, false), newFile(p[1], "|1", kindPipe, false), nil
}
var useGetTempPath2 = sync.OnceValue(func() bool {
}
f.Close()
}
+
+func TestNewFileStdinBlocked(t *testing.T) {
+ // See https://go.dev/issue/75949.
+ t.Parallel()
+
+ // Use a subprocess to test that os.NewFile on a blocked stdin works.
+ // Can't do it in the same process because os.NewFile would close
+ // stdin for the whole test process once the test ends.
+ if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
+ // In the child process, just exit.
+ // If we get here, the os package successfully initialized.
+ os.Exit(0)
+ }
+ name := pipeName()
+ stdin := newBytePipe(t, name, false)
+ file := newFileOverlapped(t, name, false)
+
+ var wg sync.WaitGroup
+ wg.Go(func() {
+ // Block stdin on a read.
+ if _, err := stdin.Read(make([]byte, 1)); err != nil {
+ t.Error(err)
+ }
+ })
+
+ time.Sleep(100 * time.Millisecond) // Give time for the read to start.
+ cmd := testenv.CommandContext(t, t.Context(), testenv.Executable(t), fmt.Sprintf("-test.run=^%s$", t.Name()))
+ cmd.Env = cmd.Environ()
+ cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
+ cmd.Stdin = stdin
+ if err := cmd.Run(); err != nil {
+ t.Fatal(err)
+ }
+ // Unblock the read to let the goroutine exit.
+ if _, err := file.Write(make([]byte, 1)); err != nil {
+ t.Fatal(err)
+ }
+ wg.Wait() // Don't leave goroutines behind.
+}