]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-127529: Correct asyncio's `accept_connection` behaviour for handling `Conne...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 15 Dec 2025 05:44:52 +0000 (06:44 +0100)
committerGitHub <noreply@github.com>
Mon, 15 Dec 2025 05:44:52 +0000 (05:44 +0000)
gh-127529: Correct asyncio's `accept_connection` behaviour for handling `ConnectionAbortedError` (GH-127532)
(cherry picked from commit 830e10651b1f45cd0af36ff611397b9f53171220)

Co-authored-by: jb2170 <email@jb2170.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Lib/asyncio/selector_events.py
Lib/test/test_asyncio/test_selector_events.py
Misc/NEWS.d/next/Library/2024-12-02-19-13-19.gh-issue-127529.Pj1Xtf.rst [new file with mode: 0644]

index 8701467d41322ab13fd485969715ed6adbc7748c..3da92adf71fc7508d46789cb8c75cfef1600bb88 100644 (file)
@@ -180,9 +180,13 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
                     logger.debug("%r got a new connection from %r: %r",
                                  server, addr, conn)
                 conn.setblocking(False)
-            except (BlockingIOError, InterruptedError, ConnectionAbortedError):
-                # Early exit because the socket accept buffer is empty.
-                return None
+            except ConnectionAbortedError:
+                # Discard connections that were aborted before accept().
+                continue
+            except (BlockingIOError, InterruptedError):
+                # Early exit because of a signal or
+                # the socket accept buffer is empty.
+                return
             except OSError as exc:
                 # There's nowhere to send the error, so just log it.
                 if exc.errno in (errno.EMFILE, errno.ENFILE,
index bfb347e4a8cf2e3ab729c98efc2ad87f709a7ba1..3311376b3e2e94cdcc7a9268d638793501a5ad07 100644 (file)
@@ -364,6 +364,31 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
         self.loop.run_until_complete(asyncio.sleep(0))
         self.assertEqual(sock.accept.call_count, backlog)
 
+    def test_accept_connection_skip_connectionabortederror(self):
+        sock = mock.Mock()
+
+        def mock_sock_accept():
+            # mock accept(2) returning -ECONNABORTED every-other
+            # time that it's called. This applies most to OpenBSD
+            # whose sockets generate this errno more reproducibly than
+            # Linux and other OS.
+            if sock.accept.call_count % 2 == 0:
+                raise ConnectionAbortedError
+            return (mock.Mock(), mock.Mock())
+
+        sock.accept.side_effect = mock_sock_accept
+        backlog = 100
+        # test that _accept_connection's loop calls sock.accept
+        # all 100 times, continuing past ConnectionAbortedError
+        # instead of unnecessarily returning early
+        mock_obj = mock.patch.object
+        with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
+            self.loop._accept_connection(
+                mock.Mock(), sock, backlog=backlog)
+        # 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)
 
 class SelectorTransportTests(test_utils.TestCase):
 
diff --git a/Misc/NEWS.d/next/Library/2024-12-02-19-13-19.gh-issue-127529.Pj1Xtf.rst b/Misc/NEWS.d/next/Library/2024-12-02-19-13-19.gh-issue-127529.Pj1Xtf.rst
new file mode 100644 (file)
index 0000000..26f2fd5
--- /dev/null
@@ -0,0 +1,4 @@
+Correct behavior of
+:func:`!asyncio.selector_events.BaseSelectorEventLoop._accept_connection`
+in handling :exc:`ConnectionAbortedError` in a loop. This improves
+performance on OpenBSD.