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

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

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

  1. func (fd *FD) Read(p []byte) (int, error) {
  2. if err := fd.readLock(); err != nil {
  3. return 0, err
  4. }
  5. defer fd.readUnlock()
  6. if len(p) == 0 {
  7. // If the caller wanted a zero byte read, return immediately
  8. // without trying (but after acquiring the readLock).
  9. // Otherwise syscall.Read returns 0, nil which looks like
  10. // io.EOF.
  11. // TODO(bradfitz): make it wait for readability? (Issue 15735)
  12. return 0, nil
  13. }
  14. if err := fd.pd.prepareRead(fd.isFile); err != nil {
  15. return 0, err
  16. }
  17. if fd.IsStream && len(p) > maxRW {
  18. p = p[:maxRW]
  19. }
  20. for {
  21. n, err := ignoringEINTRIO(syscall.Read, fd.Sysfd, p)
  22. if err != nil {
  23. n = 0
  24. if err == syscall.EAGAIN && fd.pd.pollable() {
  25. if err = fd.pd.waitRead(fd.isFile); err == nil {
  26. continue
  27. }
  28. }
  29. }
  30. err = fd.eofError(n, err)
  31. return n, err
  32. }
  33. }

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

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

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

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:

  1. func (fd *FD) Read(p []byte) (int, error) {
  2. if err := fd.readLock(); err != nil {
  3. return 0, err
  4. }
  5. defer fd.readUnlock()
  6. if len(p) == 0 {
  7. // If the caller wanted a zero byte read, return immediately
  8. // without trying (but after acquiring the readLock).
  9. // Otherwise syscall.Read returns 0, nil which looks like
  10. // io.EOF.
  11. // TODO(bradfitz): make it wait for readability? (Issue 15735)
  12. return 0, nil
  13. }
  14. if err := fd.pd.prepareRead(fd.isFile); err != nil {
  15. return 0, err
  16. }
  17. if fd.IsStream && len(p) > maxRW {
  18. p = p[:maxRW]
  19. }
  20. for {
  21. n, err := ignoringEINTRIO(syscall.Read, fd.Sysfd, p)
  22. if err != nil {
  23. n = 0
  24. if err == syscall.EAGAIN && fd.pd.pollable() {
  25. if err = fd.pd.waitRead(fd.isFile); err == nil {
  26. continue
  27. }
  28. }
  29. }
  30. err = fd.eofError(n, err)
  31. return n, err
  32. }
  33. }

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:

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

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:

确定