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

huangapple go评论86阅读模式
英文:

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

问题

我正在使用os.File.SetReadDeadlineos.File.ReadFull的组合。但是,即使使用了SetReadDeadline,我设置的截止时间完全被忽略,ReadFull仍然会一直阻塞。为什么会这样呢?

附加信息:我向文件发送了一些IOCTLS,并且需要使用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.

答案1

得分: 3

TL;DR:

在使用os.File.Fd()之后,对文件使用syscall.SetNonblock(fd.Fd(), true)


这是由于在golang UNIX中read的实现方式造成的1

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
    }
}

如果文件设置为阻塞模式,第一个n, err := ignoringEINTRIO(syscall.Read, fd.Sysfd, p)将永远阻塞。只有在文件以非阻塞模式打开时,才会执行waitRead。但是我确实以非阻塞模式打开了文件,发生了什么?

os.File.Fd()的实现方式2破坏了它:

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()总是将文件设置为阻塞模式。因此,在期望进行轮询读取之前,我们必须撤消该设置。因此:

在使用os.File.Fd()之后,对文件使用syscall.SetNonblock(fd.Fd(), true)

英文:

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()

huangapple
  • 本文由 发表于 2022年12月22日 17:22:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/74886459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定