Title: create_datagram_endpoint() no longer compatible with CAN bus raw sockets · Issue #114887 · python/cpython · GitHub
Open Graph Title: create_datagram_endpoint() no longer compatible with CAN bus raw sockets · Issue #114887 · python/cpython
X Title: create_datagram_endpoint() no longer compatible with CAN bus raw sockets · Issue #114887 · python/cpython
Description: 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 at can0 under debian linux. We are using the sock named argument to pa...
Open Graph Description: 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 at can0 under debian linux. We...
X Description: 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 at can0 under debian linux. We...
Opengraph URL: https://github.com/python/cpython/issues/114887
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"create_datagram_endpoint() no longer compatible with CAN bus raw sockets ","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\n# Problem description\r\n\r\nWe are using `create_datagram_endpoint()` to set up an asyncio-managed connection to a native hardware CAN bus interface at `can0` under debian linux. We are using the `sock` named argument to pass in a pre-built raw socket to the FD.\r\n\r\nThis 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 the `protocol_factory` would never be called. When I rejigged our code to await the coroutine I got:\r\n\r\n```\r\n ValueError: A UDP Socket was expected, got \u003csocket.socket fd=7, family=29, type=3, proto=1, laddr=('can0',)\r\n```\r\n\r\nI believe this has gone unnoticed due to hardware CAN bus interfaces being rare.\r\n\r\n# Relevant history\r\n\r\nhttps://github.com/python/cpython/pull/4231\r\nhttps://github.com/python/cpython/pull/4898\r\nhttps://github.com/python/cpython/pull/4922\r\n\r\nDiscussion of underlying issues: https://bugs.python.org/issue32331\r\n\r\n# Core issue\r\n\r\nA socket connected to a CAN bus has `sock.type` `socket.SOCK_RAW`, I.E. `0b11`. This fails the comparison to `socket.SOCK_DGRAM` (`0b01`) so `create_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:\r\n\r\n```python\r\n (sock.type \u0026 socket.SOCK_DGRAM) == socket.SOCK_DGRAM\r\n```\r\nThis worked because it was treating SOCK_DGRAM as a bitwise mask, which it kinda is, but kinda isn't.\r\n\r\nx86_64-linux-gnu/bits/socket_type.h:\r\n```c\r\n ...\r\n /* Types of sockets. */\r\n enum __socket_type\r\n {\r\n SOCK_STREAM = 1,\t\t/* Sequenced, reliable, connection-based\r\n byte streams. */\r\n #define SOCK_STREAM SOCK_STREAM\r\n SOCK_DGRAM = 2,\t\t/* Connectionless, unreliable datagrams\r\n of fixed maximum length. */\r\n #define SOCK_DGRAM SOCK_DGRAM\r\n SOCK_RAW = 3,\t\t\t/* Raw protocol interface. */\r\n #define SOCK_RAW SOCK_RAW\r\n SOCK_RDM = 4,\t\t\t/* Reliably-delivered messages. */\r\n ...\r\n```\r\n\r\nI am torn on the best way to approach a fix here. I have written a PR that replaces\r\n\r\n```python\r\n if sock.type != socket.SOCK_DGRAM:\r\n```\r\n\r\ncomparisons with\r\n\r\n```python\r\n if not sock.type \u0026 socket.SOCK_DGRAM:\r\n```\r\nhowever this perpetuates the enum-vs-bitmask confusion. A more constrained change might be to change\r\n\r\n```python\r\n if sock.type != socket.SOCK_DGRAM:\r\n```\r\n\r\nto\r\n\r\n```python\r\n if not (sock.type == socket.SOCK_DGRAM || sock.type == socket.SOCK_RAW):\r\n```\r\n\r\nbut that may not catch every instance of the problem. I'd appreciate some guidance on this.\r\n\r\nCheers,\r\nTravis.\r\n\r\n### CPython versions tested on:\r\n\r\n3.11\r\n\r\n### Operating systems tested on:\r\n\r\nLinux\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-114888\r\n* gh-114893\n* gh-114977\n* gh-114978\n* gh-114979\n* gh-114980\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/tjhowse","@type":"Person","name":"tjhowse"},"datePublished":"2024-02-02T04:02:19.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/114887/cpython/issues/114887"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:35457304-dd59-cb07-6dc6-dfd1ee07481e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E58E:3046CB:1691572:20FC181:6A4E521F |
| html-safe-nonce | 25bd5ad2dc6f1aab02578bd26ed76da70622ed86d24e05e8a38919bc5ee73778 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNThFOjMwNDZDQjoxNjkxNTcyOjIwRkMxODE6NkE0RTUyMUYiLCJ2aXNpdG9yX2lkIjoiOTQyMTczMzA2NjI2MTM0NTU5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 359acdf3975bb5ef5df02452965b56d1df72be06ba1579b3e86a0be0130e45a8 |
| hovercard-subject-tag | issue:2114002039 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/114887/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d2cf8f6b16e0ab3bfb71aac3d5ee32803345661483a2aedb1232b3f2921ad7c3/python/cpython/issues/114887 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d2cf8f6b16e0ab3bfb71aac3d5ee32803345661483a2aedb1232b3f2921ad7c3/python/cpython/issues/114887 |
| og:image:alt | 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 at can0 under debian linux. We... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tjhowse |
| hostname | github.com |
| expected-hostname | github.com |
| None | 030096ee0db095447bfe77409d33bfac127ca7128299c58deef27c52eaa1b1f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f344f1eab54fc7dfc02d0d018cda7e158c3db65c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width