Skip to content

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

Merged
colesbury merged 2 commits into
python:mainfrom
colesbury:gh-125859-get-objects
Oct 24, 2024
Merged

gh-125859: Fix crash when gc.get_objects is called during GC#125882
colesbury merged 2 commits into
python:mainfrom
colesbury:gh-125859-get-objects

Conversation

@colesbury

@colesbury colesbury commented Oct 23, 2024

Copy link
Copy Markdown
Contributor

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.

@mpage mpage left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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.

@rruuaanng rruuaanng left a comment

Copy link
Copy Markdown
Contributor

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
@miss-islington-app

Copy link
Copy Markdown

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 <colesbury@gmail.com>
@bedevere-app

bedevere-app Bot commented Oct 24, 2024

Copy link
Copy Markdown

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 <colesbury@gmail.com>
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