]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-113892: Add a extra check to `ProactorEventLoop.sock_connect` to ensure...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 1 Jun 2024 16:29:34 +0000 (18:29 +0200)
committerGitHub <noreply@github.com>
Sat, 1 Jun 2024 16:29:34 +0000 (16:29 +0000)
(cherry picked from commit cf3bba3f0671d2c9fee099e3ab0f78b98b176131)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Lib/asyncio/proactor_events.py
Lib/test/test_asyncio/test_proactor_events.py
Misc/NEWS.d/next/Library/2024-05-24-21-54-55.gh-issue-113892.JKDFqq.rst [new file with mode: 0644]

index 1e2a730cf368a99f51a049352a376eb4a63597b3..23ea29c677a2ba7f7cadf3f0d6a15cb41c816483 100644 (file)
@@ -724,6 +724,8 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
         return await self._proactor.sendto(sock, data, 0, address)
 
     async def sock_connect(self, sock, address):
+        if self._debug and sock.gettimeout() != 0:
+            raise ValueError("the socket must be non-blocking")
         return await self._proactor.connect(sock, address)
 
     async def sock_accept(self, sock):
index c42856e578b8cc8b6d2ce332e6ccbeea4815852c..1c7dff95764a978a2b1bd375e4f71bbebccddfd5 100644 (file)
@@ -1006,9 +1006,9 @@ class ProactorEventLoopUnixSockSendfileTests(test_utils.TestCase):
         self.addCleanup(self.file.close)
         super().setUp()
 
-    def make_socket(self, cleanup=True):
+    def make_socket(self, cleanup=True, blocking=False):
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        sock.setblocking(False)
+        sock.setblocking(blocking)
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024)
         if cleanup:
@@ -1070,6 +1070,11 @@ class ProactorEventLoopUnixSockSendfileTests(test_utils.TestCase):
                                                           0, None))
         self.assertEqual(self.file.tell(), 0)
 
+    def test_blocking_socket(self):
+        self.loop.set_debug(True)
+        sock = self.make_socket(blocking=True)
+        with self.assertRaisesRegex(ValueError, "must be non-blocking"):
+            self.run_loop(self.loop.sock_sendfile(sock, self.file))
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2024-05-24-21-54-55.gh-issue-113892.JKDFqq.rst b/Misc/NEWS.d/next/Library/2024-05-24-21-54-55.gh-issue-113892.JKDFqq.rst
new file mode 100644 (file)
index 0000000..639d5ab
--- /dev/null
@@ -0,0 +1,3 @@
+Now, the method ``sock_connect`` of :class:`asyncio.ProactorEventLoop`
+raises a :exc:`ValueError` if given socket is not in
+non-blocking mode, as well as in other loop implementations.