Skip to content

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

Merged
1st1 merged 2 commits into
python:masterfrom
fantix:fix-issue-30064
May 27, 2020
Merged

1st1 merged 2 commits into
python:masterfrom
fantix:fix-issue-30064

Conversation

@fantix

@fantix fantix commented May 24, 2020 •

Copy link
Copy Markdown
Contributor

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 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
Comment thread Lib/asyncio/selector_events.py Resolved
Comment thread Lib/asyncio/selector_events.py Resolved
@fantix
fantix requested a review from 1st1 May 26, 2020 00:29
@1st1

1st1 commented May 26, 2020

Copy link
Copy Markdown
Member

@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
Copy Markdown
Contributor

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

@1st1

1st1 commented May 27, 2020

Copy link
Copy Markdown
Member

Thanks, Fantix!

@bedevere-bot

Copy link
Copy Markdown

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

miss-islington added a commit that referenced this pull request May 27, 2020
(cherry picked from commit 210a137)

Co-authored-by: Fantix King <fantix.king@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

5 participants