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
create_datagram_endpoint() no longer compatible with CAN bus raw sockets #114887
Comments
|
Looking at the origin of the underlying regression in #4922 & #76512, it's clear that the intent was to eliminate the bit flag behaviour from the handling of the Python-level socket type information. Looking at the glibc source code for the declarations of the socket types, and Linux socket man page strongly suggests that bringing back the bitwise checks is NOT the right answer:
Actually handling For a pure Regardless of the solution chosen, it would be very helpful if a basic vcan based compatibility test could be added to the test suite to ensure the standard lib continues to support CAN in addition to TCP and UDP. That should be a separate enhancement ticket though, since it will require a bit of work to figure out the most appropriate way of doing that - vcan may not even be necessary, it may suffice to just test non-CAN raw packets between a Python client and server) (Full disclosure for other core devs: I used to work with @tjhowse and am hence familiar with the application(s) that are affected here) |
|
Silly suggestion: since the ValueError is really just meant to prevent users from making trivial mistakes, maybe create_datagram_endpoint() can just check that the passed-in socket is not a SOCK_STREAM socket? Since that test appears solid, only excluding stream sockets would still have the intended benefit of rejecting the (presumably more common) mistake of accidentally passing a stream socket as a datagram endpoint, without preventing advanced users from passing other weird socket types. I have no idea what CAN is, but ISTM that there are more likely additional types of datagram sockets than additional types of stream sockets. |
|
That makes sense to me. This also fixes the problem in our use-case. |
…endpoint() (#114893) Also improve exception message. Co-authored-by: Donghee Na <donghee.na92@gmail.com>
…agram_endpoint() (python#114893) Also improve exception message. Co-authored-by: Donghee Na <donghee.na92@gmail.com>
…ate_datagram_endpoint() (pythonGH-114893) Also improve exception message. (cherry picked from commit 94ec2b9) Co-authored-by: Travis Howse <tjhowse@gmail.com> Co-authored-by: Donghee Na <donghee.na92@gmail.com>
…ate_datagram_endpoint() (pythonGH-114893) Also improve exception message. (cherry picked from commit 94ec2b9) Co-authored-by: Travis Howse <tjhowse@gmail.com> Co-authored-by: Donghee Na <donghee.na92@gmail.com>
|
Commit was missing the magic comment to auto-close the issue. Closing since the regression is fixed. I'll file a new issue for the gap in the test suite (access restrictions on raw sockets make routine testing a bit awkward). |
…agram_endpoint() (python#114893) Also improve exception message. Co-authored-by: Donghee Na <donghee.na92@gmail.com>
…agram_endpoint() (python#114893) Also improve exception message. Co-authored-by: Donghee Na <donghee.na92@gmail.com>
Bug report
Bug description:
Problem description
We are using
create_datagram_endpoint()to set up an asyncio-managed connection to a native hardware CAN bus interface atcan0under debian linux. We are using thesocknamed argument to pass in a pre-built raw socket to the FD.This works in Python 3.5.3 but broke following a migration to 3.11.4. We found that the datagram endpoint would fail to be brought up, and the
connection_made(transport)method on theprotocol_factorywould never be called. When I rejigged our code to await the coroutine I got:I believe this has gone unnoticed due to hardware CAN bus interfaces being rare.
Relevant history
#4231
#4898
#4922
Discussion of underlying issues: https://bugs.python.org/issue32331
Core issue
A socket connected to a CAN bus has
sock.typesocket.SOCK_RAW, I.E.0b11. This fails the comparison tosocket.SOCK_DGRAM(0b01) socreate_datagram_endpoint()raises an exception because the socket's type fails the "is a datagram" check, even though it is. In 3.5.3 the check was:This worked because it was treating SOCK_DGRAM as a bitwise mask, which it kinda is, but kinda isn't.
x86_64-linux-gnu/bits/socket_type.h:
... /* Types of sockets. */ enum __socket_type { SOCK_STREAM = 1, /* Sequenced, reliable, connection-based byte streams. */ #define SOCK_STREAM SOCK_STREAM SOCK_DGRAM = 2, /* Connectionless, unreliable datagrams of fixed maximum length. */ #define SOCK_DGRAM SOCK_DGRAM SOCK_RAW = 3, /* Raw protocol interface. */ #define SOCK_RAW SOCK_RAW SOCK_RDM = 4, /* Reliably-delivered messages. */ ...I am torn on the best way to approach a fix here. I have written a PR that replaces
comparisons with
however this perpetuates the enum-vs-bitmask confusion. A more constrained change might be to change
to
but that may not catch every instance of the problem. I'd appreciate some guidance on this.
Cheers,
Travis.
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: