Skip to content

recv hangs if there’s no timeout and connection closes #135

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

Closed
JetForMe opened this issue Jan 5, 2024 · 1 comment · Fixed by #151
Closed

recv hangs if there’s no timeout and connection closes #135

JetForMe opened this issue Jan 5, 2024 · 1 comment · Fixed by #151

Comments

@JetForMe
Copy link

JetForMe commented Jan 5, 2024

recv() is supposed to return a 0-length bytes object when the connection closes. But the check for bytes, although it tests the timeout, doesn’t test the connection status, and thus hangs forever if the connection closes.

@JetForMe
Copy link
Author

JetForMe commented Jan 5, 2024

This is a potential fix:

        while not self._available():
            if self._status in (wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED, wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSE_WAIT):
                break
    @_check_socket_closed
    def recv(
        # pylint: disable=too-many-branches
        self,
        bufsize: int,
        flags: int = 0,
    ) -> bytes:
        """
        Receive data from the socket. The return value is a bytes object representing the data
        received. The maximum amount of data to be received at once is specified by bufsize.

        :param int bufsize: Maximum number of bytes to receive.
        :param int flags: ignored, present for compatibility.

        :return bytes: Data from the socket.
        """
        stamp = time.monotonic()
        while not self._available():
            if self._status in (wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED, wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSE_WAIT):
                break
            if self._timeout and 0 < self._timeout < time.monotonic() - stamp:
                break
            time.sleep(0.05)
        bytes_on_socket = self._available()
        if not bytes_on_socket:
            return b""
        bytes_to_read = min(bytes_on_socket, bufsize)
        if self._sock_type == SOCK_STREAM:
            bytes_read = _the_interface.socket_read(self._socknum, bytes_to_read)[1]
        else:
            bytes_read = _the_interface.read_udp(self._socknum, bytes_to_read)[1]
        gc.collect()
        return bytes(bytes_read)

aggieNick02 added a commit to PCPartPicker/Adafruit_CircuitPython_Wiznet5k that referenced this issue Oct 10, 2024
failures.

The issue is caused by the code checking socket status *after* checking
if bytes are available. Bytes may have become available between the
last byte available check and the socket status check. This causes us to
incorrectly ignore the newly available bytes and return 0 to the caller,
which in any blocking/timeout scenario tells the caller no more bytes
will ever be available.

This was introduced in commit 89b9f10
See also
adafruit#151
as a fix for
adafruit#135
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