8000 gh-90871: fix connection backlog offset in asyncio by ChristianHrs · Pull Request #134392 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-90871: fix connection backlog offset in asyncio #134392

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

Merged
merged 13 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix connection backlog offset + updated tests + added test for backlo…
…g=0 and backlog=1
  • Loading branch information
ChristianHrs committed May 20, 2025
commit a78bde0607df475e3ac009485ec22afba8d05e59
2 changes: 1 addition & 1 deletion Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _accept_connection(
# listening socket has triggered an EVENT_READ. There may be multiple
# connections waiting for an .accept() so it is called in a loop.
# See https://bugs.python.org/issue27906 for more details.
for _ in range(backlog):
for _ in range(backlog + 1):
try:
conn, addr = sock.accept()
if self._debug:
Expand Down
16 changes: 14 additions & 2 deletions Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ def test_process_events_write_cancelled(self):
selectors.EVENT_WRITE)])
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):
sock = mock.Mock()
sock.accept.return_value = (mock.Mock(), mock.Mock())
Expand All @@ -362,7 +374,7 @@ def test_accept_connection_multiple(self):
self.loop._accept_connection(
mock.Mock(), sock, backlog=backlog)
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):
sock = mock.Mock()
Expand All @@ -388,7 +400,7 @@ def mock_sock_accept():
# as in test_accept_connection_multiple avoid task pending
# warnings by using 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):

Expand Down
0