summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChayim <chayim@users.noreply.github.com>2023-03-22 18:04:01 +0200
committerGitHub <noreply@github.com>2023-03-22 18:04:01 +0200
commitb3c89acd0ffe8303649ad8207bc911b1d6a033eb (patch)
tree8a2bf6473ab828e9542b2ebe493254831db74d3e
parent8592cacf9e5069f8f6d392a2bc02aeade87c9d69 (diff)
downloadredis-py-b3c89acd0ffe8303649ad8207bc911b1d6a033eb.tar.gz
AsyncIO Race Condition Fix (#2640)v4.4.3
-rw-r--r--.github/workflows/integration.yaml4
-rw-r--r--redis/asyncio/client.py12
-rw-r--r--redis/asyncio/cluster.py12
-rw-r--r--setup.py2
-rw-r--r--tests/test_asyncio/test_cluster.py17
-rw-r--r--tests/test_asyncio/test_connection.py22
6 files changed, 61 insertions, 8 deletions
diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml
index 8d38cd4..5b53400 100644
--- a/.github/workflows/integration.yaml
+++ b/.github/workflows/integration.yaml
@@ -13,8 +13,8 @@ on:
branches:
- master
- '[0-9].[0-9]'
- schedule:
- - cron: '0 1 * * *' # nightly build
+# schedule:
+# - cron: '0 1 * * *' # nightly build
permissions:
contents: read # to fetch code (actions/checkout)
diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py
index e56fd02..0bd21d3 100644
--- a/redis/asyncio/client.py
+++ b/redis/asyncio/client.py
@@ -1374,10 +1374,16 @@ class Pipeline(Redis): # lgtm [py/init-calls-subclass]
conn = cast(Connection, conn)
try:
- return await conn.retry.call_with_retry(
- lambda: execute(conn, stack, raise_on_error),
- lambda error: self._disconnect_raise_reset(conn, error),
+ return await asyncio.shield(
+ conn.retry.call_with_retry(
+ lambda: execute(conn, stack, raise_on_error),
+ lambda error: self._disconnect_raise_reset(conn, error),
+ )
)
+ except asyncio.CancelledError:
+ # not supposed to be possible, yet here we are
+ await conn.disconnect(nowait=True)
+ raise
finally:
await self.reset()
diff --git a/redis/asyncio/cluster.py b/redis/asyncio/cluster.py
index 5a2dffd..569a076 100644
--- a/redis/asyncio/cluster.py
+++ b/redis/asyncio/cluster.py
@@ -1002,10 +1002,18 @@ class ClusterNode:
await connection.send_packed_command(connection.pack_command(*args), False)
# Read response
+ return await asyncio.shield(
+ self._parse_and_release(connection, args[0], **kwargs)
+ )
+
+ async def _parse_and_release(self, connection, *args, **kwargs):
try:
- return await self.parse_response(connection, args[0], **kwargs)
+ return await self.parse_response(connection, *args, **kwargs)
+ except asyncio.CancelledError:
+ # should not be possible
+ await connection.disconnect(nowait=True)
+ raise
finally:
- # Release connection
self._free.append(connection)
async def execute_pipeline(self, commands: List["PipelineCommand"]) -> bool:
diff --git a/setup.py b/setup.py
index 16a9156..4314d77 100644
--- a/setup.py
+++ b/setup.py
@@ -8,7 +8,7 @@ setup(
long_description_content_type="text/markdown",
keywords=["Redis", "key-value store", "database"],
license="MIT",
- version="4.4.2",
+ version="4.4.3",
packages=find_packages(
include=[
"redis",
diff --git a/tests/test_asyncio/test_cluster.py b/tests/test_asyncio/test_cluster.py
index 13e5e26..0857c05 100644
--- a/tests/test_asyncio/test_cluster.py
+++ b/tests/test_asyncio/test_cluster.py
@@ -340,6 +340,23 @@ class TestRedisClusterObj:
rc = RedisCluster.from_url("rediss://localhost:16379")
assert rc.connection_kwargs["connection_class"] is SSLConnection
+ async def test_asynckills(self, r) -> None:
+
+ await r.set("foo", "foo")
+ await r.set("bar", "bar")
+
+ t = asyncio.create_task(r.get("foo"))
+ await asyncio.sleep(1)
+ t.cancel()
+ try:
+ await t
+ except asyncio.CancelledError:
+ pytest.fail("connection is left open with unread response")
+
+ assert await r.get("bar") == b"bar"
+ assert await r.ping()
+ assert await r.get("foo") == b"foo"
+
async def test_max_connections(
self, create_redis: Callable[..., RedisCluster]
) -> None:
diff --git a/tests/test_asyncio/test_connection.py b/tests/test_asyncio/test_connection.py
index bf59dbe..ccd6cdb 100644
--- a/tests/test_asyncio/test_connection.py
+++ b/tests/test_asyncio/test_connection.py
@@ -41,6 +41,28 @@ async def test_invalid_response(create_redis):
await r.connection.disconnect()
+@pytest.mark.onlynoncluster
+async def test_asynckills(create_redis):
+
+ for b in [True, False]:
+ r = await create_redis(single_connection_client=b)
+
+ await r.set("foo", "foo")
+ await r.set("bar", "bar")
+
+ t = asyncio.create_task(r.get("foo"))
+ await asyncio.sleep(1)
+ t.cancel()
+ try:
+ await t
+ except asyncio.CancelledError:
+ pytest.fail("connection left open with unread response")
+
+ assert await r.get("bar") == b"bar"
+ assert await r.ping()
+ assert await r.get("foo") == b"foo"
+
+
@skip_if_server_version_lt("4.0.0")
@pytest.mark.redismod
@pytest.mark.onlynoncluster