Skip to content

bpo-30064: Fix asyncio loop.sock_* race condition issue #20369

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 2 commits into from
May 27, 2020

Conversation

fantix
Copy link
Contributor

@fantix fantix commented May 24, 2020

https://bugs.python.org/issue30064

This is reproducible by a cancel() followed directly by a retry:

done, pending = await asyncio.wait([loop.sock_recv(sock, 1024)], timeout=0.1)
if done:
    data = await done.pop()
else:
    pending.pop().cancel()
    data = await loop.sock_recv(sock, 1024)

This issue applies to all the loop.sock_* methods. Without this PR, users could work it around by adding a sleep(0):

done, pending = await asyncio.wait([loop.sock_recv(sock, 1024)], timeout=0.1)
if done:
    data = await done.pop()
else:
    pending.pop().cancel()
    await asyncio.sleep(0)  # <---- this ensures that the right reader is removed
    data = await loop.sock_recv(sock, 1024)

@fantix fantix force-pushed the fix-issue-30064 branch from 2bb44f9 to 04aa2a8 Compare May 25, 2020 16:54
@fantix fantix changed the title bpo-30064: Fix asyncio sock_recv* racing condition bpo-30064: Fix asyncio loop.sock_* race condition issue May 25, 2020
@fantix fantix force-pushed the fix-issue-30064 branch from 04aa2a8 to 2338f68 Compare May 25, 2020 18:58
@fantix fantix force-pushed the fix-issue-30064 branch 2 times, most recently from f65e373 to a246122 Compare May 25, 2020 20:20
@fantix fantix force-pushed the fix-issue-30064 branch from a246122 to 05c0db4 Compare May 25, 2020 21:09
@fantix fantix force-pushed the fix-issue-30064 branch from 05c0db4 to ee75018 Compare May 25, 2020 22:04
@fantix fantix marked this pull request as ready for review May 25, 2020 22:40
@fantix fantix requested review from 1st1 and asvetlov as code owners May 25, 2020 22:40
@fantix fantix requested a review from 1st1 May 26, 2020 00:29
@1st1 1st1 added the needs backport to 3.9 only security fixes label May 26, 2020
@1st1
Copy link
Member

1st1 commented May 26, 2020

@asvetlov Please take a look if you have a few minutes.

@1st1 1st1 merged commit 210a137 into python:master May 27, 2020
@miss-islington
Copy link
Contributor

Thanks @fantix for the PR, and @1st1 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@1st1
Copy link
Member

1st1 commented May 27, 2020

Thanks, Fantix!

@bedevere-bot
Copy link

GH-20460 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label May 27, 2020
miss-islington added a commit that referenced this pull request May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants