Skip to content

Relax PyUnstable_SetImmortal to objects allocated in that thread? #150865

Open

Description

@seberg

Feature or enhancement

Proposal:

Currently PyUnstable_SetImmortal requires a uniquely referenced object. I think the docs could be a bit clearer about this but that isn't the tricky part.

The tricky part is that I think it doesn't seem uncommon to create objects which are not uniquely referenced. I have two overlapping places in NumPy where I was hope to use this while making types that were previously static to heaptypes:

  1. If you have non-trivial setup code that might fail you want to immortalize at the very end. In my case I wanted to add the object to a dict (as a last step). (Sure, if I am going to immortalize it, then I can re-order adding it to the dict with a lock or so.)
  2. I am using PyType_FromMetaType() and the returned object already has 3 references when created fresh.

Basically, I get the "err on the safe side" part, but from poking it a bit and looking at #113956 it seems that the real limitation seems to be that the current thread owns the object.

Ensuring the object is created on the current thread is trivial no matter how complicated object creation itself is.

CC @ngoldbaum

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. ZeroIntensity commented on Jun 4, 2026

    @ZeroIntensity
    Member

    arbitrary objects can be made immortal if their reference count field overflows.

    Doesn't this take several years of continuously incref'ing an object?

    Yeah, I guess that PEP draft wants to share between subinterpreters so requires immutability (very hard), while immortalization just means we might effectively immortalize things kept alive?

    The PEP draft was about immortalizing mutable objects, not requiring them to be immutable. It didn't propose that any immortal object would be shareable. I wanted the implementation because I needed an immutable reference count field to create a shareable proxy object.

    if just because to me it feels pretty natural to want to immortalize objects more generally

    My point is that this is really difficult to get right for any arbitrary object, and if we were to limit it to a subset of objects, I'm not sure I could even draft a complete contract for the documentation. I'm very uncomfortable exposing a C API with such sharp edges like that.

  2. sergey-miryanov commented on Jun 4, 2026

    @sergey-miryanov
    Contributor

    Doesn't this take several years of continuously incref'ing an object?

    No, the following example makes the object immortal and introduces a memory leak:

    import sys
    from test.support import _2G
    
    c = object()
    l = [c] * (_2G - 20)
    for _ in range(30):
        l.append(c)
    
  3. ZeroIntensity commented on Jun 4, 2026

    @ZeroIntensity
    Member

    Ah, I was thinking of the 64-bit case, which is seemingly only present on the default build.

    Nonetheless, I think this section from PEP 683 applies here:

    Under reasonable conditions, it is still highly unlikely that an object be accidentally immortalized. It would have to meet these criteria:

    • targeting a non-immortal object (so not one of the high-use builtins)
    • the extension increfs without a corresponding decref (e.g. returns from a function or method)
    • no other code decrefs the object in the meantime
      Furthermore, this isn’t all that different from how such 32-bit extensions can already incref an object past 2^31 and turn the refcount negative. If that were an actual problem then we would have heard about it.

    I'm not sure accidental immortalization is enough of an issue in practice to justify the idea that any object can safely be made immortal.

  4. colesbury commented on Jun 4, 2026

    @colesbury
    Contributor

    No, that's wrong. That section is written assuming that the ~2^32 limit only applies to 32-bit platforms, but it now also applies to 64-bit platforms on both the default and free-threading builds. In the 32-bit build, it would effectively take a reference leak to trigger overflow, but that's not true for 64-bit builds, in which you can easily write a program that has more than 2^32 references to a single object.

  5. ZeroIntensity commented on Jun 4, 2026

    @ZeroIntensity
    Member

    Is there any realistic scenario where you would have 2^32 references to an object?

  6. ZeroIntensity commented on Jun 5, 2026

    @ZeroIntensity
    Member

    Ugh, the more I think about this issue, the more torn I become. I'm going to reopen this for now while I deliberate.

    On the one hand, I do think immortality has a lot of sharp edges, but on the other hand, I don't see any better ways around refcount contention in some cases. DRC does not work in C code, so sometimes immortality is quite literally the only escape hatch. I might have to swallow my fear of misuse and let practicality beat purity.

    @seberg Do you have some NumPy code that I can look at to better understand the context?

  7. seberg commented on Jun 5, 2026

    @seberg
    ContributorAuthor

    This came out of numpy/numpy#31364 where I want to make all DTypes HeapTypes (since it seems to me that is generally a good transition to do). And it just felt naturally to want to immortalize them (because they are effectively immortal); even if I can't say I know that refcounting contention will be an issue in practice.

    And of course I imagined that wanting to immortalize a somewhat less trivial object where ensuring a refcount of 1 might be hard may occur in other places.

    To me the main question was whether it is fully safe if the thread owns it (even if I believe it means that the deferred refcount part in a free-threaded build may end up with a non-zero value). And I guess the overflow case runs into the same thing so that it must be.

  8. seberg commented on Jun 5, 2026

    @seberg
    ContributorAuthor

    Thought I'd open a PR just in case. That said, I am myself not really clear about possible caveats of immortalizing on either free-threaded or not (since I think the current function does both).
    Which I guess might mean we should just close it, add some more scary docs, or just be fine.

  9. ZeroIntensity commented on Jun 5, 2026

    @ZeroIntensity
    Member

    And it just felt naturally to want to immortalize them (because they are effectively immortal); even if I can't say I know that refcounting contention will be an issue in practice.

    This is what worries me -- it should very rarely feel "natural" to immortalize something, especially GC-tracked objects on the heap. Looking at that PR, I think it makes more sense to use DRC than to immortalize the type.

    And I guess the overflow case runs into the same thing so that it must be.

    I'm unconvinced that the overflow thing implies that this already works. More likely, accidental immortalization leads to a bunch of existing bugs that haven't been noticed yet, simply because nobody has 2^32 references in practice.

  10. kumaraditya303 commented on Jun 17, 2026

    @kumaraditya303
    Contributor

    I think it makes more sense to use DRC than to immortalize the type.

    Deferred reference counting only helps in reducing contention from the interpreter's stack, numpy on the other hand mostly involves C-API calls where regulars increfs are used. The only way to avoid contention from these is to make the objects immortal.

    I don't quite understand why immortalizing GC objects is a bad idea or the memory leak caused by it is problematic. gc.freeze already allows gc objects to be made effectively uncollectable and that does leak memory.

    I think we should relax the constraint to "object should be owned by the current thread", other than that I don't think there should be any other checks.

  11. ZeroIntensity commented on Jun 17, 2026

    @ZeroIntensity
    Member

    Deferred reference counting only helps in reducing contention from the interpreter's stack, numpy on the other hand mostly involves C-API calls where regulars increfs are used.

    Yes, but it's not clear to me as to whether this is actually causing a scaling bottleneck here. If there were concrete benchmarks showing that the only way to achieve good performance is to immortalize the type in question, then I'd be more receptive.

    Did you read my comments on the PR? Regardless of whether we do this or not, the constraints seem more complicated than just "owned by the current thread", at least with the proposed implementation.

  12. eendebakpt commented on Sep 6, 2026

    @eendebakpt
    Contributor

    Deferred reference counting only helps in reducing contention from the interpreter's stack, numpy on the other hand mostly involves C-API calls where regulars increfs are used.

    Yes, but it's not clear to me as to whether this is actually causing a scaling bottleneck here. If there were concrete benchmarks showing that the only way to achieve good performance is to immortalize the type in question, then I'd be more receptive.

    @ZeroIntensity For numpy in numpy/numpy#32513 there are (micro)benchmarks showing the effect on FT scaling.

  13. ZeroIntensity commented on Sep 6, 2026

    @ZeroIntensity
    Member

    I'm not opposed to relaxing the API; I'm just making the case that the requirements are much more complicated than "owned by the current thread", because the interpreter has various assumptions about where things can and cannot be immortal.

  14. ZeroIntensity commented on Sep 7, 2026

    @ZeroIntensity
    Member

    I'm going to take a stab at this using my old implementation as a reference. It won't be nearly as complicated because we won't need to deallocate the immortalized object during finalization.

  15. self-assigned this
    on Sep 7, 2026
  16. mattip commented on Sep 25, 2026

    @mattip
    Contributor

    The inability to mark objects immortal has caused NumPy to use borrowed references for performance reasons. We would have been happier to use immortality to avoid borrowed references.

    This PR was forced to use CPython an a CPython internal API to mark objects immortal, again for performance reasons. In fact the PR mentions this discussion.

    We would be happier to use "blessed" APIs (preferably not Unstable ones) rather than use work-arounds and internal APIs.

  17. prathamhole14 commented on Sep 30, 2026

    @prathamhole14

    Another data point: migrating NumPy types to heap types (for the Limited API and subinterpreters) crushes free-threaded scaling. @ngoldbaum benchmarked neighborhood iterators on 3.14t see: numpy/numpy#32552 (comment)

    Using subtype_dealloc recovers a lot of this ground (hitting 160 M/s at 8 threads vs. 230 M/s for a static type) but it's not a complete fix. The ideal solution is immortal heap types, preferably exposed outside PyUnstable_ so Limited API builds can actually use it.

  18. ZeroIntensity commented on Sep 30, 2026

    @ZeroIntensity
    Member

    Yes, sorry, I've been meaning to get around to this. I think there's a very low chance that we put this outside of PyUnstable; immortality is closely tied to CPython's reference counting implementation, and we don't want to commit to that for eternity. Limited API builds already have some performance problems (for example, all Py_INCREF calls cannot be inlined). The solution is to ship both per-version wheels and a limited API wheel for the distant future where NumPy isn't maintained anymore.

    Long term, I'm still not super comfortable with people immortalizing everything that is touched by C code. The better solution would be to migrate to a new HPy-like C API layer that can encode deferred reference counting in its object type (basically what we do with _PyStackRef right now), but we're still a few years away from that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions