Title: asyncio: _ProactorBasePipeTransport._call_connection_lost leaks OSError [WinError 6] from PipeHandle.close() on Windows · Issue #149388 · python/cpython · GitHub
Open Graph Title: asyncio: _ProactorBasePipeTransport._call_connection_lost leaks OSError [WinError 6] from PipeHandle.close() on Windows · Issue #149388 · python/cpython
X Title: asyncio: _ProactorBasePipeTransport._call_connection_lost leaks OSError [WinError 6] from PipeHandle.close() on Windows · Issue #149388 · python/cpython
Description: Summary On Windows with the ProactorEventLoop, _ProactorBasePipeTransport._call_connection_lost calls self._sock.close() at Lib/asyncio/proactor_events.py:166 without guarding against OSError. PipeHandle.close() (Lib/asyncio/windows_util...
Open Graph Description: Summary On Windows with the ProactorEventLoop, _ProactorBasePipeTransport._call_connection_lost calls self._sock.close() at Lib/asyncio/proactor_events.py:166 without guarding against OSError. Pipe...
X Description: Summary On Windows with the ProactorEventLoop, _ProactorBasePipeTransport._call_connection_lost calls self._sock.close() at Lib/asyncio/proactor_events.py:166 without guarding against OSError. Pipe...
Opengraph URL: https://github.com/python/cpython/issues/149388
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"asyncio: _ProactorBasePipeTransport._call_connection_lost leaks OSError [WinError 6] from PipeHandle.close() on Windows","articleBody":"## Summary\n\nOn Windows with the `ProactorEventLoop`, `_ProactorBasePipeTransport._call_connection_lost` calls `self._sock.close()` at `Lib/asyncio/proactor_events.py:166` without guarding against `OSError`. `PipeHandle.close()` (`Lib/asyncio/windows_utils.py:105-108`) is not idempotent at the Win32 level: it calls `_winapi.CloseHandle(self._handle)` unconditionally, and if the underlying Win32 handle has already been closed by another code path between reading `self._handle is not None` and executing `CloseHandle`, the call raises `OSError: [WinError 6] The handle is invalid`. The exception escapes into the loop's default exception handler, causing noisy logs (and crashes where handlers escalate). Sentry observations show this fires in production across many users on Python 3.13.12 / 3.13.13.\n\n## Python version / platform\n\n- CPython 3.13.12 and 3.13.13, Windows 10 / 11 (x86_64)\n- `asyncio.ProactorEventLoop` (Windows default)\n\n## Related issues (for cross-reference)\n\n- gh-83413 (OPEN) — \"asyncio crashes when tearing down the proactor event loop\". The `__del__` → `__repr__` → `fileno()` ValueError variant is reproduced here as a side effect but is not the primary bug.\n- gh-92841 / PR gh-92842 — fixed a sibling `RuntimeError: Event loop is closed` from the same callback chain. That fix changed `_ProactorBasePipeTransport.__del__` to call `self._sock.close()` directly, which REMOVES the loop-scheduling guard but does NOT introduce idempotency in `PipeHandle.close()`.\n- gh-98703 / PR gh-98704 (merged 2022-10-27) — added `if self._called_connection_lost: return` guard at the top of `_call_connection_lost`. This prevents the transport from entering the function twice, but does not protect against an external concurrent close of the same `PipeHandle`.\n- gh-80248 (OPEN) — similar `subprocess terminate() \"invalid handle\"` pattern.\n\n## Minimal deterministic reproducer\n\nThis is the tightest demonstration of the underlying defect — 10 lines, no asyncio event loop, no subprocess, no threading:\n\n```python\nimport _winapi\nfrom asyncio import windows_utils\n\nr, w = windows_utils.pipe(overlapped=(False, False))\nph = windows_utils.PipeHandle(r)\n# Another code path closes the underlying Win32 handle directly,\n# without going through PipeHandle. This happens in the wild via\n# finalizer races, IOCP cleanup, or DuplicateHandle-based paths.\n_winapi.CloseHandle(ph._handle)\nph.close() # -> OSError: [WinError 6] The handle is invalid\n```\n\nOn Python 3.13.12 Windows this reliably raises:\n\n```\nTraceback (most recent call last):\n File \"repro.py\", line 8, in \u003cmodule>\n ph.close()\n File \".../asyncio/windows_utils.py\", line 107, in close\n CloseHandle(self._handle)\nOSError: [WinError 6] The handle is invalid\n```\n\n`PipeHandle.__del__` then also raises the same error on GC because `_handle` was never cleared — a direct trigger of the `ValueError: I/O operation on closed pipe` / `OSError [WinError 6]` family tracked in gh-83413.\n\n## End-to-end reproducer (asyncio path)\n\nThis demonstrates the same defect surfacing through asyncio's `_ProactorBasePipeTransport._call_connection_lost`. Triggers in ~30% of trials on Python 3.13.12 Windows.\n\n```python\n\"\"\"\nReproduce OSError [WinError 6] from PipeHandle.close() in\n_ProactorBasePipeTransport._call_connection_lost.\n\nRequires: Python 3.13, Windows, ProactorEventLoop (default).\n\"\"\"\nimport asyncio\nimport sys\nimport threading\nimport time\n\nHITS = []\n\n\ndef on_exc(loop, ctx):\n exc = ctx.get(\"exception\")\n if isinstance(exc, OSError) and getattr(exc, \"winerror\", None) == 6:\n HITS.append(ctx)\n print(f\"[HIT #{len(HITS)}] {ctx.get('message')}\")\n\n\nasync def one_trial():\n loop = asyncio.get_running_loop()\n loop.set_exception_handler(on_exc)\n\n proc = await asyncio.create_subprocess_exec(\n sys.executable, \"-c\",\n \"import sys; sys.stdout.write('x'*100); sys.stdout.flush()\",\n stdout=asyncio.subprocess.PIPE,\n stderr=asyncio.subprocess.PIPE,\n )\n\n # Second reference to the same PipeHandle that the transport wraps.\n # In production, the second reference is held elsewhere by asyncio\n # internals OR by user code that retained proc.stdout; here we\n # make it explicit for reproducibility.\n stdout_ph = proc._transport._proc.stdout\n\n stop = threading.Event()\n\n def closer():\n while not stop.is_set():\n try:\n stdout_ph.close()\n except OSError:\n pass\n time.sleep(0)\n\n t = threading.Thread(target=closer, daemon=True)\n t.start()\n try:\n await asyncio.wait_for(proc.communicate(), timeout=3)\n finally:\n stop.set()\n t.join(timeout=1)\n\n\ndef main():\n for _ in range(20):\n asyncio.run(one_trial())\n print(f\"\\n{len(HITS)} WinError 6 event(s) over 20 trials\")\n\n\nif __name__ == \"__main__\":\n main()\n```\n\nSample output (three separate runs of 20 trials each):\n\n```\n3 WinError 6 event(s) over 20 trials\n8 WinError 6 event(s) over 20 trials\n4 WinError 6 event(s) over 20 trials\n```\n\n## Root cause analysis\n\nThe race window is inside `PipeHandle.close()`:\n\n```python\n# Lib/asyncio/windows_utils.py\ndef close(self, *, CloseHandle=_winapi.CloseHandle):\n if self._handle is not None: # (A)\n CloseHandle(self._handle) # (B) releases GIL during blocking syscall\n self._handle = None # (C)\n```\n\nBetween (B) and (C), the GIL can be released (blocking Win32 call), allowing a second thread to also pass the `self._handle is not None` check at (A) and advance to its own (B). The second `CloseHandle` operates on a handle value that the first call has already closed, returning WinError 6.\n\nThe raised `OSError` propagates out of `_ProactorBasePipeTransport._call_connection_lost` at `Lib/asyncio/proactor_events.py:166`:\n\n```python\nself._sock.close() # ← raises OSError here\nself._sock = None # never runs\n...\nself._called_connection_lost = True # never runs\n```\n\nNeither `self._sock = None` nor `self._called_connection_lost = True` executes, because the exception escapes. The loop's exception handler then surfaces:\n\n```\nException in callback _ProactorBasePipeTransport._call_connection_lost()\nhandle: \u003cHandle _ProactorBasePipeTransport._call_connection_lost()>\nTraceback (most recent call last):\n File \"asyncio/events.py\", line 89, in _run\n File \"asyncio/proactor_events.py\", line 166, in _call_connection_lost\n File \"asyncio/windows_utils.py\", line 107, in close\n CloseHandle(self._handle)\nOSError: [WinError 6] The handle is invalid\n```\n\n## Why this surfaces in production\n\nIn production we see the event at a rate of ~20 fires across many users running a FastAPI app that spawns ffmpeg subprocesses with `stdout=PIPE` / `stderr=PIPE`, uses `proc.communicate()`, and runs the event loop in a non-main thread. Breadcrumbs consistently show back-to-back subprocess spawns (e.g., two `ffmpeg -list_devices` queries within 600ms), suggesting high-churn subprocess traffic widens the race window naturally.\n\nThe second `close()` call is not from user code in those cases — it must come from asyncio itself. The likely path is `_ProactorBasePipeTransport.__del__` (`Lib/asyncio/proactor_events.py:114-117`), which after PR gh-92842 calls `self._sock.close()` directly without synchronization with the loop's scheduled `_call_connection_lost`. If the transport is GC'd while `_call_connection_lost` is dispatched but not yet run (or is in-flight across the GIL boundary at step (B) of `PipeHandle.close()`), the finalizer races.\n\n## Proposed fix\n\nMake `PipeHandle.close()` idempotent by catching `OSError` and always clearing `_handle`:\n\n```python\n# Lib/asyncio/windows_utils.py\ndef close(self, *, CloseHandle=_winapi.CloseHandle):\n if self._handle is not None:\n try:\n CloseHandle(self._handle)\n finally:\n self._handle = None\n```\n\nAlternative: catch `OSError` in `_ProactorBasePipeTransport._call_connection_lost` around the `self._sock.close()` call, since the handle is already effectively gone and there is nothing to recover. Similar pattern to the `shutdown()`/`hasattr` guard immediately above it at lines 164-165.\n\n```python\n# Lib/asyncio/proactor_events.py\ndef _call_connection_lost(self, exc):\n if self._called_connection_lost:\n return\n try:\n self._protocol.connection_lost(exc)\n finally:\n if hasattr(self._sock, 'shutdown') and self._sock.fileno() != -1:\n self._sock.shutdown(socket.SHUT_RDWR)\n try:\n self._sock.close()\n except OSError:\n pass\n self._sock = None\n ...\n self._called_connection_lost = True\n```\n\nEither fix resolves the repro above. The first is preferable because it localizes idempotency to the type that needs it (`PipeHandle`), matching how `socket.socket.close()` is already idempotent in CPython.\n\n## Suggested regression test\n\n`Lib/test/test_asyncio/test_windows_utils.py`:\n\n```python\nimport unittest\nimport _winapi\nfrom asyncio import windows_utils\n\n\nclass PipeHandleIdempotencyTest(unittest.TestCase):\n def test_close_is_idempotent_on_stale_handle(self):\n \"\"\"PipeHandle.close() must not raise if the handle has been\n closed by another code path. Regression test for gh-NNNNNN.\n \"\"\"\n r, w = windows_utils.pipe(overlapped=(False, False))\n ph = windows_utils.PipeHandle(r)\n handle_value = ph._handle\n # Close the underlying handle directly\n _winapi.CloseHandle(handle_value)\n # PipeHandle doesn't know the handle is gone — it should not raise\n ph.close() # must not raise\n self.assertIsNone(ph._handle)\n _winapi.CloseHandle(w)\n```\n\ncc @kumaraditya303\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-149518\n* gh-149605\n* gh-149606\n* gh-149607\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/mxschmitt","@type":"Person","name":"mxschmitt"},"datePublished":"2026-05-04T23:32:37Z","url":"https://github.com/python/cpython/issues/149388"}
| 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:fba7b9cf-f2d1-52f4-349d-72eb5b8bef44 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ECE2:21BD98:2E69C30:3AE85A5:6B01F34F |
| html-safe-nonce | 90635a48d27d6cf0fe33aed09bf171e4a80c52a496b6be19839f4b8f1d4fb0bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQ0UyOjIxQkQ5ODoyRTY5QzMwOjNBRTg1QTU6NkIwMUYzNEYiLCJ2aXNpdG9yX2lkIjoiNTcyNjA4NTc4NzA1ODA0MTY3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c9e21a4607259f3ecba8bd15f487e5e42571f1b80ff532d8d2f8085944c7f92e |
| hovercard-subject-tag | issue:4380381155 |
| 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/149388/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dd589d31e7b97f0a031260e014ec0e481ff944d77b42a1a3bd5f5a9503ef0c65/python/cpython/issues/149388 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dd589d31e7b97f0a031260e014ec0e481ff944d77b42a1a3bd5f5a9503ef0c65/python/cpython/issues/149388 |
| og:image:alt | Summary On Windows with the ProactorEventLoop, _ProactorBasePipeTransport._call_connection_lost calls self._sock.close() at Lib/asyncio/proactor_events.py:166 without guarding against OSError. Pipe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mxschmitt |
| 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