Title: ProactorEventLoop datagram transports drop buffered datagrams on close() and never call connection_lost() · Issue #156920 · python/cpython · GitHub
Open Graph Title: ProactorEventLoop datagram transports drop buffered datagrams on close() and never call connection_lost() · Issue #156920 · python/cpython
X Title: ProactorEventLoop datagram transports drop buffered datagrams on close() and never call connection_lost() · Issue #156920 · python/cpython
Description: Bug report Bug description: Closing a datagram transport that still has datagrams queued drops them and never calls connection_lost(), so the socket is closed only by __del__, with a ResourceWarning; SelectorEventLoop flushes them and te...
Open Graph Description: Bug report Bug description: Closing a datagram transport that still has datagrams queued drops them and never calls connection_lost(), so the socket is closed only by __del__, with a ResourceWarnin...
X Description: Bug report Bug description: Closing a datagram transport that still has datagrams queued drops them and never calls connection_lost(), so the socket is closed only by __del__, with a ResourceWarnin...
Opengraph URL: https://github.com/python/cpython/issues/156920
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ProactorEventLoop datagram transports drop buffered datagrams on close() and never call connection_lost()","articleBody":"# Bug report\n\n### Bug description:\n\nClosing a datagram transport that still has datagrams queued drops them and\nnever calls `connection_lost()`, so the socket is closed only by `__del__`,\nwith a `ResourceWarning`; `SelectorEventLoop` flushes them and tears down\nnormally. `_ProactorDatagramTransport._loop_writing()` returns at\n`if self._conn_lost: return`, which `close()` has just incremented, so the\nteardown branch below it -- the one `close()` deliberately leaves to the\nwrite loop when the buffer is non-empty -- is unreachable.\n\n## Reproducer\n\n```python\nimport asyncio\nimport sys\n\n\nasync def main():\n loop = asyncio.get_running_loop()\n print(sys.version)\n print(type(loop).__name__)\n received = []\n lost = loop.create_future()\n\n class Receiver(asyncio.DatagramProtocol):\n def datagram_received(self, data, addr):\n received.append(data)\n\n class Sender(asyncio.DatagramProtocol):\n def connection_lost(self, exc):\n if not lost.done():\n lost.set_result(exc)\n\n recv_transport, _ = await loop.create_datagram_endpoint(\n Receiver, local_addr=('127.0.0.1', 0))\n addr = recv_transport.get_extra_info('sockname')\n\n transport, _ = await loop.create_datagram_endpoint(\n Sender, remote_addr=addr)\n\n for i in range(5):\n transport.sendto(b'datagram-%d' % i)\n transport.close()\n\n try:\n await asyncio.wait_for(lost, 5)\n except TimeoutError:\n print('connection_lost(): never called')\n else:\n print('connection_lost(): called')\n\n await asyncio.sleep(0.5)\n print('datagrams received:', len(received))\n print('write buffer size: ', transport.get_write_buffer_size())\n recv_transport.close()\n await asyncio.sleep(0)\n\n\nasyncio.run(main(), loop_factory=asyncio.SelectorEventLoop)\nasyncio.run(main(), loop_factory=asyncio.ProactorEventLoop)\n```\n\nOutput on Windows:\n```\nPS C:\\Users\\tagra\\projects\\cpython> py -3.14 .\\reproducer.py\n3.14.7 (tags/v3.14.7:823f032, Aug 5 2026, 10:51:32) [MSC v.1944 64 bit (AMD64)]\n_WindowsSelectorEventLoop\nconnection_lost(): called\ndatagrams received: 5\nwrite buffer size: 0\n3.14.7 (tags/v3.14.7:823f032, Aug 5 2026, 10:51:32) [MSC v.1944 64 bit (AMD64)]\nProactorEventLoop\nconnection_lost(): never called\ndatagrams received: 1\nwrite buffer size: 72\nPS C:\\Users\\tagra\\projects\\cpython> py -3.13 .\\reproducer.py\n3.13.15 (tags/v3.13.15:4061bc4, Aug 5 2026, 13:05:39) [MSC v.1944 64 bit (AMD64)]\n_WindowsSelectorEventLoop\nconnection_lost(): called\ndatagrams received: 5\nwrite buffer size: 0\n3.13.15 (tags/v3.13.15:4061bc4, Aug 5 2026, 13:05:39) [MSC v.1944 64 bit (AMD64)]\nProactorEventLoop\nconnection_lost(): never called\ndatagrams received: 1\nwrite buffer size: 72\n```\n\n## CPython versions tested on\n\nPy3.13 Py3.14\n\n## Operating systems tested on\n\nWindows\n\n### CPython versions tested on:\n\n3.13, 3.14\n\n### Operating systems tested on:\n\nWindows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156921\n* gh-157607\n* gh-157608\n* gh-157609\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/graingert","@type":"Person","name":"graingert"},"datePublished":"2026-09-04T09:15:57Z","url":"https://github.com/python/cpython/issues/156920"}
| 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:585e819e-a386-6230-16e9-8b9463f899e6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86A6:6365:3459256:45D5099:6B020371 |
| html-safe-nonce | 6c99d5eb3bf9c5ddc1ff7f6517f9fff8c505788793e1c90a5e92976b3172c371 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkE2OjYzNjU6MzQ1OTI1Njo0NUQ1MDk5OjZCMDIwMzcxIiwidmlzaXRvcl9pZCI6IjEzNDc1ODc0MDcyODQ5MjkzOTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4be57c91feb2bdffc76ab0b50291dcec8e17b9ce24f9a56847ddd54d5ce6ea5f |
| hovercard-subject-tag | issue:5346356011 |
| 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/156920/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fbc63c62364a3dfb95415c61d7724ae67c48cf655e9ebd91e65e06a3885ec825/python/cpython/issues/156920 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fbc63c62364a3dfb95415c61d7724ae67c48cf655e9ebd91e65e06a3885ec825/python/cpython/issues/156920 |
| og:image:alt | Bug report Bug description: Closing a datagram transport that still has datagrams queued drops them and never calls connection_lost(), so the socket is closed only by __del__, with a ResourceWarnin... |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width