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

bpo-30064: Fix asyncio loop.sock_* race condition issue#20369
1st1 merged 2 commits into
python:masterfrom
fantix:fix-issue-30064

Conversation

@fantix

@fantixfantix 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=awaitasyncio.wait([loop.sock_recv(sock, 1024)], timeout=0.1)
ifdone:
data=awaitdone.pop()
else:
pending.pop().cancel()
data=awaitloop.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=awaitasyncio.wait([loop.sock_recv(sock, 1024)], timeout=0.1)
ifdone:
data=awaitdone.pop()
else:
pending.pop().cancel()
awaitasyncio.sleep(0) # <---- this ensures that the right reader is removeddata=awaitloop.sock_recv(sock, 1024)

@fantixfantix changed the title bpo-30064: Fix asyncio sock_recv* racing conditionbpo-30064: Fix asyncio loop.sock_* race condition issueMay 25, 2020
@fantix
fantixforce-pushed the fix-issue-30064 branch 2 times, most recently from f65e373 to a246122CompareMay 25, 2020 20:20
@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 ownersMay 25, 2020 22:40
Comment threadLib/asyncio/selector_events.py
Comment threadLib/asyncio/selector_events.py Outdated
@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:masterMay 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 freeto 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

@fantix@1st1@miss-islington@bedevere-bot@the-knights-who-say-ni