Skip to content

Partial recv can cause failure #16

Description

@osresearch

sock.recv(length) is not guaranteed to return all the bytes in a single call, so it is necessary to wrap it in recv_frame(). Otherwise a slow network can cause unrecoverable protocol errors.

diff --git a/uwebsockets/protocol.py b/uwebsockets/protocol.py
index 64f4d06..8ed34b8 100644
--- a/uwebsockets/protocol.py
+++ b/uwebsockets/protocol.py
@@ -113,7 +115,9 @@ class Websocket:
             mask_bits = self.sock.read(4)
 
         try:
-            data = self.sock.read(length)
+            data = b''
+            while len(data) < length:
+                data += self.sock.read(length - len(data))
         except MemoryError:
             # We can't receive this many bytes, close the socket
             if __debug__: LOGGER.debug("Frame of length %s too big. Closing",

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions