Skip to content

Commit 55f6515

Browse files
[3.12] gh-113892: Add a extra check to ProactorEventLoop.sock_connect to ensure that the given socket is in non-blocking mode (GH-119519) (#119913)
(cherry picked from commit cf3bba3) Co-authored-by: Kirill Podoprigora <[email protected]>
1 parent 34a0e7a commit 55f6515

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/asyncio/proactor_events.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,8 @@ async def sock_sendto(self, sock, data, address):
724724
return await self._proactor.sendto(sock, data, 0, address)
725725

726726
async def sock_connect(self, sock, address):
727+
if self._debug and sock.gettimeout() != 0:
728+
raise ValueError("the socket must be non-blocking")
727729
return await self._proactor.connect(sock, address)
728730

729731
async def sock_accept(self, sock):

Lib/test/test_asyncio/test_proactor_events.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,9 @@ def setUp(self):
10061006
self.addCleanup(self.file.close)
10071007
super().setUp()
10081008

1009-
def make_socket(self, cleanup=True):
1009+
def make_socket(self, cleanup=True, blocking=False):
10101010
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1011-
sock.setblocking(False)
1011+
sock.setblocking(blocking)
10121012
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
10131013
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024)
10141014
if cleanup:
@@ -1070,6 +1070,11 @@ def test_sock_sendfile_not_regular_file(self):
10701070
0, None))
10711071
self.assertEqual(self.file.tell(), 0)
10721072

1073+
def test_blocking_socket(self):
1074+
self.loop.set_debug(True)
1075+
sock = self.make_socket(blocking=True)
1076+
with self.assertRaisesRegex(ValueError, "must be non-blocking"):
1077+
self.run_loop(self.loop.sock_sendfile(sock, self.file))
10731078

10741079
if __name__ == '__main__':
10751080
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Now, the method ``sock_connect`` of :class:`asyncio.ProactorEventLoop`
2+
raises a :exc:`ValueError` if given socket is not in
3+
non-blocking mode, as well as in other loop implementations.

0 commit comments

Comments
 (0)