Title: gh-150411: fix `gc_generation.count` race by LindaSummer · Pull Request #150413 · python/cpython · GitHub
Open Graph Title: gh-150411: fix `gc_generation.count` race by LindaSummer · Pull Request #150413 · python/cpython
X Title: gh-150411: fix `gc_generation.count` race by LindaSummer · Pull Request #150413 · python/cpython
Description: 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.
Open Graph Description: 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 you...
X Description: 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 you...
Opengraph URL: https://github.com/python/cpython/pull/150413
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:4888ce50-de50-5a8e-e052-33849a6bb38c |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C024:71E5A:643B58:86AD40:6A580C20 |
| html-safe-nonce | ecc9aae1d33955e4ae25c3193251bd15a41897df56bf5022193abe0ff97dcf34 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMDI0OjcxRTVBOjY0M0I1ODo4NkFENDA6NkE1ODBDMjAiLCJ2aXNpdG9yX2lkIjoiNDQ5ODE1Mzk0NTYzMDc3MjI1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3b0373ebf0d4df360dbedeb207dd1b38c29c72a2a2c134c119da288fc1ad27a9 |
| hovercard-subject-tag | pull_request:3743080137 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/150413/files |
| twitter:image | https://avatars.githubusercontent.com/u/14014471?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/14014471?s=400&v=4 |
| og:image:alt | 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 you... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 49c8c15fabcbf356d607a90ca115c13b273e42ff8b74155de050fd229a9b0121 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3fb1f684e7a833eb1b2d01d39875a2b52cb4fe9b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width