Title: `_interpchannels`: missing `PyErr_NoMemory()` causes `SystemErroron` OOM in `{create,close}()` · Issue #150213 · python/cpython · GitHub
Open Graph Title: `_interpchannels`: missing `PyErr_NoMemory()` causes `SystemErroron` OOM in `{create,close}()` · Issue #150213 · python/cpython
X Title: `_interpchannels`: missing `PyErr_NoMemory()` causes `SystemErroron` OOM in `{create,close}()` · Issue #150213 · python/cpython
Description: Bug report Bug description: Bug description parent issue:#146102 refs:https://gist.github.com/devdanzin/d9975354c27e57f2589acfd0a777a2f1 Several allocation failure paths in Modules/_interpchannelsmodule.c return NULL or -1 without settin...
Open Graph Description: Bug report Bug description: Bug description parent issue:#146102 refs:https://gist.github.com/devdanzin/d9975354c27e57f2589acfd0a777a2f1 Several allocation failure paths in Modules/_interpchannelsm...
X Description: Bug report Bug description: Bug description parent issue:#146102 refs:https://gist.github.com/devdanzin/d9975354c27e57f2589acfd0a777a2f1 Several allocation failure paths in Modules/_interpchannelsm...
Opengraph URL: https://github.com/python/cpython/issues/150213
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`_interpchannels`: missing `PyErr_NoMemory()` causes `SystemErroron` OOM in `{create,close}()`","articleBody":"# Bug report\n\n### Bug description:\n\n## Bug description\nparent issue:https://github.com/python/cpython/issues/146102\nrefs:https://gist.github.com/devdanzin/d9975354c27e57f2589acfd0a777a2f1\n\nSeveral allocation failure paths in `Modules/_interpchannelsmodule.c`\nreturn `NULL` or `-1` without setting an exception.\n\nThis violates CPython's C API convention that an error return should have\nan exception set. In release builds, this can surface as:\n\n```text\nSystemError: \u003cbuilt-in function ...\u003e returned NULL without setting an exception\n```\n\ninstead of the expected `MemoryError`. In debug builds, the same paths may\ntrip `assert(PyErr_Occurred())` in `handle_channel_error()`.\n\n### Affected functions\n\nThe affected allocation sites are:\n\n| Function | Triggered via |\n|---|---|\n| `_channelends_new()` | `_channels.create()` |\n| `_channel_new()` | `_channels.create()` |\n| `_channelref_new()` | `_channels.create()` |\n| `_channel_set_closing()` | `_channels.close(send=True)` on a non-empty channel |\n\nNearby helper functions in the same file already handle this correctly by\ncalling `PyErr_NoMemory()` on allocation failure, for example:\n\n| Function |\n|---|\n| `_channelitem_new()` |\n| `_channelqueue_new()` |\n| `_channelend_new()` |\n\n### Reproducer\n\n```python\nimport _testcapi\nimport _interpchannels as _channels\nfrom concurrent.interpreters import _crossinterp\n\nREPLACE = _crossinterp._UNBOUND_CONSTANT_TO_FLAG[_crossinterp.UNBOUND]\n\n# Case 1: _channels.create()\nfor n in range(1, 20):\n _testcapi.set_nomemory(n, n + 1)\n try:\n cid = _channels.create(REPLACE)\n except MemoryError:\n pass\n except SystemError as exc:\n print(f\"[create] n={n}: {type(exc).__name__}: {exc}\")\n else:\n _channels.destroy(cid)\n finally:\n _testcapi.remove_mem_hooks()\n\n# Case 2: _channels.close(send=True) on a non-empty channel\nfor n in range(1, 30):\n cid = _channels.create(REPLACE)\n _channels.send(cid, b\"spam\", blocking=False)\n _testcapi.set_nomemory(n, n + 1)\n try:\n _channels.close(cid, send=True)\n except MemoryError:\n pass\n except SystemError as exc:\n print(f\"[close] n={n}: {type(exc).__name__}: {exc}\")\n finally:\n _testcapi.remove_mem_hooks()\n try:\n _channels.close(cid, force=True)\n except Exception:\n pass\n try:\n _channels.destroy(cid)\n except Exception:\n pass\n```\n\nSample output on main, CPython 3.16.0a0, macOS, release build:\n\n```text\n[create] n=1: SystemError: \u003cbuilt-in function create\u003e returned NULL without setting an exception\n[create] n=3: SystemError: \u003cbuilt-in function create\u003e returned NULL without setting an exception\n[create] n=4: SystemError: \u003cbuilt-in function create\u003e returned NULL without setting an exception\n[close] n=1: SystemError: \u003cbuilt-in function close\u003e returned NULL without setting an exception\n```\n\n### Expected behavior\n\nAll affected allocation failure paths should raise `MemoryError`, consistent\nwith nearby helper functions and CPython's general C API error handling\ncontract.\n\n### Fix\n\nCall `PyErr_NoMemory()` before returning an error from the affected\nallocation failure paths.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-150214\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/lpyu001","@type":"Person","name":"lpyu001"},"datePublished":"2026-05-22T02:17:16.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/150213/cpython/issues/150213"}
| 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:99d99018-0122-0fc5-5b0a-35c6e1b00fba |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1CC:322E35:982E4D8:D6AAC4F:6A5110C2 |
| html-safe-nonce | 6ec30f594d48f181b1955ec7b250b1bdce00e7548872b032fbb81acca1c4cf97 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUNDOjMyMkUzNTo5ODJFNEQ4OkQ2QUFDNEY6NkE1MTEwQzIiLCJ2aXNpdG9yX2lkIjoiMjYzNzgyNTkzOTgyMDU4MTA1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e4c6cd85abbb19c6bc2fcfe8596319c7b163a190b04f21841c641c62e8ae56e9 |
| hovercard-subject-tag | issue:4499268950 |
| 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/150213/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6a7088ae471a2557733955407c65a80c54782862da0e9f98dc3fbdb5dd67453f/python/cpython/issues/150213 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6a7088ae471a2557733955407c65a80c54782862da0e9f98dc3fbdb5dd67453f/python/cpython/issues/150213 |
| og:image:alt | Bug report Bug description: Bug description parent issue:#146102 refs:https://gist.github.com/devdanzin/d9975354c27e57f2589acfd0a777a2f1 Several allocation failure paths in Modules/_interpchannelsm... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lpyu001 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b076e100febaa00374cd89239f915d00dd3187f7c68b12bc2b7688181305344d |
| 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 | f9137f6d981cbbfd780af369f80ce890b2d0d2d4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width