Skip to content

Commit

Permalink
[3.13] gh-113892: Add a extra check to `ProactorEventLoop.sock_connec…
Browse files Browse the repository at this point in the history
…t` to ensure that the given socket is in non-blocking mode (GH-119519) (#119912)

(cherry picked from commit cf3bba3)

Co-authored-by: Kirill Podoprigora <[email protected]>
  • Loading branch information
miss-islington and Eclips4 authored Jun 1, 2024
1 parent 4e147ca commit 0ea77d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Lib/asyncio/proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,8 @@ async def sock_sendto(self, sock, data, address):
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):
Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_asyncio/test_proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,9 +1018,9 @@ def setUp(self):
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:
Expand Down Expand Up @@ -1082,6 +1082,11 @@ def test_sock_sendfile_not_regular_file(self):
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()
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 0ea77d4

Please sign in to comment.