Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-88968: Reject socket that is already used as a transport #98010

Merged
merged 4 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, selector=None):

def _make_socket_transport(self, sock, protocol, waiter=None, *,
extra=None, server=None):
self._ensure_fd_no_transport(sock)
return _SelectorSocketTransport(self, sock, protocol, waiter,
extra, server)

Expand All @@ -68,6 +69,7 @@ def _make_ssl_transport(
ssl_handshake_timeout=constants.SSL_HANDSHAKE_TIMEOUT,
ssl_shutdown_timeout=constants.SSL_SHUTDOWN_TIMEOUT,
):
self._ensure_fd_no_transport(rawsock)
ssl_protocol = sslproto.SSLProtocol(
self, protocol, sslcontext, waiter,
server_side, server_hostname,
Expand All @@ -80,6 +82,7 @@ def _make_ssl_transport(

def _make_datagram_transport(self, sock, protocol,
address=None, waiter=None, extra=None):
self._ensure_fd_no_transport(sock)
return _SelectorDatagramTransport(self, sock, protocol,
address, waiter, extra)

Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ def setUp(self):
def test_make_socket_transport(self):
m = mock.Mock()
self.loop.add_reader = mock.Mock()
self.loop._ensure_fd_no_transport = mock.Mock()
transport = self.loop._make_socket_transport(m, asyncio.Protocol())
self.assertIsInstance(transport, _SelectorSocketTransport)
self.assertEqual(self.loop._ensure_fd_no_transport.call_count, 1)

# Calling repr() must not fail when the event loop is closed
self.loop.close()
Expand All @@ -78,8 +80,10 @@ def test_make_ssl_transport_without_ssl_error(self):
self.loop.add_writer = mock.Mock()
self.loop.remove_reader = mock.Mock()
self.loop.remove_writer = mock.Mock()
self.loop._ensure_fd_no_transport = mock.Mock()
with self.assertRaises(RuntimeError):
self.loop._make_ssl_transport(m, m, m, m)
self.assertEqual(self.loop._ensure_fd_no_transport.call_count, 1)

def test_close(self):
class EventLoop(BaseSelectorEventLoop):
Expand Down