Skip to content

Commit 2210966

Browse files
Eclips4pull[bot]
authored andcommitted
gh-113892: Add a extra check to ProactorEventLoop.sock_connect to ensure that the given socket is in non-blocking mode (#119519)
1 parent d5a1441 commit 2210966

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
@@ -721,6 +721,8 @@ async def sock_sendto(self, sock, data, address):
721721
return await self._proactor.sendto(sock, data, 0, address)
722722

723723
async def sock_connect(self, sock, address):
724+
if self._debug and sock.gettimeout() != 0:
725+
raise ValueError("the socket must be non-blocking")
724726
return await self._proactor.connect(sock, address)
725727

726728
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
@@ -1018,9 +1018,9 @@ def setUp(self):
10181018
self.addCleanup(self.file.close)
10191019
super().setUp()
10201020

1021-
def make_socket(self, cleanup=True):
1021+
def make_socket(self, cleanup=True, blocking=False):
10221022
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1023-
sock.setblocking(False)
1023+
sock.setblocking(blocking)
10241024
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
10251025
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024)
10261026
if cleanup:
@@ -1082,6 +1082,11 @@ def test_sock_sendfile_not_regular_file(self):
10821082
0, None))
10831083
self.assertEqual(self.file.tell(), 0)
10841084

1085+
def test_blocking_socket(self):
1086+
self.loop.set_debug(True)
1087+
sock = self.make_socket(blocking=True)
1088+
with self.assertRaisesRegex(ValueError, "must be non-blocking"):
1089+
self.run_loop(self.loop.sock_sendfile(sock, self.file))
10851090

10861091
if __name__ == '__main__':
10871092
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)