Skip to content

Commit

Permalink
Allow for null messages; should fix #918
Browse files Browse the repository at this point in the history
  • Loading branch information
technige committed Oct 7, 2021
1 parent a7d0c65 commit 166a131
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions py2neo/client/bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,22 @@ def read_message(self):
raise_from(ConnectionBroken("Failed to read message"), error)
else:
if hi == lo == 0:
break
size = hi << 8 | lo
chunks.append(self.wire.read(size))
# We hit a zero chunk...
if chunks:
# ... and some non-zero chunks have been collected,
# therefore we can exit the loop with a message.
break
else:
# ... and no non-zero chunks have yet been collected,
# therefore, this must be a null message used for
# keep-alive and we should carry on looping.
pass
else:
# We found a non-zero chunk which can be collected.
size = hi << 8 | lo
chunks.append(self.wire.read(size))
message = b"".join(chunks)
assert message # the message should never be empty
_, n = divmod(message[0], 0x10)
try:
fields = list(unpack(message, offset=2))
Expand All @@ -179,10 +191,22 @@ def read_message_py2(self):
raise_from(ConnectionBroken("Failed to read message"), error)
else:
if hi == lo == 0:
break
size = hi << 8 | lo
chunks.append(self.wire.read(size))
# We hit a zero chunk...
if chunks:
# ... and some non-zero chunks have been collected,
# therefore we can exit the loop with a message.
break
else:
# ... and no non-zero chunks have yet been collected,
# therefore, this must be a null message used for
# keep-alive and we should carry on looping.
pass
else:
# We found a non-zero chunk which can be collected.
size = hi << 8 | lo
chunks.append(self.wire.read(size))
message = bytearray(b"".join(map(bytes, chunks)))
assert message # the message should never be empty
_, n = divmod(message[0], 0x10)
try:
fields = list(unpack(message, offset=2))
Expand Down

0 comments on commit 166a131

Please sign in to comment.