Blame SOURCES/Always-buffer-TCP-data-in-__handle_recv.patch

52ae74
From c1be487bb00f2e813212031d93fcebbfbd0da60b Mon Sep 17 00:00:00 2001
52ae74
From: Robbie Harwood <rharwood@redhat.com>
52ae74
Date: Thu, 29 Aug 2019 11:13:41 -0400
52ae74
Subject: [PATCH] Always buffer TCP data in __handle_recv()
52ae74
52ae74
Refactor __handle_recv() to always create a BytesIO() object for TCP
52ae74
data.  Linearize control flow for ease of debugging.  Always apply
52ae74
length checks so that we don't have to wait for EOF in the multiple-recv
52ae74
case.
52ae74
52ae74
Fixes a bug where we wouldn't return any data because we never received
52ae74
the EOF, or didn't receive it fast enough.
52ae74
52ae74
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
52ae74
(cherry picked from commit 7e2b1ab27b843c220fe301b74bab01ed61b0f59a)
52ae74
---
52ae74
 kdcproxy/__init__.py | 54 +++++++++++++++++++++++++-------------------
52ae74
 1 file changed, 31 insertions(+), 23 deletions(-)
52ae74
52ae74
diff --git a/kdcproxy/__init__.py b/kdcproxy/__init__.py
52ae74
index 6526bc9..9bc7044 100644
52ae74
--- a/kdcproxy/__init__.py
52ae74
+++ b/kdcproxy/__init__.py
52ae74
@@ -128,29 +128,37 @@ class Application:
52ae74
             # length prefix. So add it.
52ae74
             reply = struct.pack("!I", len(reply)) + reply
52ae74
             return reply
52ae74
-        else:
52ae74
-            # TCP is a different story. The reply must be buffered
52ae74
-            # until the full answer is accumulated.
52ae74
-            buf = read_buffers.get(sock)
52ae74
-            part = sock.recv(1048576)
52ae74
-            if buf is None:
52ae74
-                if len(part) > 4:
52ae74
-                    # got enough data in the initial package. Now check
52ae74
-                    # if we got the full package in the first run.
52ae74
-                    (length, ) = struct.unpack("!I", part[0:4])
52ae74
-                    if length + 4 == len(part):
52ae74
-                        return part
52ae74
-                read_buffers[sock] = buf = io.BytesIO()
52ae74
-
52ae74
-            if part:
52ae74
-                # data received, accumulate it in a buffer
52ae74
-                buf.write(part)
52ae74
-                return None
52ae74
-            else:
52ae74
-                # EOF received
52ae74
-                read_buffers.pop(sock)
52ae74
-                reply = buf.getvalue()
52ae74
-                return reply
52ae74
+
52ae74
+        # TCP is a different story. The reply must be buffered until the full
52ae74
+        # answer is accumulated.
52ae74
+        buf = read_buffers.get(sock)
52ae74
+        if buf is None:
52ae74
+            read_buffers[sock] = buf = io.BytesIO()
52ae74
+
52ae74
+        part = sock.recv(1048576)
52ae74
+        if not part:
52ae74
+            # EOF received.  Return any incomplete data we have on the theory
52ae74
+            # that a decode error is more apparent than silent failure.  The
52ae74
+            # client will fail faster, at least.
52ae74
+            read_buffers.pop(sock)
52ae74
+            reply = buf.getvalue()
52ae74
+            return reply
52ae74
+
52ae74
+        # Data received, accumulate it in a buffer.
52ae74
+        buf.write(part)
52ae74
+
52ae74
+        reply = buf.getvalue()
52ae74
+        if len(reply) < 4:
52ae74
+            # We don't have the length yet.
52ae74
+            return None
52ae74
+
52ae74
+        # Got enough data to check if we have the full package.
52ae74
+        (length, ) = struct.unpack("!I", reply[0:4])
52ae74
+        if length + 4 == len(reply):
52ae74
+            read_buffers.pop(sock)
52ae74
+            return reply
52ae74
+
52ae74
+        return None
52ae74
 
52ae74
     def __filter_addr(self, addr):
52ae74
         if addr[0] not in (socket.AF_INET, socket.AF_INET6):