Skip to content

gh-125859: Fix crash when gc.get_objects is called during GC #125882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Oct 23, 2024

This fixes a crash when gc.get_objects() or gc.get_referrers() is called during a GC in the free threading build.

Switch to _PyObjectStack to avoid corrupting the struct worklist linked list maintained by the GC. Also, don't return objects that are frozen (gc.freeze()) or in the process of being collected to more closely match the behavior of the default build.

This fixes a crash when `gc.get_objects()` or `gc.get_referrers()` is
called during a GC in the free threading build. Switch to
`_PyObjectStack` to avoid corrupting the `struct worklist` linked list
maintained by the GC. Also, don't return objects that are frozen
(gc.freeze) or in the process of being collected to more closely match
the behavior of the default build.
Copy link
Contributor

@mpage mpage left a comment

Choose a reason for hiding this comment

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

LGTM

@colesbury colesbury marked this pull request as draft October 23, 2024 17:56
After the `_PyEval_StartTheWorld()` call, other threads may be running
and mutating objects. Ensure that the objects are kept alive by
incref'ing them when they're added to the `_PyObjectStack`.
@colesbury colesbury marked this pull request as ready for review October 23, 2024 19:36
@colesbury colesbury requested a review from mpage October 23, 2024 19:36
@colesbury
Copy link
Contributor Author

@mpage - would you please re-review this? The previous version had a bug where the objects found during gc_visit_heaps might get deallocated when other threads resume after the _PyEval_StartTheWorld() call.

Copy link
Contributor

@rruuaanng rruuaanng left a comment

Choose a reason for hiding this comment

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

LGTM

@colesbury colesbury merged commit e545ead into python:main Oct 24, 2024
41 checks passed
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@colesbury colesbury deleted the gh-125859-get-objects branch October 24, 2024 13:33
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 24, 2024
…ythonGH-125882)

This fixes a crash when `gc.get_objects()` or `gc.get_referrers()` is
called during a GC in the free threading build.

Switch to `_PyObjectStack` to avoid corrupting the `struct worklist`
linked list maintained by the GC. Also, don't return objects that are frozen
(`gc.freeze()`) or in the process of being collected to more closely match
the behavior of the default build.
(cherry picked from commit e545ead)

Co-authored-by: Sam Gross <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Oct 24, 2024

GH-125921 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Oct 24, 2024
colesbury added a commit that referenced this pull request Oct 24, 2024
…GH-125882) (GH-125921)

This fixes a crash when `gc.get_objects()` or `gc.get_referrers()` is
called during a GC in the free threading build.

Switch to `_PyObjectStack` to avoid corrupting the `struct worklist`
linked list maintained by the GC. Also, don't return objects that are frozen
(`gc.freeze()`) or in the process of being collected to more closely match
the behavior of the default build.
(cherry picked from commit e545ead)

Co-authored-by: Sam Gross <[email protected]>
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
…ython#125882)

This fixes a crash when `gc.get_objects()` or `gc.get_referrers()` is
called during a GC in the free threading build.

Switch to `_PyObjectStack` to avoid corrupting the `struct worklist`
linked list maintained by the GC. Also, don't return objects that are frozen
(`gc.freeze()`) or in the process of being collected to more closely match
the behavior of the default build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants