if chunk == '': did not detect socket closure correctly, because chunk is a bytestring and b'' == '' is False. On socket closure by the server, recv_all() would get stuck in a busy loop and consume memory without limit. Check if not chunk: instead.
In addition, raise an exception instead of returning partial data. Partial data is not useful to the caller, in the case of unexpected remote socket closure, better to raise an exception immediately than wait for calling code to raise exceptions about not being able to parse the partial data.
if chunk == '':
did not detect socket closure correctly, because chunk is a bytestring andb'' == ''
is False. On socket closure by the server,recv_all()
would get stuck in a busy loop and consume memory without limit. Checkif not chunk:
instead.In addition, raise an exception instead of returning partial data. Partial data is not useful to the caller, in the case of unexpected remote socket closure, better to raise an exception immediately than wait for calling code to raise exceptions about not being able to parse the partial data.