mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
gh-90871: fix connection backlog offset in asyncio (gh-134392)
Co-authored-by: Łukasz Langa <lukasz@langa.pl> Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
parent
15a8b5b9bd
commit
109f7597d2
3 changed files with 17 additions and 3 deletions
|
@ -173,7 +173,7 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
# listening socket has triggered an EVENT_READ. There may be multiple
|
# listening socket has triggered an EVENT_READ. There may be multiple
|
||||||
# connections waiting for an .accept() so it is called in a loop.
|
# connections waiting for an .accept() so it is called in a loop.
|
||||||
# See https://bugs.python.org/issue27906 for more details.
|
# See https://bugs.python.org/issue27906 for more details.
|
||||||
for _ in range(backlog):
|
for _ in range(backlog + 1):
|
||||||
try:
|
try:
|
||||||
conn, addr = sock.accept()
|
conn, addr = sock.accept()
|
||||||
if self._debug:
|
if self._debug:
|
||||||
|
|
|
@ -347,6 +347,18 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
|
||||||
selectors.EVENT_WRITE)])
|
selectors.EVENT_WRITE)])
|
||||||
self.loop._remove_writer.assert_called_with(1)
|
self.loop._remove_writer.assert_called_with(1)
|
||||||
|
|
||||||
|
def test_accept_connection_zero_one(self):
|
||||||
|
for backlog in [0, 1]:
|
||||||
|
sock = mock.Mock()
|
||||||
|
sock.accept.return_value = (mock.Mock(), mock.Mock())
|
||||||
|
with self.subTest(backlog):
|
||||||
|
mock_obj = mock.patch.object
|
||||||
|
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
|
||||||
|
self.loop._accept_connection(
|
||||||
|
mock.Mock(), sock, backlog=backlog)
|
||||||
|
self.loop.run_until_complete(asyncio.sleep(0))
|
||||||
|
self.assertEqual(sock.accept.call_count, backlog + 1)
|
||||||
|
|
||||||
def test_accept_connection_multiple(self):
|
def test_accept_connection_multiple(self):
|
||||||
sock = mock.Mock()
|
sock = mock.Mock()
|
||||||
sock.accept.return_value = (mock.Mock(), mock.Mock())
|
sock.accept.return_value = (mock.Mock(), mock.Mock())
|
||||||
|
@ -362,7 +374,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
|
||||||
self.loop._accept_connection(
|
self.loop._accept_connection(
|
||||||
mock.Mock(), sock, backlog=backlog)
|
mock.Mock(), sock, backlog=backlog)
|
||||||
self.loop.run_until_complete(asyncio.sleep(0))
|
self.loop.run_until_complete(asyncio.sleep(0))
|
||||||
self.assertEqual(sock.accept.call_count, backlog)
|
self.assertEqual(sock.accept.call_count, backlog + 1)
|
||||||
|
|
||||||
def test_accept_connection_skip_connectionabortederror(self):
|
def test_accept_connection_skip_connectionabortederror(self):
|
||||||
sock = mock.Mock()
|
sock = mock.Mock()
|
||||||
|
@ -388,7 +400,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
|
||||||
# as in test_accept_connection_multiple avoid task pending
|
# as in test_accept_connection_multiple avoid task pending
|
||||||
# warnings by using asyncio.sleep(0)
|
# warnings by using asyncio.sleep(0)
|
||||||
self.loop.run_until_complete(asyncio.sleep(0))
|
self.loop.run_until_complete(asyncio.sleep(0))
|
||||||
self.assertEqual(sock.accept.call_count, backlog)
|
self.assertEqual(sock.accept.call_count, backlog + 1)
|
||||||
|
|
||||||
class SelectorTransportTests(test_utils.TestCase):
|
class SelectorTransportTests(test_utils.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fixed an off by one error concerning the backlog parameter in
|
||||||
|
:meth:`~asyncio.loop.create_unix_server`. Contributed by Christian Harries.
|
Loading…
Add table
Add a link
Reference in a new issue