Search code examples
gounixselectpollingepoll

GOLANG: Why does SetDeadline/SetReadDeadline/SetWriteDeadline not work on a file when using os.File.Fd()?


I am using a combination of os.File.SetReadDeadline and os.File.ReadFull. But even if using SetReadDeadline, the deadline I have set is completely ignored and ReadFull blocks forever. Why is that?

Additional info: I fire some IOCTLS towards the file and therefore need os.File.Fd() to get the file descriptor.


Solution

  • TL;DR:

    Use syscall.SetNonblock(fd.Fd(), true) on the file after having used os.File.Fd()


    This is due to the implementation of read in golang UNIX:

    func (fd *FD) Read(p []byte) (int, error) {
        if err := fd.readLock(); err != nil {
            return 0, err
        }
        defer fd.readUnlock()
        if len(p) == 0 {
            // If the caller wanted a zero byte read, return immediately
            // without trying (but after acquiring the readLock).
            // Otherwise syscall.Read returns 0, nil which looks like
            // io.EOF.
            // TODO(bradfitz): make it wait for readability? (Issue 15735)
            return 0, nil
        }
        if err := fd.pd.prepareRead(fd.isFile); err != nil {
            return 0, err
        }
        if fd.IsStream && len(p) > maxRW {
            p = p[:maxRW]
        }
        for {
            n, err := ignoringEINTRIO(syscall.Read, fd.Sysfd, p)
            if err != nil {
                n = 0
                if err == syscall.EAGAIN && fd.pd.pollable() {
                    if err = fd.pd.waitRead(fd.isFile); err == nil {
                        continue
                    }
                }
            }
            err = fd.eofError(n, err)
            return n, err
        }
    }
    

    The first n, err := ignoringEINTRIO(syscall.Read, fd.Sysfd, p) blocks forever if the file is set to blocking mode. waitRead is only executed if the file is opened in non-blocking mode. But I did open the file in non-blocking mode, so what happened?

    The Implementation of os.File.Fd() broke it:

    func (f *File) Fd() uintptr {
        if f == nil {
            return ^(uintptr(0))
        }
    
        // If we put the file descriptor into nonblocking mode,
        // then set it to blocking mode before we return it,
        // because historically we have always returned a descriptor
        // opened in blocking mode. The File will continue to work,
        // but any blocking operation will tie up a thread.
        if f.nonblock {
            f.pfd.SetBlocking()
        }
    
        return uintptr(f.pfd.Sysfd)
    }
    

    Fd() always sets the file to blocking. So we have to undo that before expecting polling reads. Therefore:

    Use syscall.SetNonblock(fd.Fd(), true) on the file after having used os.File.Fd()