Skip to content
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

bpo-33694: Fix race condition on proactor recv() #7498

Merged
merged 4 commits into from
Jun 7, 2018
Merged

bpo-33694: Fix race condition on proactor recv() #7498

merged 4 commits into from
Jun 7, 2018

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jun 7, 2018

The cancellation of an overlapped WSARecv() has a race condition
which causes data loss because of the current implementation of
proactor in asyncio.

No longer cancel overlapped WSARecv() in _ProactorReadPipeTransport
to work around the race condition.

Remove the optimized recv_into() implementation to get simple
implementation of pause_reading() using the single _pending_data
attribute.

https://bugs.python.org/issue33694

The cancellation of an overlapped WSARecv() has a race condition
which causes data loss because of the current implementation of
proactor in asyncio.

No longer cancel overlapped WSARecv() in _ProactorReadPipeTransport
to work around the race condition.

Remove the optimized recv_into() implementation to get simple
implementation of pause_reading() using the single _pending_data
attribute.
@vstinner
Copy link
Member Author

vstinner commented Jun 7, 2018

I ran manually race.py of https://bugs.python.org/issue33694:

  • 5000 packets of 16 KiB
  • 50 packets of 16 MiB

This script didn't detect any regression, whereas these two tests always reproduced the race condition in a reliable way.

nbytes = len(data)
if nbytes:
try:
buf = self._protocol.get_buffer(-1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should pass nbytes to get_buffer(). Also, get_buffer() can return a smaller buffer than requested. So you better should use sslproto._feed_data_to_bufferred_proto helper to make sure that all of the received data is passed to the BufferedProtocol.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you better should use sslproto._feed_data_to_bufferred_proto helper

Ok, done.

Remove set_protocol() which became useless
return

if isinstance(self._protocol, protocols.BufferedProtocol):
try:
sslproto._feed_data_to_bufferred_proto(self._protocol, data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also move _feed_data_to_bufferred_proto helper to asyncio/protocols.py so that this is less confusing for whoever reads the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, done.

@@ -459,6 +459,8 @@ def test_dont_pause_writing(self):
self.assertFalse(self.protocol.pause_writing.called)


@unittest.skip('FIXME: bpo-33694: these tests are too close '
'to the implementation and should be refactored or removed')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

@vstinner
Copy link
Member Author

vstinner commented Jun 7, 2018

I tested manually the latest change ("Move _feed_data_to_bufferred_proto() to protocols") on my Windows VM:

  • race.py with 50 packets of 16 MiB: ok
  • race.py with 2000 packets of 16 KiB: ok
  • python -m test test_asyncio -m test_start_tls_1 -F: still ok after 300 runs

Each of these tests always reproduced the race condition with the fix.

Note: test_asyncio pass as well ;-)

@1st1
Copy link
Member

1st1 commented Jun 7, 2018

Each of these tests always reproduced the race condition with the fix.

Here, a tired but happy smiley face from me: 😍

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proactor requires an alternative way of thinking.
Looks good, the implementation is cleaner than before.

@vstinner vstinner merged commit 79790bc into python:master Jun 7, 2018
@vstinner vstinner deleted the proactor_buffer branch June 7, 2018 22:25
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 7, 2018
The cancellation of an overlapped WSARecv() has a race condition
which causes data loss because of the current implementation of
proactor in asyncio.

No longer cancel overlapped WSARecv() in _ProactorReadPipeTransport
to work around the race condition.

Remove the optimized recv_into() implementation to get simple
implementation of pause_reading() using the single _pending_data
attribute.

Move _feed_data_to_bufferred_proto() to protocols.py.

Remove set_protocol() method which became useless.
(cherry picked from commit 79790bc)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
@bedevere-bot
Copy link

GH-7499 is a backport of this pull request to the 3.7 branch.

vstinner added a commit that referenced this pull request Jun 7, 2018
The cancellation of an overlapped WSARecv() has a race condition
which causes data loss because of the current implementation of
proactor in asyncio.

No longer cancel overlapped WSARecv() in _ProactorReadPipeTransport
to work around the race condition.

Remove the optimized recv_into() implementation to get simple
implementation of pause_reading() using the single _pending_data
attribute.

Move _feed_data_to_bufferred_proto() to protocols.py.

Remove set_protocol() method which became useless.
(cherry picked from commit 79790bc)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
@njsmith
Copy link
Contributor

njsmith commented Jun 8, 2018

I didn't look at the code but fyi in passing: "bufferred" is misspelled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants