)]}'
{
  "commit": "c71c85bcc8084d1fcc526aad2019c2597f31cab1",
  "tree": "10cdbf37e1618c6843ad85f350ac97f9f74745bf",
  "parents": [
    "068e8b74031deb1c2411467a3bc735914e7d51dd"
  ],
  "author": {
    "name": "Jahongir Qurbonov",
    "email": "109198731+Jahongir-Qurbonov@users.noreply.github.com",
    "time": "Thu Nov 19 20:12:45 2026 +0500"
  },
  "committer": {
    "name": "GitHub",
    "email": "noreply@github.com",
    "time": "Thu Nov 19 16:12:45 2026 +0100"
  },
  "message": "[mypyc] Fix race in the __weakref__ getter on free-threaded builds (#22486)\n\nThe `__weakref__` getter of native classes (`CPy_GetWeakrefs`, added in\n#22387) reads the head of the object\u0027s weak reference list and increfs\nit without any synchronization. On a free-threaded build another thread\ncan drop the last reference to that weak reference object between the\ntwo steps. `weakref_dealloc` then unlinks it from the list and frees it,\nand the getter increfs freed memory.\n\nCPython had the same race in `subtype_getweakref` (the getter that\nclasses defined in Python use). It was fixed in python/cpython#150247\n(for python/cpython#149816; backported to 3.14 and 3.15, not to 3.13):\nthe getter now holds the interpreter\u0027s weak reference list lock and uses\n`_Py_TryIncref`. Both are internal APIs, and the lock is a striped mutex\nin the interpreter state, so an extension can\u0027t take the same lock.\n`PyUnstable_TryIncRef` alone isn\u0027t enough, because it relies on the\ncaller holding a lock that the deallocator also takes.\n\n### Reproducer\n\n```python\nimport threading, weakref\nfrom mypy_extensions import mypyc_attr\n\n@mypyc_attr(weakrefs\u003dTrue)\nclass C:\n    pass\n\ndef churn(o: C, n: int) -\u003e None:\n    for _ in range(n):\n        weakref.ref(o, None)\n        weakref.ref(o, lambda r: None)\n\ndef read(o: object, n: int) -\u003e None:\n    for _ in range(n):\n        getattr(o, \"__weakref__\")\n```\n\nRunning two `churn` threads and two `read` threads on one instance with\n`n \u003d 100_000`, compiled for 3.14t: on master 8 of 10 runs failed (5\nsegfaults, 3 hangs). With this PR all 10 passed, and the same script\nwith a non-native class passes on both.\n\n### Fix\n\nOn free-threaded builds, `CPy_GetWeakrefs` now calls CPython\u0027s own\ngetter. It gets the `__weakref__` descriptor of a small class created\nwith `type()` on first use and calls its `PyGetSetDef.get` function\ndirectly. `subtype_getweakref` only looks at\n`Py_TYPE(obj)-\u003etp_weaklistoffset`, so it works for native classes too.\nThat way the getter takes the right lock, and native classes behave the\nsame as classes defined in Python on every version: they are safe\nwherever CPython is, and on versions where CPython\u0027s own getter is still\nracy (3.13t, 3.14t before 3.14.9) they are no worse than interpreted\nclasses.\n\nThe descriptor is cached with a compare-and-swap rather than a mutex,\nsince creating the class can run arbitrary code through the GC, which\ncould reenter the getter. GIL builds are unchanged.\n\n### Tests\n\nI added `testConcurrentWeakrefGetter` to `run-threading.test`, with the\nsame pattern as the other concurrency tests there. On 3.14t it\nsegfaulted in 3 of 3 runs without the fix and passed in 3 of 3 runs with\nit. It also passes on regular builds, where it\u0027s only a smoke test. The\nweakref and threading run tests pass on 3.14t, and the full\n`test_run.py` passes on 3.13.",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "d0111e861db910ba8decca5555e4cb528aec8fbc",
      "old_mode": 33188,
      "old_path": "mypyc/lib-rt/misc_ops.c",
      "new_id": "9f1a519e7a4474506b396ce2a1d8cb35b4adc61f",
      "new_mode": 33188,
      "new_path": "mypyc/lib-rt/misc_ops.c"
    },
    {
      "type": "modify",
      "old_id": "7ae693ca40b7e63ea9900420bcf42a71af958e73",
      "old_mode": 33188,
      "old_path": "mypyc/test-data/run-threading.test",
      "new_id": "5b07d672dbdc448dfc81171d7aad4ec98c6ebbfd",
      "new_mode": 33188,
      "new_path": "mypyc/test-data/run-threading.test"
    }
  ]
}
