Title: LIST_APPEND and SET_ADD opcodes safety in Free Threading · Issue #152288 · python/cpython · GitHub
Open Graph Title: LIST_APPEND and SET_ADD opcodes safety in Free Threading · Issue #152288 · python/cpython
X Title: LIST_APPEND and SET_ADD opcodes safety in Free Threading · Issue #152288 · python/cpython
Description: Hi, While reviewing PR gh-152273, I moved _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); check to set_add_entry_takeref() to check for bugs. I discovered that SET_ADD opcode calls _PySet_AddTakeRef() without getting the set critical sect...
Open Graph Description: Hi, While reviewing PR gh-152273, I moved _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); check to set_add_entry_takeref() to check for bugs. I discovered that SET_ADD opcode calls _PySet_AddTakeRef...
X Description: Hi, While reviewing PR gh-152273, I moved _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); check to set_add_entry_takeref() to check for bugs. I discovered that SET_ADD opcode calls _PySet_AddTakeRef...
Opengraph URL: https://github.com/python/cpython/issues/152288
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"LIST_APPEND and SET_ADD opcodes safety in Free Threading","articleBody":"Hi,\n\nWhile reviewing PR gh-152273, I moved `_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);` check to `set_add_entry_takeref()` to check for bugs. I discovered that `SET_ADD` opcode calls `_PySet_AddTakeRef()` without getting the set critical section. That's surprising and IMO it would deserve adding a comment to explain why it's safe to omit locking here.\n\n`LIST_APPEND` has a similar design (also omit locking).\n\n---\n\nThe following code uses the evil `gc.get_objects()` function to call `set.add()` in a different thread while the main thread is building a set:\n\n```py\nimport dis\nimport gc\nimport threading\n\nGLOBAL_SET = None\n\ndef worker():\n EVENT.wait()\n obj = GLOBAL_SET\n for i in range(100):\n # Call set.add() which uses the critical section\n obj.add(i)\n\n\nMARKER = b\"MARKER\".decode()\nEVENT = threading.Event()\n\nclass EvilHash:\n def __init__(self, hash_value):\n self.hash_value = hash_value\n\n def __hash__(self):\n global GLOBAL_SET, EVENT\n if GLOBAL_SET is None:\n # Invoke the evil gc.get_objects()!\n for obj in gc.get_objects():\n if isinstance(obj, set) and MARKER in obj:\n GLOBAL_SET = obj\n EVENT.set()\n return self.hash_value\n\n def __repr__(self):\n return f\"EvilHash({self.hash_value})\"\n\ndef func():\n build_set = {\n # Use *list so following items are added by SET_ADD opcode\n *[MARKER],\n # Added by SET_ADD which calls _PySet_AddTakeRef()\n # without the critical section\n EvilHash(0), EvilHash(1), EvilHash(2), EvilHash(3), EvilHash(4),\n EvilHash(5)}\n\n print(build_set)\n print(\"Length:\", len(build_set))\n if len(build_set) != 107:\n raise Exception(\"race condition!\")\n\nthread = threading.Thread(target=worker)\nthread.start()\nfunc()\nthread.join()\n#dis.dis(func)\n```\n\nI expected the code to fail randomly, but so far I failed to trigger a race condition on Free Threading. Running the code on Python built with `--with-thread-sanitizer` doesn't show any warning.","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2026-06-26T14:06:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/152288/cpython/issues/152288"}
| 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:05fe0501-04cf-02be-174f-8a7e51b22b0b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8FAA:2DA286:1008D6F:15FC6D1:6A4DDAE3 |
| html-safe-nonce | 23364f27f9faf14e4d7bc40715c1068249703c12f2b945b36e31fa480148d4d9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RkFBOjJEQTI4NjoxMDA4RDZGOjE1RkM2RDE6NkE0RERBRTMiLCJ2aXNpdG9yX2lkIjoiMjg3MDAwNjYzNjI3Njk5NjgzNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f08939085816063ce384f14a438603c66e6fc281afcab4b99ad6cebda3f26c9c |
| hovercard-subject-tag | issue:4752603107 |
| 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/152288/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5d3a5d8455a139bd9c829a73458bd78ecc4f84016c12f5baa939eaa77fd4474d/python/cpython/issues/152288 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5d3a5d8455a139bd9c829a73458bd78ecc4f84016c12f5baa939eaa77fd4474d/python/cpython/issues/152288 |
| og:image:alt | Hi, While reviewing PR gh-152273, I moved _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so); check to set_add_entry_takeref() to check for bugs. I discovered that SET_ADD opcode calls _PySet_AddTakeRef... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 06b8a6144231bf3a234f1c2e9993861e07ce98a905912b114aa386c2d7e84b33 |
| 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 | 1d344bdb7547fe6bca17a59bb2b8aac3dc9532a0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width