pyln: Fix an issue with the LightningConnection short-reading

We may end up with a short read, that is not empty, when a message
gets fragmented during transport, so we need to loop until we either
reach the full size or we have an empty read indicating a dropped
connection.

Changelog-None
This commit is contained in:
Christian Decker 2022-11-30 16:09:47 +01:00
parent 280b49a677
commit d7cd3e1cb5

View file

@ -236,13 +236,21 @@ class LightningConnection(object):
length, = struct.unpack("!H", length) length, = struct.unpack("!H", length)
self.rn += 1 self.rn += 1
mc = self.connection.recv(length + 16) # Large messages may be split into multiple packets:
if len(mc) < length + 16: mc = b''
raise ValueError( toread = length + 16
"Short read reading the message: {} != {}".format( while len(mc) < length + 16:
length + 16, len(lc) d = self.connection.recv(toread)
if len(d) == 0:
# Not making progress anymore
raise ValueError(
"Short read reading the message: {} != {}".format(
length + 16, len(mc)
)
) )
) mc += d
toread -= len(d)
m = decryptWithAD(self.rk, self.nonce(self.rn), b'', mc) m = decryptWithAD(self.rk, self.nonce(self.rn), b'', mc)
self.rn += 1 self.rn += 1
assert(self.rn % 2 == 0) assert(self.rn % 2 == 0)