Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL Handler Protocol Socket in_waiting Implementation Is Incorrect #759

Open
cbreins opened this issue Jun 12, 2024 · 0 comments · May be fixed by #790
Open

URL Handler Protocol Socket in_waiting Implementation Is Incorrect #759

cbreins opened this issue Jun 12, 2024 · 0 comments · May be fixed by #790

Comments

@cbreins
Copy link

cbreins commented Jun 12, 2024

The current implementation is shown below from url_handler.protocol_socket.Serial. The select call returns whether or not data is ready as shown by the ready loop in the read function and not the number of bytes in waiting. This is particularly a problem when using the ReaderThread class as it causes the run loop to "busy" loop and consume excessive amounts of CPU.

@property
def in_waiting(self):
    """Return the number of bytes currently in the input buffer."""
    if not self.is_open:
        raise PortNotOpenError()
    # Poll the socket to see if it is ready for reading.
    # If ready, at least one byte will be to read.
    lr, lw, lx = select.select([self._socket], [], [], 0)
    return len(lr)

while len(read) < size:
    try:
        ready, _, _ = select.select([self._socket], [], [], timeout.time_left())

I have included a possible fix below. I have tested the implementation on Unix (Linux Ubuntu 20.04), but I don't have access to a Windows machine at the moment to verify the windows implementation.

  @property
  def in_waiting(self):
    """Return the number of bytes currently in the input buffer."""
    if not self.is_open:
        raise PortNotOpenError()
        
    if os.name == 'nt':  # Windows
        return len(sock.recv(4096, socket.MSG_PEEK))
        
    else:  # POSIX (Unix-like)
        import fcntl
        import struct
        import termios
        
        buf = fcntl.ioctl(self._socket, termios.FIONREAD, struct.pack("I", 0))
        nbytes = struct.unpack("I", buf)[0]
        return nbytes
@cbreins cbreins changed the title URL Handler Protocol Socket in_waiting is incorrect URL Handler Protocol Socket in_waiting Implementation Is Incorrect Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant