Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions aiomcache/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ async def _execute_simple_command(self, conn: Connection, raw_command: bytes) ->

while not line.endswith(b'\r\n'):
line = await conn.reader.readline()
if not line:
# readline() returns an empty string at EOF.
raise ClientException(
'Connection closed by the server before receiving a full response',
)
response.extend(line)
return response[:-2]

Expand Down
20 changes: 20 additions & 0 deletions tests/commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,26 @@ async def test_close(mcache: Client) -> None:
assert mcache._pool.size() == 0


async def test_simple_command_server_dies_mid_response() -> None:
async def handler(reader: asyncio.StreamReader,
writer: asyncio.StreamWriter) -> None:
await reader.readline()
writer.write(b"VERSION 1.6.0") # incomplete line, no \r\n
await writer.drain()
writer.close()

server = await asyncio.start_server(handler, "127.0.0.1", 0)
port = server.sockets[0].getsockname()[1]
client = Client("127.0.0.1", port)
try:
with pytest.raises(ClientException, match="closed by the server"):
await asyncio.wait_for(client.version(), timeout=5)
finally:
await client.close()
server.close()
await server.wait_closed()


@pytest.mark.parametrize(
"value",
[
Expand Down