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

Asyncio socket-level methods do not support SSLSocket #122706

Closed
NoahStapp opened this issue Aug 5, 2024 · 4 comments
Closed

Asyncio socket-level methods do not support SSLSocket #122706

NoahStapp opened this issue Aug 5, 2024 · 4 comments
Labels
docs Documentation in the Doc dir topic-asyncio

Comments

@NoahStapp
Copy link

NoahStapp commented Aug 5, 2024

Attempting to use a SSLSocket with theasyncio.loop.sock_sendall method and the other socket-level async functions raises the following error:

TypeError: Socket cannot be of type SSLSocket

This can be reproduced by the following if socket is a SSLSocket:

 await asyncio.wait_for(loop.sock_sendall(socket, buf), timeout=timeout) 

However, the ssl-nonblocking section of the docs says the following:

"See also: The asyncio module supports non-blocking SSL sockets and provides a higher level API. It polls for events using the selectors module and handles SSLWantWriteError, SSLWantReadError and BlockingIOError exceptions. It runs the SSL handshake asynchronously as well."

Is this section specifically referencing the streams API as the SSL-compatible asyncio API? The current wording is unclear and suggests to me that non-blocking SSLSocket sockets are supported by asyncio, despite the error above. Is there a different section that specifies that SSLSocket is not supported by the socket-level asyncio API?

Linked PRs

@NoahStapp NoahStapp added the docs Documentation in the Doc dir label Aug 5, 2024
@github-project-automation github-project-automation bot moved this to Todo in asyncio Aug 5, 2024
@ZeroIntensity
Copy link
Member

ZeroIntensity commented Aug 6, 2024

I'm wondering if this isn't a mistake in the docs, but a bug in _check_ssl_socket (see here):

def _check_ssl_socket(sock):
    if ssl is not None and isinstance(sock, ssl.SSLSocket):
        raise TypeError("Socket cannot be of type SSLSocket")

It doesn't make much sense to me to raise the error when ssl is not None, it should be vice versa. I'm assuming it was done to prevent a None access on the isinstance check, but if it is supposed to support SSL sockets, per the docs, then this looks wrong.

@ShaneHarvey
Copy link
Contributor

The ssl is not None check is to ensure the ssl module is available (IIRC cpython can be built without the ssl module):

try:
    import ssl
except ImportError:  # pragma: no cover
    ssl = None

@ZeroIntensity
Copy link
Member

The ssl is not None check is to ensure the ssl module is available (IIRC cpython can be built without the ssl module):

try:
    import ssl
except ImportError:  # pragma: no cover
    ssl = None

Yup, that's why the check is there, but I was wondering wondering if it was a mistake. Anyways, I take that back, this is a docs mistake.

@kumaraditya303
Copy link
Contributor

Fixed by #128092

@github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Dec 19, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 19, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 19, 2024
kumaraditya303 added a commit that referenced this issue Dec 19, 2024
gh-122706: fix docs for asyncio ssl sockets (GH-128092)
(cherry picked from commit 19c5134)

Co-authored-by: Kumar Aditya <[email protected]>
kumaraditya303 added a commit that referenced this issue Dec 19, 2024
gh-122706: fix docs for asyncio ssl sockets (GH-128092)
(cherry picked from commit 19c5134)

Co-authored-by: Kumar Aditya <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-asyncio
Projects
Status: Done
Development

No branches or pull requests

5 participants