Title: 3.14: truthiness checks are not reliable in specialized bytecode · Issue #158401 · python/cpython · GitHub
Open Graph Title: 3.14: truthiness checks are not reliable in specialized bytecode · Issue #158401 · python/cpython
X Title: 3.14: truthiness checks are not reliable in specialized bytecode · Issue #158401 · python/cpython
Description: Bug report Bug description: Extensions compiled against 3.11 or older headers implement Py_DECREF as an inline --ob_refcnt. That includes abi3 wheels built on 3.11, which are common. Since 3.12 the interpreter hands out references to imm...
Open Graph Description: Bug report Bug description: Extensions compiled against 3.11 or older headers implement Py_DECREF as an inline --ob_refcnt. That includes abi3 wheels built on 3.11, which are common. Since 3.12 the...
X Description: Bug report Bug description: Extensions compiled against 3.11 or older headers implement Py_DECREF as an inline --ob_refcnt. That includes abi3 wheels built on 3.11, which are common. Since 3.12 the...
Opengraph URL: https://github.com/python/cpython/issues/158401
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"3.14: truthiness checks are not reliable in specialized bytecode","articleBody":"# Bug report\n\n### Bug description:\n\nExtensions compiled against 3.11 or older headers implement `Py_DECREF` as an inline `--ob_refcnt`. That includes abi3 wheels built on 3.11, which are common. Since 3.12 the interpreter hands out references to immortal objects without incrementing their refcount, so each such reference the extension releases is a net decrement. 3.14 starts immortal objects at `3 \u003c\u003c 30` and treats `(int32_t)ob_refcnt \u003c 0` as immortal, which leaves a margin of 2**30 (see gh-125174).\n\nThat margin runs out in real long-running processes. Home Assistant on 3.14.6 crashes after hours to days because PyAV's `cp311-abi3` wheels drain `True` by a few counts per `av.open()` and per FFmpeg log line (https://github.com/PyAV-Org/PyAV/issues/2420, https://github.com/home-assistant/core/issues/178218). A core dump from an affected instance had `_Py_TrueStruct.ob_refcnt == 0x7ffb2327`, which is 2**30 + 318,681 below `0xC0000000`.\n\nOn 3.12 and 3.13, losing immortality is fairly benign: the object is then refcounted like any other, and results stay correct. On 3.14 it is a silent correctness failure. The reason is in the default build's stackrefs:\n\n- `PyStackRef_IsTrue`/`IsFalse`/`IsNone` compare the tagged bits exactly.\n- `_PyStackRef_FromPyObjectNew` and `PyStackRef_MakeHeapSafe` decide whether to tag with `_Py_IsImmortal()`, which reads the refcount.\n- `PyStackRef_FromPyObjectSteal` decides with `ob_flags`.\n\nOnce `True`'s refcount is below 2**31, a `True` loaded by a specialized attribute load (`LOAD_ATTR_INSTANCE_VALUE` and others) or passed through `RETURN_VALUE` is untagged, and `POP_JUMP_IF_TRUE` treats it as false. `True` from constants, comparisons and C calls still tests true, so the failures look random. In Home Assistant, `Thread.name` asserted `Thread.__init__() not called`, `Condition.notify` raised `cannot notify on un-acquired lock`, `traceback` printed `None` as the exception type, and so on, across threads, until the process exited.\n\nReproducer. `ctypes` stands in for the extension's unbalanced decrefs:\n\n```python\nimport ctypes, sys\n\nprint(sys.version)\n\nclass C:\n def __init__(self):\n self.flag = True\n\ndef check_attr(o):\n if not o.flag: # LOAD_ATTR_INSTANCE_VALUE, POP_JUMP_IF_TRUE\n return \"WRONG: o.flag is True but tested false\"\n return \"ok\"\n\ndef get_true():\n return True\n\ndef check_return():\n if not get_true(): # CALL_PY_EXACT_ARGS, RETURN_VALUE, POP_JUMP_IF_TRUE\n return \"WRONG: get_true() returned True but tested false\"\n return \"ok\"\n\no = C()\nfor _ in range(1000): # warm up so both functions get specialized\n check_attr(o), check_return()\n\nrefcnt = ctypes.c_uint32.from_address(id(True)) # low 32 bits of ob_refcnt (64-bit little-endian)\nprint(\"before: ob_refcnt = %#x\" % refcnt.value, check_attr(o), check_return())\nrefcnt.value = 2**31 - 10**6 # 3.14: 2**30 + 10**6 raw decrements from 0xc0000000\nprint(\"after: ob_refcnt = %#x\" % refcnt.value, check_attr(o), check_return())\nprint(\"o.flag is True:\", o.flag is True, \"| bool(o.flag):\", bool(o.flag))\n```\n\nOutput with the official Docker images (`python:3.x`, Linux aarch64, default build):\n\n```\n3.14.7 (main, Sep 19 2026, 03:34:22) [GCC 14.2.0]\nbefore: ob_refcnt = 0xc0000000 ok ok\nafter: ob_refcnt = 0x7ff0bdc0 WRONG: o.flag is True but tested false WRONG: get_true() returned True but tested false\no.flag is True: True | bool(o.flag): True\n\n3.15.0rc2 (main, Sep 19 2026, 03:32:36) [GCC 14.2.0]\nbefore: ob_refcnt = 0xc0000000 ok ok\nafter: ob_refcnt = 0x7ff0bdc0 WRONG: o.flag is True but tested false WRONG: get_true() returned True but tested false\no.flag is True: True | bool(o.flag): True\n\n3.13.15 (main, Sep 19 2026, 03:38:46) [GCC 14.2.0]\nbefore: ob_refcnt = 0xffffffff ok ok\nafter: ob_refcnt = 0x7ff0bdc0 ok ok\no.flag is True: True | bool(o.flag): True\n\n3.12.14 (main, Sep 19 2026, 03:37:20) [GCC 14.2.0]\nbefore: ob_refcnt = 0xffffffff ok ok\nafter: ob_refcnt = 0x7ff0bdc0 ok ok\no.flag is True: True | bool(o.flag): True\n```\n\nThe value has to be far enough below 2**31. At exactly `0x7fffffff`, the first mortal-path incref brings `True` back to `0x80000000`, and it is immortal again.\n\nThe drift itself is the extension's bug, and PyAV is fixing its build. But 3.14 turns it from a lost optimization into wrong branches, and nothing in the process points at the cause. Some options, in no particular order:\n\n- Tag consistently. `_PyStackRef_FromPyObjectNew` and `PyStackRef_MakeHeapSafe` could use `ob_flags & _Py_IMMORTAL_FLAGS` as `PyStackRef_FromPyObjectSteal` does. Alternatively, `PyStackRef_IsTrue`/`IsFalse`/`IsNone` could ignore the tag bit. Either would fix the truth tests, though not the drift.\n- Treat statically allocated objects (`_Py_STATICALLY_ALLOCATED_FLAG`) as immortal whatever their refcount says.\n- Periodically reset the refcount of static immortal singletons, for example during GC, as suggested in gh-125174.\n\n### CPython versions tested on:\n\n3.12, 3.13, 3.14, 3.15\n\n### Operating systems tested on:\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162120\n* gh-162224\n* gh-162275\n* gh-162325\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/clairernovotny","@type":"Person","name":"clairernovotny"},"datePublished":"2026-09-29T10:58:22Z","url":"https://github.com/python/cpython/issues/158401"}
| 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:bf1a91aa-2064-fa29-9a91-b825e4a8c9fb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDBA:4279A:305A2EE:4107AD0:6B01FE1E |
| html-safe-nonce | 876bf0ddcb3488b282affb8c3f495bfa57941ee3adc5f6261e14586b28f72d2a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREJBOjQyNzlBOjMwNUEyRUU6NDEwN0FEMDo2QjAxRkUxRSIsInZpc2l0b3JfaWQiOiI4ODE5MzkwNDg1Njg5OTI1MTUwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2390e7fc5fd0478116dfb32d91286f9c640f58915b197621eb7f3c3839fe411f |
| hovercard-subject-tag | issue:5629423210 |
| 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/158401/issue_layout |
| twitter:image | https://opengraph.githubassets.com/63cc9c5fdf9dbfd4f8cd760d5c85f16fbd3041bac39b497c8da5e10465beae03/python/cpython/issues/158401 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/63cc9c5fdf9dbfd4f8cd760d5c85f16fbd3041bac39b497c8da5e10465beae03/python/cpython/issues/158401 |
| og:image:alt | Bug report Bug description: Extensions compiled against 3.11 or older headers implement Py_DECREF as an inline --ob_refcnt. That includes abi3 wheels built on 3.11, which are common. Since 3.12 the... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | clairernovotny |
| 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