do not assume that read will consume the number of bytes requested

The issue is that ``os.read`` does not always read the expected number of
bytes, and thus we are moving to the next frame too early causing drift
in the byte stream. When the reading drifts, it starts reading garbage
as the next frame size. The some examples of frame sizes were
4032897957 bytes, etc. Values this large were causing the exceptions
from ``os.read``.

fixes #1211

Signed-off-by: Michael Merickel <michael@merickel.org>
This commit is contained in:
Michael Merickel 2016-10-11 02:38:20 -05:00
parent f36c28926c
commit dbd704e68d
1 changed files with 7 additions and 3 deletions

View File

@ -69,7 +69,11 @@ def frames_iter(socket):
"""
Returns a generator of frames read from socket
"""
n = next_frame_size(socket)
while n > 0:
yield read(socket, n)
while True:
n = next_frame_size(socket)
if n == 0:
break
while n > 0:
result = read(socket, n)
n -= len(result)
yield result