Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ CHANGES

Unreleased
==========
- Drop what an interrupted command still had buffered when its connection is
discarded, instead of sending it. memcached could carry out a ``set()`` after
the caller got ``TimeoutError``.
- Limit keys to 250 bytes, as memcached does, instead of 250 characters. Longer
non-ASCII keys now raise ``ValidationException`` instead of a misleading
``ClientException`` from the server.
Expand Down
10 changes: 7 additions & 3 deletions aiomcache/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ async def clear(self) -> None:
if conn is not None:
self._do_close(conn)

def _do_close(self, conn: Connection) -> None:
def _do_close(self, conn: Connection, *, abort: bool = False) -> None:
conn.reader.feed_eof()
conn.writer.close()
if abort:
# Drop anything still buffered instead of sending it.
conn.writer.transport.abort()
else:
conn.writer.close()

async def acquire(self) -> Connection:
"""Acquire connection from the pool, or spawn new one
Expand Down Expand Up @@ -92,7 +96,7 @@ def release(self, conn: Connection, *, broken: bool = False) -> None:
broken = broken or conn.reader.at_eof() or conn.reader.exception() is not None
released_conn: Connection | None = conn
if broken:
self._do_close(conn)
self._do_close(conn, abort=True)
# None will wake a waiter so it can discard this slot and create a new conn.
released_conn = None
self._pool.put_nowait(released_conn)
Expand Down
37 changes: 37 additions & 0 deletions tests/pool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,40 @@ async def test_pool_unix_socket_bad_path() -> None:
assert pool.size() == 0
with pytest.raises(FileNotFoundError):
await pool.acquire()


async def test_broken_connection_drops_buffered_writes() -> None:
# A command that timed out may still have data buffered. It must not be
# sent once the connection is discarded, or memcached would carry it out.
value = b"x" * 16 * 1024 * 1024
received = 0
released = asyncio.Event()
done = asyncio.Event()

async def handler(reader: asyncio.StreamReader,
writer: asyncio.StreamWriter) -> None:
nonlocal received
await released.wait() # Don't read until the connection is released.
while data := await reader.read(1024 * 1024):
received += len(data)
writer.close()
done.set()

server = await asyncio.start_server(handler, "127.0.0.1", 0)
port = server.sockets[0].getsockname()[1]
client = Client("127.0.0.1", port, timeout=0.2)
try:
# Open the connection first, so set() buffers its write at once and
# the deadline can't expire before anything is sent.
conn = await client._pool.acquire()
client._pool.release(conn)
with pytest.raises(TimeoutError):
await client.set(b"key", value)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
released.set()
async with asyncio.timeout(10):
await done.wait()
assert received < len(value)
finally:
await client.close()
server.close()
await server.wait_closed()
Loading