fd_wait.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """POSIX fd-readiness helper shared by the stdout drains and the PTY bridge.
  2. ``select.select()`` raises ``ValueError: filedescriptor out of range in select()``
  3. once an fd reaches FD_SETSIZE (1024). A long-lived gateway/desktop process holds
  4. that many open fds, and every drain swallowed that ValueError as "fd already
  5. closed", silently discarding output (issue #94928).
  6. ``selectors.DefaultSelector`` (epoll on Linux, kqueue on macOS) has no FD_SETSIZE
  7. ceiling. ``poll()`` is not the default: macOS ``poll()`` does not support device
  8. fds and reports a PTY master as POLLNVAL, i.e. "ready" at once, so a PTY read loop
  9. would spin a core. It stays the fallback because, unlike epoll/kqueue, it needs no
  10. fd of its own.
  11. """
  12. import errno
  13. import os
  14. import selectors
  15. import stat
  16. import sys
  17. class FdWaiter:
  18. """Readability wait on one fd. Owns a selector (epoll/kqueue are fds themselves):
  19. create it once per reader lifetime and ``close()`` it when the reader is done."""
  20. def __init__(self, fd: int, selector_cls):
  21. self._selector = selector_cls()
  22. try:
  23. self._selector.register(fd, selectors.EVENT_READ)
  24. except BaseException:
  25. self._selector.close()
  26. raise
  27. def wait(self, timeout_s: float) -> bool:
  28. """True as soon as the fd is readable (EOF and hangup included), False when
  29. *timeout_s* elapses idle."""
  30. return bool(self._selector.select(max(timeout_s, 0)))
  31. def close(self) -> None:
  32. self._selector.close()
  33. def open_fd_waiter(fd: int):
  34. """Return an :class:`FdWaiter` for *fd*, or None only when *fd* is closed/invalid
  35. and there is nothing to drain. Raises OSError when no safe wait exists."""
  36. try:
  37. return FdWaiter(fd, selectors.DefaultSelector)
  38. except ValueError:
  39. return None
  40. except OSError as exc:
  41. if exc.errno == errno.EBADF:
  42. return None
  43. # The selector itself could not be set up (EMFILE: the process is at its fd limit)
  44. # while the fd is still live. poll() needs no extra fd; never report that as EOF.
  45. # On macOS it is no wait at all for a device (PTY): raise rather than spin.
  46. if sys.platform == "darwin" and stat.S_ISCHR(os.fstat(fd).st_mode):
  47. raise
  48. return FdWaiter(fd, selectors.PollSelector)