Title: free-threaded: setiter_iternext releases the set once per racing thread · Issue #160573 · python/cpython · GitHub
Open Graph Title: free-threaded: setiter_iternext releases the set once per racing thread · Issue #160573 · python/cpython
X Title: free-threaded: setiter_iternext releases the set once per racing thread · Issue #160573 · python/cpython
Description: setiter_iternext reads the set before taking the critical section and then clears it outside the critical section, so every thread that reaches the exhausted branch decrements the same set. Objects/setobject.c:1098 reads PySetObject *so ...
Open Graph Description: setiter_iternext reads the set before taking the critical section and then clears it outside the critical section, so every thread that reaches the exhausted branch decrements the same set. Objects...
X Description: setiter_iternext reads the set before taking the critical section and then clears it outside the critical section, so every thread that reaches the exhausted branch decrements the same set. Objects...
Opengraph URL: https://github.com/python/cpython/issues/160573
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"free-threaded: setiter_iternext releases the set once per racing thread","articleBody":"`setiter_iternext` reads the set before taking the critical section and then clears it outside the critical section, so every thread that reaches the exhausted branch decrements the same set.\n\n- `Objects/setobject.c:1098` reads `PySetObject *so = si->si_set;` unlocked.\n- `1100` `if (so == NULL) return NULL;` lets every racing thread through, because `si_set` is still set at that point.\n- `1113-1124` the critical section covers only the table scan.\n- `1126-1129` clears `si->si_set` and calls `Py_DECREF(so)` after `Py_END_CRITICAL_SECTION()`.\n\nEight threads that arrive together each hold a non-NULL `so` and each run one decrement, so the set loses seven references it should not.\n\nReproduction on an unmodified Py_DEBUG free-threading build (`3.16.0a1+dev free-threading build`, main at 53b0004):\n\n```python\nimport sys, threading\nN = 300000\ns = set(range(N))\nfor i in range(1, N):\n s.discard(i) # one element, 299,999 dummy entries\nkeep = [s] * 1000 # extra decrefs must not free the set\nbase = sys.getrefcount(s)\nit = iter(s); next(it)\nbarrier = threading.Barrier(8)\ndef worker():\n barrier.wait()\n try: next(it)\n except StopIteration: pass\nfor r in range(3):\n ts = [threading.Thread(target=worker) for _ in range(8)]\n [t.start() for t in ts]; [t.join() for t in ts]\n print(r, base, sys.getrefcount(s))\n```\n\nOutput:\n\n```\n0 1002 996\n1 1002 996\n2 1002 996\nPython/gc_free_threading.c:1118: validate_gc_objects: Assertion \"gc_get_refs(op) >= 0\" failed: refcount is too small\nobject address : 0x20002375e10\nobject refcount : 995\nobject type name: set\nobject repr : {0}\n\nFatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed\nPython runtime state: finalizing (tstate=0x00005f29df1515f0)\n```\n\nexit status 134.\n\nThis is a sample from one run. The final count lands at 995 or 996 depending on how many of the eight threads lose a reference (six or seven); the abort itself is stable across runs. Rounds 1 and 2 are flat because `si_set` is already NULL by then. The process aborts while finalizing, and the object it complains about is the set itself.\n\nThe large table is what makes the window wide enough to hit. The scan after the unlocked read takes long enough for all eight threads to pass the `so == NULL` check. With a one-element set the same eight-thread barrier loop ran 31,020 rounds on the same build with no hit.\n\nThis has the same shape as the dict iterator in gh-154130. #144356 covers the `setiter_len` length hint only, and I did not find a report for the `iternext` path.\n\nClearing `si->si_set` inside the critical section at 1113-1124 and decref'ing after `Py_END_CRITICAL_SECTION()` would make the release happen once. Treating `si_set` like `di_dict`, with `_Py_XGetRef` and an atomic exchange, is the other route.\n\nI have the harness that produced this (`repros/set_iter_double_release.py`, with an instrumented build that counts second releases) if it is useful.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160619\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/scrieve","@type":"Person","name":"scrieve"},"datePublished":"2026-10-24T06:29:42Z","url":"https://github.com/python/cpython/issues/160573"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:cfee8dd1-76c8-02bb-9a52-e480b594a595 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B4EC:31E99C:34F43B2:469E281:6B02025D |
| html-safe-nonce | 53a0b851118235efe8df9927191e72ad373200c5b7f57f06a1223901daf07e1d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNEVDOjMxRTk5QzozNEY0M0IyOjQ2OUUyODE6NkIwMjAyNUQiLCJ2aXNpdG9yX2lkIjoiNzI1MzU4MzkxMzY3MzY4NzY0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e7d7d96b60654dec5111760be351ec4b5fda2c37e01db040c653e675347e9a27 |
| hovercard-subject-tag | issue:6010147262 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/python/cpython/160573/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dbcacd6627f3b5594dfc1aac0ecd83c93f98ba4b47ec2eacf7a11294dfa11730/python/cpython/issues/160573 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dbcacd6627f3b5594dfc1aac0ecd83c93f98ba4b47ec2eacf7a11294dfa11730/python/cpython/issues/160573 |
| og:image:alt | setiter_iternext reads the set before taking the critical section and then clears it outside the critical section, so every thread that reaches the exhausted branch decrements the same set. Objects... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | scrieve |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width