Skip to content

gh-116946: add Py_TPFLAGS_IMMUTABLETYPE to _random.Random - #138341

Merged
picnixz merged 3 commits into
python:mainfrom
picnixz:fix/gc/random-heap-types-116946
Sep 3, 2025
Merged

picnixz merged 3 commits into
python:mainfrom
picnixz:fix/gc/random-heap-types-116946

Conversation

@picnixz

@picnixz picnixz commented Sep 1, 2025 •

Copy link
Copy Markdown
Member

On main the following leaks:

$ ./python -X showrefcount -c 'import _random; s = _random.Random(); t = type(s); t._random = s'
[42 refs, 26 blocks]
ZeroIntensity
ZeroIntensity previously approved these changes Sep 1, 2025

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM with one idea.

Comment thread Modules/_randommodule.c Resolved
@serhiy-storchaka

Copy link
Copy Markdown
Member

Wait, why is _random.Random mutable?

@ZeroIntensity

Copy link
Copy Markdown
Member

Probably because it would break code to change that.

@serhiy-storchaka

Copy link
Copy Markdown
Member

It became mutable in 3.9. I think this is a bug.

@picnixz

picnixz commented Sep 1, 2025

Copy link
Copy Markdown
Member Author

It became mutable in 3.9. I think this is a bug.

I was also surprised but I just didn't want to break stuff here.

@kumaraditya303

Copy link
Copy Markdown
Contributor

It became mutable in 3.9. I think this is a bug.

Yes, I think adding IMMUTABLE flag was missed while conversion, it shouldn't be a mutable type.

@kumaraditya303

Copy link
Copy Markdown
Contributor

See #88074 where most of the converted types where made immutable, same should be done for random and epoll

Comment thread Modules/_randommodule.c Resolved
@picnixz
picnixz marked this pull request as draft September 1, 2025 18:57
@picnixz picnixz changed the title gh-116946: fully implement GC protocol for random.Random Sep 2, 2025
@picnixz

picnixz commented Sep 2, 2025

Copy link
Copy Markdown
Member Author

@rhettinger Does this change satisfy you? do you want this to be backported?

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@vstinner

vstinner commented Sep 2, 2025

Copy link
Copy Markdown
Member

do you want this to be backported?

Please don't backport this change. It would be surprising that a class becomes immutable in 3.x.y bugfix release.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but I would prefer a single large PR that fixes the 3.9 error by making all converted types immutable.

@picnixz
picnixz merged commit 51244ba into python:main Sep 3, 2025
@picnixz
picnixz deleted the fix/gc/random-heap-types-116946 branch September 3, 2025 07:55
lkollar pushed a commit to lkollar/cpython that referenced this pull request Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants