-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fixing cancelled async futures #2666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a3a9a73
try to fix
dvora-h 0cf8aae
starting testing
chayim 4163e85
pipeline
chayim 20893a4
partial fix
chayim 38f7957
Update tests/test_asyncio/test_cwe_404.py
chayim e54d3bb
fail-fast false
chayim b9e92f0
syntax
chayim f373962
con reconnect in one state
chayim e4854de
moved the shield
chayim 6a92da7
linter fix
chayim 715fd8d
Merge branch 'master' into ck-dh-asyncalls
chayim 7744dc6
move try-except inside the asyncio.shield
dvora-h 7f82052
fix pipeline
dvora-h f89c0a7
catching, handling, and validating the error
chayim 9516f21
skip specific test on python 3.7
chayim e0517a8
except and the finally
chayim ceda6ac
tagging tests appropriately
chayim bb65a25
awaits ho
chayim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import asyncio | ||
import sys | ||
|
||
import pytest | ||
|
||
from redis.asyncio import Redis | ||
from redis.asyncio.cluster import RedisCluster | ||
|
||
|
||
async def pipe( | ||
reader: asyncio.StreamReader, writer: asyncio.StreamWriter, delay: float, name="" | ||
): | ||
while True: | ||
data = await reader.read(1000) | ||
if not data: | ||
break | ||
await asyncio.sleep(delay) | ||
writer.write(data) | ||
await writer.drain() | ||
|
||
|
||
class DelayProxy: | ||
def __init__(self, addr, redis_addr, delay: float): | ||
self.addr = addr | ||
self.redis_addr = redis_addr | ||
self.delay = delay | ||
|
||
async def start(self): | ||
self.server = await asyncio.start_server(self.handle, *self.addr) | ||
self.ROUTINE = asyncio.create_task(self.server.serve_forever()) | ||
|
||
async def handle(self, reader, writer): | ||
# establish connection to redis | ||
redis_reader, redis_writer = await asyncio.open_connection(*self.redis_addr) | ||
pipe1 = asyncio.create_task(pipe(reader, redis_writer, self.delay, "to redis:")) | ||
pipe2 = asyncio.create_task( | ||
pipe(redis_reader, writer, self.delay, "from redis:") | ||
) | ||
await asyncio.gather(pipe1, pipe2) | ||
|
||
async def stop(self): | ||
# clean up enough so that we can reuse the looper | ||
self.ROUTINE.cancel() | ||
loop = self.server.get_loop() | ||
await loop.shutdown_asyncgens() | ||
|
||
|
||
async def test_standalone(): | ||
|
||
# create a tcp socket proxy that relays data to Redis and back, | ||
# inserting 0.1 seconds of delay | ||
dp = DelayProxy(addr=("localhost", 5380), redis_addr=("localhost", 6379), delay=0.1) | ||
await dp.start() | ||
|
||
for b in [True, False]: | ||
# note that we connect to proxy, rather than to Redis directly | ||
async with Redis(host="localhost", port=5380, single_connection_client=b) as r: | ||
|
||
await r.set("foo", "foo") | ||
await r.set("bar", "bar") | ||
|
||
t = asyncio.create_task(r.get("foo")) | ||
await asyncio.sleep(0.050) | ||
t.cancel() | ||
try: | ||
await t | ||
sys.stderr.write("try again, we did not cancel the task in time\n") | ||
except asyncio.CancelledError: | ||
sys.stderr.write( | ||
"canceled task, connection is left open with unread response\n" | ||
) | ||
|
||
assert await r.get("bar") == b"bar" | ||
assert await r.ping() | ||
assert await r.get("foo") == b"foo" | ||
|
||
await dp.stop() | ||
|
||
|
||
async def test_standalone_pipeline(): | ||
dp = DelayProxy(addr=("localhost", 5380), redis_addr=("localhost", 6379), delay=0.1) | ||
await dp.start() | ||
async with Redis(host="localhost", port=5380) as r: | ||
await r.set("foo", "foo") | ||
await r.set("bar", "bar") | ||
|
||
pipe = r.pipeline() | ||
|
||
pipe2 = r.pipeline() | ||
pipe2.get("bar") | ||
pipe2.ping() | ||
pipe2.get("foo") | ||
|
||
t = asyncio.create_task(pipe.get("foo").execute()) | ||
await asyncio.sleep(0.050) | ||
t.cancel() | ||
|
||
pipe.get("bar") | ||
pipe.ping() | ||
pipe.get("foo") | ||
assert await pipe.execute() == [b"foo", b"bar", True, b"foo"] | ||
assert await pipe2.execute() == [b"bar", True, b"foo"] | ||
|
||
await dp.stop() | ||
|
||
|
||
async def test_cluster(request): | ||
|
||
dp = DelayProxy(addr=("localhost", 5381), redis_addr=("localhost", 6372), delay=0.1) | ||
await dp.start() | ||
|
||
r = RedisCluster.from_url("redis://localhost:5381") | ||
await r.initialize() | ||
await r.set("foo", "foo") | ||
await r.set("bar", "bar") | ||
|
||
t = asyncio.create_task(r.get("foo")) | ||
await asyncio.sleep(0.050) | ||
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" | ||
|
||
await dp.stop() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.