Title: `_ProactorDatagramTransport` never resumes a paused protocol after a write error · Issue #156698 · python/cpython · GitHub
Open Graph Title: `_ProactorDatagramTransport` never resumes a paused protocol after a write error · Issue #156698 · python/cpython
X Title: `_ProactorDatagramTransport` never resumes a paused protocol after a write error · Issue #156698 · python/cpython
Description: Bug report Bug description: On ProactorEventLoop, if a UDP write fails synchronously or asynchronously while the write buffer is non-empty, _loop_writing() reports the error via error_received() and just returns. It doesn't re-arm the wr...
Open Graph Description: Bug report Bug description: On ProactorEventLoop, if a UDP write fails synchronously or asynchronously while the write buffer is non-empty, _loop_writing() reports the error via error_received() an...
X Description: Bug report Bug description: On ProactorEventLoop, if a UDP write fails synchronously or asynchronously while the write buffer is non-empty, _loop_writing() reports the error via error_received() an...
Opengraph URL: https://github.com/python/cpython/issues/156698
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`_ProactorDatagramTransport` never resumes a paused protocol after a write error","articleBody":"# Bug report\n\n### Bug description:\n\nOn `ProactorEventLoop`, if a UDP write fails synchronously or asynchronously while the\nwrite buffer is non-empty, `_loop_writing()` reports the error via `error_received()`\nand just returns. It doesn't re-arm the write loop and doesn't call\n`_maybe_resume_protocol()`. If the protocol was paused at that point, it stays paused\nforever anything left in `self._buffer` is stranded, and a well-behaved protocol that\nrespects `pause_writing()` will never call `sendto()` again to un-stick it. Lost wakeup.\n\n```python\n# Lib/asyncio/proactor_events.py, _ProactorDatagramTransport._loop_writing\n except OSError as exc:\n self._protocol.error_received(exc)\n except Exception as exc:\n self._fatal_error(exc, 'Fatal write error on datagram transport')\n else:\n self._write_fut.add_done_callback(self._loop_writing)\n self._maybe_resume_protocol()\n```\n\n`_SelectorDatagramTransport._sendto_ready()` has the identical structure on `OSError`, but it's\nharmless there the fd is still registered with `add_writer()`, so the loop calls it\nagain next iteration and drains the buffer anyway. The proactor has no equivalent\nre-trigger, so the same code is self-healing on selectors and terminal on IOCP.\n\n### Reproducer\n\nAttached: `proactor_datagram_write_pause.py`. Gives a deterministic write failure, and `set_write_buffer_limits(0)` makes the\npause deterministic.\n\nConfirmed on Windows, both 3.10.11 and 3.14.7:\n\n```\nfail-in-flight:\n events = ['pause_writing', 'error_received([WinError 1784] ...)']\n buffered = 6\n verdict = STRANDED\n```\n\n`pause_writing` fires, the write fails, and `resume_writing` never comes the protocol\nsits paused with data stuck in the buffer until something outside asyncio forces another\n`sendto()`.\n\nLinux (3.12.3, selector loop) is fine, as expected: the buffer never has a chance to fill\nin the first place, so `pause_writing` never fires.\n\n### Suggested fix\n\n```python\n except OSError as exc:\n self._protocol.error_received(exc)\n if self._buffer and not self._conn_lost:\n self._loop.call_soon(self._loop_writing)\n else:\n self._maybe_resume_protocol()\n```\n\n### Related\n\n- gh-91227 / gh-32011 the read-side twin of this, fixed in 3.12 by swallowing\n `ERROR_PORT_UNREACHABLE` in `IocpProactor._finish_recvfrom` (not backported to 3.11)\n- gh-88906 closed by the above\n- gh-127057 (open) `WSARecvFrom` raising `ConnectionResetError` synchronously still\n kills `_loop_reading()` the same way, on all versions\n\n### Your environment\n\n- CPython versions tested: 3.10.11, 3.14.7\n- OS: Windows 11, `ProactorEventLoop\n\nhere's the reproducer:\n```py\n\"\"\"\nDoes _ProactorDatagramTransport._loop_writing() strand a paused protocol?\n\nRun on Windows (ProactorEventLoop). See README.md for what the output means.\n\"\"\"\n\nfrom __future__ import annotations\n\nimport asyncio\nimport sys\n\nOVERSIZED = b\"\\x00\" * 70000 # guaranteed oversend error (WSAEMSGSIZE or, on some\n# builds, WinError 1784 ERROR_INVALID_USER_BUFFER); either way it fails the write\n\n\nclass Protocol(asyncio.DatagramProtocol):\n def connection_made(self, transport: asyncio.DatagramTransport) -> None:\n self.transport = transport\n self.events: list[str] = []\n self.error = asyncio.get_running_loop().create_future()\n # a high water mark of 0 makes the pause deterministic; it is what\n # anyio's UDP sockets use\n transport.set_write_buffer_limits(0)\n\n def datagram_received(self, data: bytes, addr: object) -> None:\n self.events.append(f\"datagram_received({len(data)} bytes)\")\n\n def pause_writing(self) -> None:\n self.events.append(\"pause_writing\")\n\n def resume_writing(self) -> None:\n self.events.append(\"resume_writing\")\n\n def error_received(self, exc: Exception) -> None:\n self.events.append(f\"error_received({exc})\")\n if not self.error.done():\n self.error.set_result(exc)\n\n\nasync def scenario(name: str, first: bytes, second: bytes) -> None:\n loop = asyncio.get_running_loop()\n transport, protocol = await loop.create_datagram_endpoint(\n Protocol, local_addr=(\"127.0.0.1\", 0)\n )\n addr = transport.get_extra_info(\"sockname\")\n\n # the first sendto() arms the overlapped write, so the second one has to be\n # buffered behind it, which trips pause_writing() at a high water mark of 0\n transport.sendto(first, addr)\n transport.sendto(second, addr)\n\n try:\n await asyncio.wait_for(protocol.error, 5)\n except asyncio.TimeoutError:\n protocol.events.append(\"\u003cno error reported>\")\n\n await asyncio.sleep(0.5)\n paused = \"pause_writing\" in protocol.events\n resumed = \"resume_writing\" in protocol.events\n buffered = transport.get_write_buffer_size()\n print(f\"{name}:\")\n print(f\" events = {protocol.events}\")\n print(f\" buffered = {buffered}\")\n print(f\" verdict = {'STRANDED' if paused and not resumed else 'ok'}\")\n\n # recovery probe: an application send is the only thing that can re-arm the\n # write loop, which a protocol honouring pause_writing() would never do\n if paused and not resumed:\n transport.sendto(b\"probe\", addr)\n await asyncio.sleep(0.5)\n print(f\" after an unsolicited sendto(): {protocol.events}\")\n\n transport.close()\n await asyncio.sleep(0.1)\n\n\nasync def main() -> None:\n print(sys.version)\n print(type(asyncio.get_running_loop()).__name__)\n print()\n # covers WSAEMSGSIZE being reported at completion of the in-flight write\n await scenario(\"fail-in-flight\", OVERSIZED, b\"queued\")\n # covers WSAEMSGSIZE being reported synchronously by WSASendTo, from the\n # completion callback of the preceding write\n await scenario(\"fail-from-callback\", b\"ok\", OVERSIZED)\n\n\n```\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nWindows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156718\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/graingert","@type":"Person","name":"graingert"},"datePublished":"2026-08-31T10:28:12Z","url":"https://github.com/python/cpython/issues/156698"}
| 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:d90aee83-a155-d902-ded3-87825f0ee2fd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCE8:24645:155D1A1:1C310E9:6B01FA70 |
| html-safe-nonce | 3f05f3814daea7b4cb47eba7e3581aebd4dd0d434e032e94c2b179f7a81bfde7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0U4OjI0NjQ1OjE1NUQxQTE6MUMzMTBFOTo2QjAxRkE3MCIsInZpc2l0b3JfaWQiOiIyNjQ3MzIwOTE4NTIyNzIyOTI4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 94f7f9276bc5116e39a1def13135bbebc1145b30d113b362598974a4dde39a72 |
| hovercard-subject-tag | issue:5299112555 |
| 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/156698/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7e8b8fd0d7102bb4859f1877b36c1b2953e8896d9c0100802ecbc192f948b533/python/cpython/issues/156698 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7e8b8fd0d7102bb4859f1877b36c1b2953e8896d9c0100802ecbc192f948b533/python/cpython/issues/156698 |
| og:image:alt | Bug report Bug description: On ProactorEventLoop, if a UDP write fails synchronously or asynchronously while the write buffer is non-empty, _loop_writing() reports the error via error_received() an... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | graingert |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width