Title: asyncio proactor udp transport stops responding after send to port that isn't listening due to ConnectionResetError from WSARecvFrom · Issue #127057 · python/cpython · GitHub
Open Graph Title: asyncio proactor udp transport stops responding after send to port that isn't listening due to ConnectionResetError from WSARecvFrom · Issue #127057 · python/cpython
X Title: asyncio proactor udp transport stops responding after send to port that isn't listening due to ConnectionResetError from WSARecvFrom · Issue #127057 · python/cpython
Description: Bug report Bug description: I face a similar issue to #91227 , although instead of the error being thrown from the GetOverlappedResult, I get a ConnectionResetError thrown directly from the WSARecvFrom call. I am not quite sure what caus...
Open Graph Description: Bug report Bug description: I face a similar issue to #91227 , although instead of the error being thrown from the GetOverlappedResult, I get a ConnectionResetError thrown directly from the WSARecv...
X Description: Bug report Bug description: I face a similar issue to #91227 , although instead of the error being thrown from the GetOverlappedResult, I get a ConnectionResetError thrown directly from the WSARecv...
Opengraph URL: https://github.com/python/cpython/issues/127057
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"asyncio proactor udp transport stops responding after send to port that isn't listening due to ConnectionResetError from WSARecvFrom","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nI face a similar issue to #91227 , although instead of the error being thrown from the `GetOverlappedResult`, I get a `ConnectionResetError` thrown directly from the `WSARecvFrom` call.\r\n\r\nI am not quite sure what causes Windows to decide to throw the error from `WSARecvFrom` rather than from the respective `GetOverlappedResult`, but it appears to have something to do with timings and/or number of times an ICMP unreachable has been sent.\r\n\r\nI haven't put a log of effort to investigate, but I am able to reproduce it 100% of the times on my machine using this code:\r\n```python\r\nimport asyncio\r\nimport time\r\nimport socket\r\n\r\n\r\nclass Protocol(asyncio.DatagramProtocol):\r\n def connection_made(self, transport):\r\n self.transport = transport\r\n\r\n def datagram_received(self, data, addr):\r\n print(f'Received {data}')\r\n time.sleep(1)\r\n self.transport.sendto(b'test', ('127.0.0.1', 51346))\r\n\r\n\r\ndef send_data():\r\n s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\r\n for i in range(100):\r\n print(f'Sending {str(i)}')\r\n s.sendto(str(i).encode(), ('127.0.0.1', 1337))\r\n time.sleep(1)\r\n s.close()\r\n\r\n\r\nasync def main():\r\n loop = asyncio.get_running_loop()\r\n transport, _ = await loop.create_datagram_endpoint(Protocol, local_addr=('127.0.0.1', 1337))\r\n await asyncio.get_running_loop().run_in_executor(None, send_data)\r\n transport.close()\r\n\r\n\r\nasyncio.run(main())\r\n```\r\nI usually get something like this output:\r\n```\r\nSending 0\r\nReceived b'0'\r\nSending 1\r\nReceived b'1'\r\nSending 2\r\nReceived b'2'\r\nSending 3\r\nSending 4\r\nSending 5\r\nSending 6\r\nSending 7\r\n```\r\n(i.e. after ~3 successful receives, the server stops receiving data).\r\n\r\nWhile debugging it, I was able to see that the call to `ov.WSARecvFrom(conn.fileno(), nbytes, flags)` in `windows_event.py` throws a `ConnectionResetError` at some point, which is caught in `_loop_writing` in `proactor_events.py`, causing it to basically stop its receiving loop.\r\n\r\nAn easy fix would be to retry the call to `ov.WSARecvFrom(conn.fileno(), nbytes, flags)` on `ConnectionResetError`. It seems to work after 2 retries, though I couldn't find a documentation that guarantees this behavior.\r\nSomething like this works for me:\r\n```diff\r\n def recvfrom(self, conn, nbytes, flags=0):\r\n self._register_with_iocp(conn)\r\n- ov = _overlapped.Overlapped(NULL)\r\n try:\r\n+ for _ in range(10):\r\n+ try:\r\n+ ov = _overlapped.Overlapped(NULL)\r\n- ov.WSARecvFrom(conn.fileno(), nbytes, flags)\r\n+ ov.WSARecvFrom(conn.fileno(), nbytes, flags)\r\n+ break\r\n+ except ConnectionResetError:\r\n+ pass\r\n except BrokenPipeError:\r\n return self._result((b'', None))\r\n\r\n return self._register(ov, conn, partial(self._finish_recvfrom,\r\n empty_result=b''))\r\n```\r\n\r\nI could open a PR with such a change, though I am not sure how it should be tested, as I wasn't able to reproduce the issue without ugly and non-deterministic sleeps.\r\n\r\n### CPython versions tested on:\r\n\r\n3.13\r\n\r\n### Operating systems tested on:\r\n\r\nWindows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156726\n* gh-156976\n* gh-156977\n* gh-156978\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/bugale","@type":"Person","name":"bugale"},"datePublished":"2024-11-20T13:09:39Z","url":"https://github.com/python/cpython/issues/127057"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:2d5d417d-a710-450e-000a-64df72423377 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8E8:380435:3568C2:437224:6B01FA5A |
| html-safe-nonce | 0bc179cb6847615e8e9fa04144baa64a91f64b029df5a683a5692d1a9467c594 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEU4OjM4MDQzNTozNTY4QzI6NDM3MjI0OjZCMDFGQTVBIiwidmlzaXRvcl9pZCI6IjQ3ODUyODI0MTI1NTE4NDk4NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 43a93c907b69a22f5248cbaf54bebb2571bd03b445038b9859e8bfb1691665e1 |
| hovercard-subject-tag | issue:2675803049 |
| 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/127057/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1a2e2ba57cbd4418531e767255d495df9906740de19e3d3a227b5b6fcd0c9814/python/cpython/issues/127057 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1a2e2ba57cbd4418531e767255d495df9906740de19e3d3a227b5b6fcd0c9814/python/cpython/issues/127057 |
| og:image:alt | Bug report Bug description: I face a similar issue to #91227 , although instead of the error being thrown from the GetOverlappedResult, I get a ConnectionResetError thrown directly from the WSARecv... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bugale |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width