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

GH-74116: allow multiple drain waiters for asyncio.StreamWriter #94705

Merged
merged 3 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions Lib/asyncio/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'StreamReader', 'StreamWriter', 'StreamReaderProtocol',
'open_connection', 'start_server')

import collections
import socket
import sys
import weakref
Expand Down Expand Up @@ -128,7 +129,7 @@ def __init__(self, loop=None):
else:
self._loop = loop
self._paused = False
self._drain_waiter = None
self._drain_waiters = collections.deque()
self._connection_lost = False

def pause_writing(self):
Expand All @@ -143,9 +144,7 @@ def resume_writing(self):
if self._loop.get_debug():
logger.debug("%r resumes writing", self)

waiter = self._drain_waiter
if waiter is not None:
self._drain_waiter = None
for waiter in self._drain_waiters:
if not waiter.done():
waiter.set_result(None)

Expand All @@ -154,27 +153,25 @@ def connection_lost(self, exc):
# Wake up the writer if currently paused.
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
if not self._paused:
return
waiter = self._drain_waiter
if waiter is None:
return
self._drain_waiter = None
if waiter.done():
return
if exc is None:
waiter.set_result(None)
else:
waiter.set_exception(exc)

for waiter in self._drain_waiters:
if not waiter.done():
if exc is None:
waiter.set_result(None)
else:
waiter.set_exception(exc)
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved

async def _drain_helper(self):
if self._connection_lost:
raise ConnectionResetError('Connection lost')
if not self._paused:
return
waiter = self._drain_waiter
assert waiter is None or waiter.cancelled()
waiter = self._loop.create_future()
self._drain_waiter = waiter
await waiter
self._drain_waiters.append(waiter)
try:
await waiter
finally:
self._drain_waiters.remove(waiter)
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved

def _get_close_waiter(self, stream):
raise NotImplementedError
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,25 @@ def test_streamreaderprotocol_constructor_use_global_loop(self):
self.assertEqual(cm.filename, __file__)
self.assertIs(protocol._loop, self.loop)

def test_multiple_drain(self):
# See https://github.com/python/cpython/issues/74116
drained = 0

async def drainer(stream):
nonlocal drained
await stream._drain_helper()
drained += 1

async def main():
loop = asyncio.get_running_loop()
stream = asyncio.streams.FlowControlMixin(loop)
stream.pause_writing()
loop.call_later(0.1, stream.resume_writing)
await asyncio.gather(*[drainer(stream) for _ in range(10)])
self.assertEqual(drained, 10)

self.loop.run_until_complete(main())

def test_drain_raises(self):
# See http://bugs.python.org/issue25441

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :meth:`asyncio.StreamWriter.drain` to be awaited concurrently by multiple tasks. Patch by Kumar Aditya.
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved