Skip to content

gh-157377: Don't flush the thread-local allocation count in gc.get_count - #157381

Merged
ngoldbaum merged 2 commits into
python:mainfrom
ngoldbaum:fix-gc-get-count
Sep 14, 2026
Merged

ngoldbaum merged 2 commits into
python:mainfrom
ngoldbaum:fix-gc-get-count

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

On the free-threaded build, gc.get_count() flushed the allocation counter into the global young count and reset it. Modifying the local count leads to pathological cases like in the linked issue. It could also flush negative local counts into the global count, which bypassed the clamp from gh-142048 and led to under-collection compared with not calling get_count().

With this change, get_count() returns the global count plus the local counter without modifying either. The reported value still includes the caller's buffered allocations, and scheduling behaves exactly as if get_count() had never been called.

Comment thread Modules/gcmodule.c
@ngoldbaum
ngoldbaum merged commit c215919 into python:main Sep 14, 2026
58 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @ngoldbaum for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Sep 14, 2026

Copy link
Copy Markdown

GH-157505 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 14, 2026
@bedevere-app

bedevere-app Bot commented Sep 14, 2026

Copy link
Copy Markdown

GH-157506 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 14, 2026
ngoldbaum added a commit that referenced this pull request Sep 14, 2026
….get_count (GH-157381) (#157506)

gh-157377: Don't flush the thread-local allocation count in gc.get_count (GH-157381)

* gh-157377: Don't flush the thread-local allocation count in gc.get_count()

* gh-157377: clamp gc.get_count() return value at zero
(cherry picked from commit c215919)

Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
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.

2 participants