Skip to content

Conversation

@kevmo314
Copy link
Contributor

@kevmo314 kevmo314 commented Nov 28, 2025

Fixes quadratically increasing GC delays in the free-threaded build. This is done by updating the local to global allocation count propagation to require the global count to be non-negative. This matches the existing behavior in non-free-threaded gc.c. Additionally, adds a flush that I found was causing allocations to be lost.

@python-cla-bot
Copy link

python-cla-bot bot commented Nov 28, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Nov 28, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@sergey-miryanov
Copy link
Contributor

cc @nascheme @colesbury

@kevmo314
Copy link
Contributor Author

kevmo314 commented Nov 28, 2025

Thinking about this solution a little more, perhaps an alternative strategy that's more aligned with the original intent would be to only update the global counter to be non-negative, allowing the local ones to be negative. I'm not sure how to do that with the atomics though.

I think I have a better intuition about the core bug though: the compounding happens because garbage collection first resets the global count to zero, then frees objects. Each freed object calls record_deallocation(), driving local counts negative, which eventually get flushed to the global counter. Thus the global counter keeps getting more and more negative.

@bedevere-app
Copy link

bedevere-app bot commented Nov 30, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Adjust allocation count handling to prevent negative values and update GC state accordingly.
@bedevere-app
Copy link

bedevere-app bot commented Dec 1, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@kevmo314 kevmo314 changed the title gh-142048: Reset allocation counts in gc thread-local buffers gh-142048: Enforce global allocation count to be non-negative Dec 1, 2025
@bedevere-app
Copy link

bedevere-app bot commented Dec 1, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Co-authored-by: Sam Gross <colesbury@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Dec 1, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@colesbury
Copy link
Contributor

Please add a NEWS blurb. You can either use the blurb-it webapp mentioned above or the blurb command. (I find the blurb command via uvx blurb to be the easiest). It should be in the "Core and Builtins" sections. I think the important thing here is that it fixes quadratic behavior in the GC for free threaded Python, not really whether the count can be negative.

@kevmo314 kevmo314 changed the title gh-142048: Enforce global allocation count to be non-negative gh-142048: Fix quadratically increasing GC delays Dec 1, 2025
@kevmo314 kevmo314 requested a review from colesbury December 1, 2025 20:43
Removed flushing of thread's local GC allocation count before thread state deletion.
@kevmo314 kevmo314 requested a review from colesbury December 1, 2025 23:40
Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

Thanks!

@colesbury colesbury added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes awaiting merge and removed awaiting merge labels Dec 1, 2025
@colesbury colesbury merged commit eb89286 into python:main Dec 2, 2025
58 checks passed
@miss-islington-app
Copy link

Thanks @kevmo314 for the PR, and @colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 2, 2025
)

The GC for the free threaded build would get slower with each collection due
to effectively double counting objects freed by the GC.
(cherry picked from commit eb89286)

Co-authored-by: Kevin Wang <kevmo314@gmail.com>
@miss-islington-app
Copy link

Sorry, @kevmo314 and @colesbury, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker eb892868b31322d7cf271bc25923e14b1f67ae38 3.13

@bedevere-app
Copy link

bedevere-app bot commented Dec 2, 2025

GH-142166 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 Dec 2, 2025
colesbury pushed a commit to colesbury/cpython that referenced this pull request Dec 2, 2025
…ngh-142051)

The GC for the free threaded build would get slower with each collection due
to effectively double counting objects freed by the GC.
(cherry picked from commit eb89286)

Co-authored-by: Kevin Wang <kevmo314@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Dec 2, 2025

GH-142167 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 Dec 2, 2025
colesbury pushed a commit that referenced this pull request Dec 2, 2025
…h-142166)

The GC for the free threaded build would get slower with each collection due
to effectively double counting objects freed by the GC.
(cherry picked from commit eb89286)

Co-authored-by: Kevin Wang <kevmo314@gmail.com>
colesbury added a commit that referenced this pull request Dec 2, 2025
…142167)

The GC for the free threaded build would get slower with each collection due
to effectively double counting objects freed by the GC.
(cherry picked from commit eb89286)

Co-authored-by: Kevin Wang <kevmo314@gmail.com>
@bedevere-bot

This comment was marked as resolved.

@colesbury
Copy link
Contributor

I think the buildbot failure is unrelated. The change only affects the free threaded build and the buildbot isn't for free threaded Python.

@bedevere-bot

This comment was marked as resolved.

@bedevere-bot

This comment was marked as resolved.

@bedevere-bot

This comment was marked as resolved.

@bedevere-bot

This comment was marked as resolved.

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.

4 participants