Skip to content

fix possibly leaked borrow in PyRef::into_super#5281

Merged
davidhewitt merged 4 commits intoPyO3:mainfrom
davidhewitt:pyref-intosuper-2
Aug 2, 2025
Merged

fix possibly leaked borrow in PyRef::into_super#5281
davidhewitt merged 4 commits intoPyO3:mainfrom
davidhewitt:pyref-intosuper-2

Conversation

@davidhewitt
Copy link
Member

Builds on top of #5253 with a solution that I think might now be correct 🤔

Copy link
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

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

Clever solution, I also think this should work. I guess this means there is a brief moment where the borrow count is not completely accurate, but I don't think this is observable anywhere (and it would be higher, which is safe)

src/pycell.rs Outdated
Comment on lines +369 to +370
if <T::Frozen as crate::pyclass::boolean_struct::private::Boolean>::VALUE
!= <U::Frozen as crate::pyclass::boolean_struct::private::Boolean>::VALUE
Copy link
Member

Choose a reason for hiding this comment

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

I think we only need to check this when going from mutable to frozen (and not when going from frozen to mutable because that would already use Us borrow checker), so maybe !T::Value && U::Value would be a tiny optimization.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that's correct too, have adjusted accordingly 👍

@davidhewitt
Copy link
Member Author

I guess this means there is a brief moment where the borrow count is not completely accurate, but I don't think this is observable anywhere (and it would be higher, which is safe)

Agreed, I figure that having the shared borrow count be temporarily raised is not a concern, even if it's a slight inefficiency. It's such a corner case that I doubt its a problem in practice, especially concerned to the extremely subtle and confusing bug users would face if they ran into this.

@davidhewitt davidhewitt added this pull request to the merge queue Aug 2, 2025
@davidhewitt davidhewitt removed this pull request from the merge queue due to a manual request Aug 2, 2025
@davidhewitt davidhewitt changed the title fix possibly leaked borow in PyRef::into_super fix possibly leaked borrow in PyRef::into_super Aug 2, 2025
@davidhewitt davidhewitt added this pull request to the merge queue Aug 2, 2025
Merged via the queue into PyO3:main with commit 563917a Aug 2, 2025
44 checks passed
@davidhewitt davidhewitt deleted the pyref-intosuper-2 branch August 2, 2025 12:38
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