GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-150411: fix `gc_generation.count` race by LindaSummer · Pull Request #150413 · python/cpython

Conversation

@LindaSummer

@LindaSummer LindaSummer commented May 25, 2026

Copy link
Copy Markdown
Contributor

Issue

gh-150411

Root Cause

Refer the analytics in issue gh-150411, it should be a gc_generation.count update during the cyclic object allocation triggered the local allocation count migrated to young generation. Meantime we try to read the gc_generation.count without sync caused the race.

cpython/Python/gc_free_threading.c

Lines 2017 to 2037 in c714b56

static void
record_allocation(PyThreadState *tstate)
{
struct _gc_thread_state *gc = &((_PyThreadStateImpl *)tstate)->gc;
// We buffer the allocation count to avoid the overhead of atomic
// operations for every allocation.
gc->alloc_count++;
if (gc->alloc_count >= LOCAL_ALLOC_COUNT_THRESHOLD) {
// TODO: Use Py_ssize_t for the generation count.
GCState *gcstate = &tstate->interp->gc;
_Py_atomic_add_int(&gcstate->young.count, (int)gc->alloc_count);
gc->alloc_count = 0;
if (gc_should_collect(gcstate) &&
!_Py_atomic_load_int_relaxed(&gcstate->collecting))
{
_Py_ScheduleGC(tstate);
}
}
}

I find this problem during proposing gh-150356. So they have similar reproduce way.

Proposed Changes

I added an atomic relax load guard for the gc_generation.count.
It was protected in other places expect current one.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chatgpt-codex-connector[bot] reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@LindaSummer
LindaSummer requested a review from pablogsal as a code owner May 25, 2026 16:18
@bedevere-app bedevere-app Bot mentioned this pull request May 25, 2026
Data race between record_allocation and gc_get_count_impl #150411
Closed
@bedevere-app bedevere-app Bot added the awaiting review label May 25, 2026
@pablogsal

pablogsal commented May 25, 2026

Copy link
Copy Markdown
Member

Same concerns as #150356 (comment)

CC @nascheme

LindaSummer reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303
kumaraditya303 reviewed Jun 4, 2026
View reviewed changes
Comment thread Modules/gcmodule.c Outdated
gcstate->old[0].count,
gcstate->old[1].count);
_Py_atomic_load_int_relaxed(&gcstate->young.count),
_Py_atomic_load_int_relaxed(&gcstate->old[0].count),

@kumaraditya303 kumaraditya303 Jun 4, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Does gcstate->old[0].count need to be atomic here? I think it is only ever written under stw so it is unnecessary.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

LindaSummer reacted with heart emoji
All reactions
  • ❤️ 1 reaction

@LindaSummer LindaSummer Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hi @kumaraditya303 ,

Thanks very much for your review! ❤️
Yes, I searched the usage of count and find that only the young.count is updated without STW guard.
old[generation].count are all protected by STW.
So we only need to make young.count use atomic action and old[generation].count read is safe.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
LindaSummer added 3 commits June 4, 2026 23:33
@LindaSummer
fix gc_generation.count race
c916f01
@LindaSummer
add blurb
b2e9bca
@LindaSummer
remove useless atomic
639cc24
@LindaSummer
LindaSummer force-pushed the gc_count_tsan branch from b71383e to 639cc24 Compare June 4, 2026 15:50
@LindaSummer

LindaSummer commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Hi @pablogsal and @kumaraditya303 ,

Thanks very much for your review and suggestions!

I have updated the patch with only atomic for young.count.
The old[generation].count write is protected by STW.
The young.count is a frequently updated counter and in this patch we only read its snapshot.
So I think the result is consistent since we only synced one variable.

Please correct me if I made any mistake or misunderstanding.

Wish you a good day! 🌞

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303
kumaraditya303 reviewed Jun 6, 2026
View reviewed changes
Comment thread Lib/test/test_free_threading/test_gc.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303
Update Lib/test/test_free_threading/test_gc.py
4ab7a60
kumaraditya303
kumaraditya303 approved these changes Jun 6, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Jun 6, 2026
@kumaraditya303
kumaraditya303 enabled auto-merge (squash) June 6, 2026 16:36
@kumaraditya303
kumaraditya303 merged commit 12af26d into python:main Jun 6, 2026
59 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 6, 2026
philthompson10 pushed a commit to philthompson10/cpython that referenced this pull request Jun 17, 2026
@LindaSummer @philthompson10
pythongh-150411: fix gc_generation.count race in free-threading (py…
fa2eb1e
…thon#150413)
@nascheme nascheme mentioned this pull request Jul 14, 2026
Backport free-threading memory-safety fixes to 3.14 and 3.15 #153714
Closed
@nascheme nascheme added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Jul 16, 2026
@miss-islington-app

miss-islington-app Bot commented Jul 16, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Jul 16, 2026

Copy link
Copy Markdown

Thanks @LindaSummer for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nascheme nascheme added the topic-free-threading label Jul 16, 2026
@bedevere-app

bedevere-app Bot commented Jul 16, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jul 16, 2026
@bedevere-app

bedevere-app Bot commented Jul 16, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Jul 16, 2026
kumaraditya303 pushed a commit that referenced this pull request Jul 16, 2026
@miss-islington @LindaSummer
[3.15] gh-150411: fix gc_generation.count race in free-threading (G…
6d6f526
…H-150413) (#153788)

gh-150411: fix `gc_generation.count` race in free-threading (GH-150413)
(cherry picked from commit 12af26d)

Co-authored-by: Edward Xu <[email protected]>
nascheme pushed a commit that referenced this pull request Jul 17, 2026
@miss-islington @LindaSummer
[3.14] gh-150411: fix gc_generation.count race in free-threading (G…
46dbee9
…H-150413) (GH-153787)

(cherry picked from commit 12af26d)

Co-authored-by: Edward Xu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes
@pablogsal pablogsal Awaiting requested review from pablogsal pablogsal is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-free-threading

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@LindaSummer @pablogsal @kumaraditya303 @nascheme
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.