Freeze on startup due to invalid handshake?
NavyaZaveri opened this issue · comments
As documented, the client sometimes freezes upon startup; I tried to debug this and managed to root out the cause.
In a nutshell, we're running into an infinite loop trying to parse the handshake response:
async def _handshake(self):
"""
Send the initial handshake to the remote peer and wait for the peer
to respond with its handshake.
"""
self.writer.write(Handshake(self.info_hash, self.peer_id).encode())
await self.writer.drain()
buf = b''
while len(buf) < Handshake.length:
buf = await self.reader.read(PeerStreamIterator.CHUNK_SIZE)
When debugging, I noticed that the len(buf)
stays at 0 at all times during the occasional freeze, thus creating the infinite loop. I'm not entirely surely if it's a problem with our parsing methods, or if the handshake from that one peer was indeed invalid.
That said, I think we can easily mitigate this issue by hardcoding a retry limit. I've attempted a fix in #2 in conjunction with main feature in that PR, and things seemed to work fine (i.e, we can download a torrent without freezing).