Skip to content

Immutability Fix including heap types breaking SCCs#83

Open
xFrednet wants to merge 1 commit intoimmutable-mainfrom
fix-heap-type-cycles
Open

Immutability Fix including heap types breaking SCCs#83
xFrednet wants to merge 1 commit intoimmutable-mainfrom
fix-heap-type-cycles

Conversation

@xFrednet
Copy link
Collaborator

This used to let the SCC RC drop to zero or even negative or trigger an assert:

from immutable import freeze,set_freezable

class Node:
    pass

Node.a = Node()
Node.b = Node()
Node.c = Node()
freeze(Node)

Credit to David for finding it!

@mjp41
Copy link
Collaborator

mjp41 commented Mar 13, 2026

Should we add a test to cover the case that needs this?

@xFrednet
Copy link
Collaborator Author

Yeah, I need to adjust the traversable but not reachable type for that. But your right

This used to trigger an assert:

```
from immutable import freeze,set_freezable

class Node:
    pass

Node.a = Node()
Node.b = Node()
Node.c = Node()
freeze(Node)
```

Credit to David for finding it!
@xFrednet xFrednet force-pushed the fix-heap-type-cycles branch from 49cbe62 to 9025a92 Compare March 13, 2026 21:11
@xFrednet
Copy link
Collaborator Author

So, this added a bunch of additional warnings to the output, but I think this is a better solution than my optimistic fix earlier.

@xFrednet xFrednet requested a review from mjp41 March 14, 2026 11:11
Comment on lines +364 to +369
// `tp_traverse` of heap types *should* include a
// `Py_VISIT(Py_TYPE(self));` since around Python 2.7 but
// there are still plenty of types that don't. LLMs currently
// also don't do this consistently. So, instead of visiting the
// type directly we throw it on to the DFS stack to check the
// correct behavior on back traversal.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried about double visiting and having an incorrect RC. In the case of using a tp_traverse, we could check if it does visit the type? If it doesn't raise a warning, and add it manually.

*
* If the type is part of an SCC we may end up with a higher SCC-RC
* since this can only account for one internal edge. But this will
* just cause a memory leak instead of crashing.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned by this. I think ultimately tp_reachable should have the correct semantics. We are adding it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants