Title: os.closerange(0, 0) closes an independently opened file descriptor · Issue #159182 · python/cpython · GitHub
Open Graph Title: os.closerange(0, 0) closes an independently opened file descriptor · Issue #159182 · python/cpython
X Title: os.closerange(0, 0) closes an independently opened file descriptor · Issue #159182 · python/cpython
Description: Bug report Bug description On Linux with a working close_range() path, os.closerange(0, 0) closes an independently opened file descriptor, although the range is empty. The documented contract is equivalent to closing descriptors in range...
Open Graph Description: Bug report Bug description On Linux with a working close_range() path, os.closerange(0, 0) closes an independently opened file descriptor, although the range is empty. The documented contract is eq...
X Description: Bug report Bug description On Linux with a working close_range() path, os.closerange(0, 0) closes an independently opened file descriptor, although the range is empty. The documented contract is eq...
Opengraph URL: https://github.com/python/cpython/issues/159182
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"os.closerange(0, 0) closes an independently opened file descriptor","articleBody":"### Bug report\n\n### Bug description\n\nOn Linux with a working `close_range()` path, `os.closerange(0, 0)` closes an independently opened file descriptor, although the range is empty.\n\nThe [documented contract](https://docs.python.org/3.15/library/os.html#os.closerange) is equivalent to closing descriptors in `range(fd_low, fd_high)`. Therefore, `(0, 0)` should be a no-op.\n\n### Reproducer\n\nRun this with the interpreter being tested. The call happens only in a disposable child process; the parent reports the exit status without relying on the child's standard streams.\n\n```python\nimport subprocess\nimport sys\n\nchild = r'''\nimport errno\nimport os\nimport sys\n\nfd = os.open(os.devnull, os.O_RDONLY)\nif sys.argv[1] == \"empty\":\n os.closerange(0, 0)\ntry:\n os.fstat(fd)\nexcept OSError as exc:\n os._exit(42 if exc.errno == errno.EBADF else 43)\nos._exit(0)\n'''\n\nprint(sys.version)\nfor case in (\"control\", \"empty\"):\n result = subprocess.run(\n [sys.executable, \"-I\", \"-c\", child, case],\n stdin=subprocess.DEVNULL,\n stdout=subprocess.DEVNULL,\n stderr=subprocess.DEVNULL,\n close_fds=True,\n timeout=5,\n )\n print(f\"{case}: {result.returncode}\")\n```\n\nExpected: both cases return `0`.\n\nActual on the affected builds:\n\n```text\ncontrol: 0\nempty: 42\n```\n\n`42` explicitly means `os.fstat(fd)` failed with `EBADF`; `43` distinguishes other `OSError` values.\n\n### Tested versions and platform\n\nReproduced on Linux x86-64, kernel 6.18.44, glibc 2.41, with:\n\n- CPython 3.15.0 from the [actions/python-versions Linux x64 artifact](https://github.com/actions/python-versions/releases/download/3.15.0-38076878863/python-3.15.0-linux-24.04-x64.tar.gz).\n- An independent, unpatched CPython 3.15.0 source build at `d220409a13b77de7a6c30abe16ea90db01528edc`, built with GCC 14.2.0.\n\nA controlled same-source feature-toggle experiment also reproduces the distinction: with `HAVE_CLOSE_RANGE` defined, the results are `0` / `42`; with it undefined, they are `0` / `0`. Both affected translation units were recompiled and the interpreters relinked, keeping unrelated objects identical. This was an incremental A/B experiment, not two clean configure-from-scratch builds.\n\n### Likely cause\n\nThe [Python wrapper](https://github.com/python/cpython/blob/d220409a13b77de7a6c30abe16ea90db01528edc/Modules/posixmodule.c#L11805-L11811) converts the exclusive upper bound by calling `_Py_closerange(fd_low, fd_high - 1)`. For `(0, 0)`, that becomes `(0, -1)`.\n\nThe [helper](https://github.com/python/cpython/blob/d220409a13b77de7a6c30abe16ea90db01528edc/Python/fileutils.c#L3039-L3048) passes these values to `close_range()` without first rejecting an empty range. Linux [defines unsigned, inclusive endpoints](https://www.man7.org/linux/man-pages/man2/close_range.2.html), so `-1` converts to `UINT_MAX`, requesting closure of every descriptor from zero upward.\n\nAn early empty-range check before this conversion/path appears necessary. A subprocess regression test would avoid closing the test runner's descriptors.\n\nThe runtime results above are limited to these builds and this Linux environment; I have not tested older release binaries or other platforms.\n\n\n### CPython versions tested on:\n\n3.15\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159183\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/youknowone","@type":"Person","name":"youknowone"},"datePublished":"2026-10-11T17:09:48Z","url":"https://github.com/python/cpython/issues/159182"}
| 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:3dc85e2c-ca59-bd9e-a4b0-0009d24b528a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D77E:20AA01:3DC343A:4E65BB2:6B01F9E5 |
| html-safe-nonce | 8e2ffff01027bb3acecf9b0cfa372ba63f74d88902b9b9da10021641a97c3060 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzdFOjIwQUEwMTozREMzNDNBOjRFNjVCQjI6NkIwMUY5RTUiLCJ2aXNpdG9yX2lkIjoiNjk4ODU2NzM0MTgzNTYxNDY5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e4f5d37ceb2610db37a0c3e11bd023c209ee7fde105113e28b8b2fcd45119019 |
| hovercard-subject-tag | issue:5803277388 |
| 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/159182/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7281770157d48dbcc34dd7c1fc2ee57959e03370eb0c79066279f275be4b2bee/python/cpython/issues/159182 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7281770157d48dbcc34dd7c1fc2ee57959e03370eb0c79066279f275be4b2bee/python/cpython/issues/159182 |
| og:image:alt | Bug report Bug description On Linux with a working close_range() path, os.closerange(0, 0) closes an independently opened file descriptor, although the range is empty. The documented contract is eq... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | youknowone |
| 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