Skip to content

3.14: truthiness checks are not reliable in specialized bytecode #158401

Open

Description

@clairernovotny

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 immortal objects without incrementing their refcount, so each such reference the extension releases is a net decrement. 3.14 starts immortal objects at 3 << 30 and treats (int32_t)ob_refcnt < 0 as immortal, which leaves a margin of 2**30 (see gh-125174).

That 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 (PyAV-Org/PyAV#2420, home-assistant/core#178218). A core dump from an affected instance had _Py_TrueStruct.ob_refcnt == 0x7ffb2327, which is 2**30 + 318,681 below 0xC0000000.

On 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:

  • PyStackRef_IsTrue/IsFalse/IsNone compare the tagged bits exactly.
  • _PyStackRef_FromPyObjectNew and PyStackRef_MakeHeapSafe decide whether to tag with _Py_IsImmortal(), which reads the refcount.
  • PyStackRef_FromPyObjectSteal decides with ob_flags.

Once 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.

Reproducer. ctypes stands in for the extension's unbalanced decrefs:

import ctypes, sys

print(sys.version)

class C:
    def __init__(self):
        self.flag = True

def check_attr(o):
    if not o.flag:                      # LOAD_ATTR_INSTANCE_VALUE, POP_JUMP_IF_TRUE
        return "WRONG: o.flag is True but tested false"
    return "ok"

def get_true():
    return True

def check_return():
    if not get_true():                  # CALL_PY_EXACT_ARGS, RETURN_VALUE, POP_JUMP_IF_TRUE
        return "WRONG: get_true() returned True but tested false"
    return "ok"

o = C()
for _ in range(1000):                   # warm up so both functions get specialized
    check_attr(o), check_return()

refcnt = ctypes.c_uint32.from_address(id(True))   # low 32 bits of ob_refcnt (64-bit little-endian)
print("before: ob_refcnt = %#x" % refcnt.value, check_attr(o), check_return())
refcnt.value = 2**31 - 10**6            # 3.14: 2**30 + 10**6 raw decrements from 0xc0000000
print("after:  ob_refcnt = %#x" % refcnt.value, check_attr(o), check_return())
print("o.flag is True:", o.flag is True, "| bool(o.flag):", bool(o.flag))

Output with the official Docker images (python:3.x, Linux aarch64, default build):

3.14.7 (main, Sep 19 2026, 03:34:22) [GCC 14.2.0]
before: ob_refcnt = 0xc0000000 ok ok
after:  ob_refcnt = 0x7ff0bdc0 WRONG: o.flag is True but tested false WRONG: get_true() returned True but tested false
o.flag is True: True | bool(o.flag): True

3.15.0rc2 (main, Sep 19 2026, 03:32:36) [GCC 14.2.0]
before: ob_refcnt = 0xc0000000 ok ok
after:  ob_refcnt = 0x7ff0bdc0 WRONG: o.flag is True but tested false WRONG: get_true() returned True but tested false
o.flag is True: True | bool(o.flag): True

3.13.15 (main, Sep 19 2026, 03:38:46) [GCC 14.2.0]
before: ob_refcnt = 0xffffffff ok ok
after:  ob_refcnt = 0x7ff0bdc0 ok ok
o.flag is True: True | bool(o.flag): True

3.12.14 (main, Sep 19 2026, 03:37:20) [GCC 14.2.0]
before: ob_refcnt = 0xffffffff ok ok
after:  ob_refcnt = 0x7ff0bdc0 ok ok
o.flag is True: True | bool(o.flag): True

The 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.

The 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:

  • 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.
  • Treat statically allocated objects (_Py_STATICALLY_ALLOCATED_FLAG) as immortal whatever their refcount says.
  • Periodically reset the refcount of static immortal singletons, for example during GC, as suggested in "Immortal" objects aren't immortal and that breaks things. #125174.

CPython versions tested on:

3.12, 3.13, 3.14, 3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. picnixz commented on Sep 29, 2026

    @picnixz
    Member

    should I understand that the problem is extensions not properly managing refcounts or is there something else? I don't understand the issue exactly here. AFAIU:

    The drift itself is the extension's bug, and PyAV is fixing its build

    So that's the extension problem? or is it ours?

  2. added
    type-bug
    interpreter-core
    pending
    on Sep 29, 2026
  3. clairernovotny commented on Oct 1, 2026

    @clairernovotny
    Author

    Both, in different ways.

    The drift itself is the extension's bug: PyAV's abi3 wheels were compiled against 3.11 headers, so their inline Py_DECREF is a raw decrement, applied to references (e.g. Py_True from PyObject_RichCompare) that 3.12+ hands out without an incref. PyAV 19.0.0 builds against 3.12 headers and no longer drifts (measured: 0 vs. −12k to −34k per 4,000 av.open calls on 17.x/18.x).

    The part that may be CPython's is what happens once such drift takes a singleton below the immortality threshold. On 3.12/3.13 that is harmless, and the reproducer above prints ok ok. On 3.14/3.15, specialized code starts treating True as false (WRONG: o.flag is True but tested false), and in a real process that showed up as "impossible" assertion failures across threads, then a crash. Since the stable ABI still lets extensions built with 3.11 headers load on 3.14, the question is whether 3.14 should tolerate a de-immortalized singleton rather than silently miscompute, for example by not deriving the stackref tag from _Py_IsImmortal, or by checking ob_flags consistently.

  4. picnixz commented on Oct 1, 2026

    @picnixz
    Member

    Ah I see, ok now it's clearer. What we never want is true becoming false at some point if extensions correctly manage their references. If this is the case, then this means something is wrong.

    The problem is that 3.11 is security-only and this can't really be considered a security issue. The best we can do is possibly detecting on 3.14+ whether we are using old stable ABI or not to determine what to do with the refcount properly.

    I will let @markshannon decide whwat to do because I don't have enough insight on whether checking immortality properly could actually slow down things (I belieev it can, but by how much I don't know). In some sense, may I read this issue as "3.14 broke Stable ABI because of immortality assumptions"?

  5. removed
    pending
    on Oct 1, 2026
  6. clairernovotny commented on Oct 1, 2026

    @clairernovotny
    Author

    Yes, I think that's a fair reading, with one refinement: the extension is managing its references correctly by the rules it was compiled for.

    Under 3.11 semantics, PyObject_RichCompare returns a new reference to Py_True and the extension Py_DECREFs it, which is balanced. On 3.12+, the runtime skips the incref for immortal objects, but the 3.11-header inline Py_DECREF in the extension still does a raw --ob_refcnt. So code that is correct under the limited API it targets produces a net decrement per call, and in a long-running process that adds up. In the Home Assistant case it crossed the threshold in hours to days, depending on how much FFmpeg logged.

    PEP 683 anticipated old extensions touching immortal refcounts and relied on the margin being too large to matter. That held on 3.12/3.13 because losing immortality there is benign. On 3.14 it isn't, because the stackref fast paths derive the tag from _Py_IsImmortal() while other paths use the static flags, so True can compare unequal to itself.

    So I agree the fix belongs in 3.14+, not 3.11. A cheap option might be to make PyStackRef_IsTrue/IsFalse/IsNone (and the tagging of these static singletons) independent of the current refcount, for example by comparing the untagged pointer or by tagging statically allocated immortals from ob_flags. That avoids an immortality check on the hot incref/decref paths. Whether that is acceptable performance-wise is Mark's call.

  7. added
    3.14
    3.15
    3.16
    on Oct 1, 2026
  8. 8 remaining items

  9. da-woods commented on Nov 10, 2026

    @da-woods
    Contributor

    One thing I spot (ignoring the possibility of a bigger change):

    # if Py_LIMITED_API+0 >= 0x030a00A7

    I think that should be # if Py_LIMITED_API+0 >= 0x030a0000 (and the same in DECREF) since Py_LIMITED_API is almost always set to end with 0000 and so most people defining it for 3.10 won't get the version that uses the function. (The only other exception to this in the Python source code looks to be a single function in sliceobject.h but that's explicitly documented so shouldn't change).

  10. vstinner commented on Nov 10, 2026

    @vstinner
    Member

    I think that should be # if Py_LIMITED_API+0 >= 0x030a0000 (and the same in DECREF) since Py_LIMITED_API is almost always set to end with 0000 and so most people defining it for 3.10 won't get the version that uses the function.

    Oh, you're right! I wrote a way larger change to use _Py_PACK_VERSION() #161622 which replaces 0x030a00A7 with _Py_PACK_VERSION(3, 10) :-) It solves that issue.

  11. nthanhcongaasc commented on Nov 16, 2026

    @nthanhcongaasc

    Following up on my earlier comment: I reran the repro on PyQt6 6.12.1, the first release built against the 3.14 headers, and also checked the other abi3 wheels installed in the same environment (CPython 3.14.7, Windows x64). Numbers are the change in None's refcount per call:

    • PyQt6: fixed. PyQt6 6.12.1 and PyQt6-WebEngine 6.140.1 (now on PyPI) stay at 0; 6.11.0 and 6.12.0 were +1 per void call. So building with newer headers does fix it.
    • Still +1 per call in the latest release: opencv-python 5.0.0.93 (cp37-abi3, any cv2 function returning None), psutil 7.2.2 (cp37-abi3, Process(), Process.is_running(), Process.ppid()), curl_cffi 0.17.0 (cp310-abi3, Curl.reset()).
    • protobuf 7.36.2 / 7.37.0rc1: +1, already fixed on main (Build limited API Python wheels against Python 3.14 headers. protocolbuffers/protobuf#31089).
    • Clean: pymupdf 1.28.2, tokenizers 0.23.2, safetensors 0.8.0, cryptography 50.0.1, pycryptodome 3.23.0.

    So 6 of the 11 abi3 packages I could measure drifted None in the versions installed (True/False never moved). Old-header abi3 builds still seem common.

  12. vstinner commented on Nov 16, 2026

    @vstinner
    Member

    I understand that usually, when the limited C API version VERSION is used, the old Python version VERSION is used.

    I wrote #162120 to implemented Py_INCREF()/Py_DECREF() as function calls on all limited C API versions. So if you use Python 3.16 and target older limited C API version, you should work around this "objects de-immortalized" issue.

    This change doesn't cover all cases, but it should fix the issue if you use Python 3.16.

  13. added 2 commits that reference this issue on Nov 18, 2026
  14. changed the title [-]3.14: objects de-immortalized by old stable-ABI extensions break truthiness checks in specialized bytecode[/-] [+]3.14: truthiness checks are not reliable in specialized bytecode[/+] on Nov 18, 2026
  15. markshannon commented on Nov 18, 2026

    @markshannon
    Member

    I've edited the title to describe the problem. (First the bug description then the triaging)

    The second part of the title, "objects de-immortalized by old stable-ABI extensions" describes part of the problem, but not all of it.
    The objects in question are not de-immortalized, in the important sense that they will never deallocated: their tp_dealloc function resets their refcount to ~3 billion. What happens is that they appear mortal to the interpreter, and some of the bytecodes incorrectly rely on them always appearing immortal.

    From the OP: "Alternatively, PyStackRef_IsTrue/IsFalse/IsNone could ignore the tag bit."
    This is the correct fix, IMO.

  16. added 2 commits that reference this issue on Nov 19, 2026
  17. encukou commented on Nov 19, 2026

    @encukou
    Member

    From the OP: "Alternatively, PyStackRef_IsTrue/IsFalse/IsNone could ignore the tag bit."
    This is the correct fix, IMO.

    Thanks! I filed #162275 for this.
    With that, the reproducer above runs into assertion failures, but other than asserts I haven't found other code that doesn't degrade gracefully if an extension de-immortalizes objects.

  18. added a commit that references this issue on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.143.153.16interpreter-coretopic-C-APItype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions