Skip to content

gh-114887: Perform bitwise comparisons with SOCK_DGRAM and SOCK_STEAM #114888

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
wants to merge 1 commit into from

Conversation

tjhowse
Copy link
Contributor

@tjhowse tjhowse commented Feb 2, 2024

Details in #114887

This change is technically incorrect as SOCK_STREAM and SOCK_DGRAM are not actually bitwise comparators.

@ghost
Copy link

ghost commented Feb 2, 2024

The following commit authors need to sign the Contributor License Agreement:

Click the button to sign:
CLA not signed

@bedevere-app
Copy link

bedevere-app bot commented Feb 2, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@@ -1340,7 +1340,7 @@ async def create_datagram_endpoint(self, protocol_factory,
allow_broadcast=None, sock=None):
"""Create datagram connection."""
if sock is not None:
if sock.type != socket.SOCK_DGRAM:
if not sock.type & socket.SOCK_DGRAM:
raise ValueError(
f'A UDP Socket was expected, got {sock!r}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message should also change.

@gvanrossum
Copy link
Member

gvanrossum commented Feb 2, 2024

I need help with this. I don’t know anything about this part of the socket API — is this assumption universally true?

@tjhowse
Copy link
Contributor Author

tjhowse commented Feb 2, 2024

No, it's not. It appears to be true, and often IS true due a quirk of how the enum is defined, but strictly speaking SOCK_DGRAM and SOCK_STREAM are not intended to be bitmasks. Note @ncoghlan 's comments on #114887. I am closing this PR and will raise a new one with a more appropriate fix.

@tjhowse tjhowse closed this Feb 2, 2024
proto = socket.IPPROTO_TCP
elif type == socket.SOCK_DGRAM:
elif type & socket.SOCK_DGRAM:
proto = socket.IPPROTO_UDP
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuing to exclude SOCK_RAW here actually seems like the right thing to do (since raw sockets don't have IP addresses).

If omitting this part of the change doesn't fix the original bug report, then I'd suggest adding SOCK_RAW as its own branch saying something like:

elif type == socket.SOCK_RAW:
    # asyncio mishandles raw sockets if `_ipaddr_info` returns `None`,
    # so fib about the underlying protocol
    proto = socket.IPPROTO_UDP

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change isn't required to fix our specific problem. I feel fewer side effects would come from preserving the current behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants