Skip to content

Inconsistent code found by static analyzers at base_events.py #132307

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

Closed
Anchels opened this issue Apr 9, 2025 · 4 comments
Closed

Inconsistent code found by static analyzers at base_events.py #132307

Anchels opened this issue Apr 9, 2025 · 4 comments
Labels
easy stdlib Python modules in the Lib dir topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@Anchels
Copy link

Anchels commented Apr 9, 2025

Greetings! I've been analyzing Cpython with Svace static analyzer. It has found a code inconsitency issue at the asyncio library in the following method:

async def connect_accepted_socket(
self, protocol_factory, sock,
*, ssl=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None):
if sock.type != socket.SOCK_STREAM:
raise ValueError(f'A Stream Socket was expected, got {sock!r}')
if ssl_handshake_timeout is not None and not ssl:
raise ValueError(
'ssl_handshake_timeout is only meaningful with ssl')
if ssl_shutdown_timeout is not None and not ssl:
raise ValueError(
'ssl_shutdown_timeout is only meaningful with ssl')
if sock is not None:
_check_ssl_socket(sock)
transport, protocol = await self._create_connection_transport(
sock, protocol_factory, ssl, '', server_side=True,
ssl_handshake_timeout=ssl_handshake_timeout,
ssl_shutdown_timeout=ssl_shutdown_timeout)
if self._debug:
# Get the socket from the transport because SSL transport closes
# the old socket and creates a new SSL socket
sock = transport.get_extra_info('socket')
logger.debug("%r handled: (%r, %r)", sock, transport, protocol)
return transport, protocol

The problem is a redundant comparison with a None value for reference sock here:

if sock is not None:
_check_ssl_socket(sock)

which has already been dereferenced before:

if sock.type != socket.SOCK_STREAM:
raise ValueError(f'A Stream Socket was expected, got {sock!r}')

According to the documentation, the sock input parameter is a preexisting object returned from the socket.accept() function. So it shouldn't be None


Proposed solution

I believe it is advisable to either move the null check for sock to the beginning of the function, or remove it. I'm not sure which of these options would be more in line with the regulations. After I get a response, I might be able to create a pull request.

Linked PRs

@ZeroIntensity ZeroIntensity added topic-asyncio stdlib Python modules in the Lib dir labels Apr 9, 2025
@github-project-automation github-project-automation bot moved this to Todo in asyncio Apr 9, 2025
@ZeroIntensity ZeroIntensity added the type-bug An unexpected behavior, bug, or error label Apr 9, 2025
@ZeroIntensity
Copy link
Member

I'm pretty sure we can just remove the sock is not None check.

@sobolevn
Copy link
Member

But, should we really? Is there any indication that something does not work? Or that this check is slow?

@ZeroIntensity
Copy link
Member

It's just dead code. That check can never fail, because if it was None, an error would have already been raised when accessing type. If it's messing with static analysis, then I don't see why it should stay.

@kumaraditya303
Copy link
Contributor

I think it is fine to remove it although it doesn't matter much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy stdlib Python modules in the Lib dir topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

4 participants