You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Relax PyUnstable_SetImmortal to objects allocated in that thread? #150865
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:
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.)
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.
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.
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.
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.
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?
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Activity
ZeroIntensity commentedon Jun 4, 2026
Doesn't this take several years of continuously incref'ing an object?
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.
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.
sergey-miryanov commentedon Jun 4, 2026
No, the following example makes the object immortal and introduces a memory leak:
ZeroIntensity commentedon Jun 4, 2026
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:
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.
colesbury commentedon Jun 4, 2026
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.
ZeroIntensity commentedon Jun 4, 2026
Is there any realistic scenario where you would have 2^32 references to an object?
ZeroIntensity commentedon Jun 5, 2026
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?
seberg commentedon Jun 5, 2026
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.
seberg commentedon Jun 5, 2026
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.
ZeroIntensity commentedon Jun 5, 2026
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.
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.
kumaraditya303 commentedon Jun 17, 2026
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.freezealready 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.
ZeroIntensity commentedon Jun 17, 2026
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.
eendebakpt commentedon Sep 6, 2026
@ZeroIntensity For numpy in numpy/numpy#32513 there are (micro)benchmarks showing the effect on FT scaling.
ZeroIntensity commentedon Sep 6, 2026
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.
ZeroIntensity commentedon Sep 7, 2026
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.
mattip commentedon Sep 25, 2026
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 ana 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
Unstableones) rather than use work-arounds and internal APIs.prathamhole14 commentedon Sep 30, 2026
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.14tsee: numpy/numpy#32552 (comment)Using
subtype_deallocrecovers a lot of this ground (hitting160 M/sat 8 threads vs.230 M/sfor a static type) but it's not a complete fix. The ideal solution is immortal heap types, preferably exposed outsidePyUnstable_so Limited API builds can actually use it.ZeroIntensity commentedon Sep 30, 2026
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, allPy_INCREFcalls 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
_PyStackRefright now), but we're still a few years away from that.