Skip to content

Data race between set_richcompare and frozenset_hash under free-threading #132213

@vfdev-5

Description

@vfdev-5

Bug report

Bug description:

We have the following TSAN report from coming JAX CI:

==================
 WARNING: ThreadSanitizer: data race (pid=40645)
   Read of size 8 at 0x7fffb6bdc1a0 by thread T6 (mutexes: read M0):
     #0 set_richcompare /__w/jax/jax/cpython/Objects/setobject.c:2144:16 (python3.14+0x2e1904) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #1 do_richcompare /__w/jax/jax/cpython/Objects/object.c:1054:15 (python3.14+0x2a644b) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #2 PyObject_RichCompare /__w/jax/jax/cpython/Objects/object.c:1103:21 (python3.14+0x2a644b)
     #3 _PyEval_EvalFrameDefault /__w/jax/jax/cpython/Python/generated_cases.c.h:4471:35 (python3.14+0x40690b) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #4 _PyEval_EvalFrame /__w/jax/jax/cpython/./Include/internal/pycore_ceval.h:119:16 (python3.14+0x3f7550) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #5 _PyEval_Vector /__w/jax/jax/cpython/Python/ceval.c:1902:12 (python3.14+0x3f7550)
     #6 _PyFunction_Vectorcall /__w/jax/jax/cpython/Objects/call.c (python3.14+0x1f1a8f) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #7 _PyObject_VectorcallTstate /__w/jax/jax/cpython/./Include/internal/pycore_call.h:169:11 (python3.14+0x2f9e4a) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
...
   Previous atomic write of size 8 at 0x7fffb6bdc1a0 by thread T8 (mutexes: read M0):
     #0 _Py_atomic_store_ssize_relaxed /__w/jax/jax/cpython/./Include/cpython/pyatomic_gcc.h:513:3 (python3.14+0x2e22b9) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #1 frozenset_hash /__w/jax/jax/cpython/Objects/setobject.c:801:5 (python3.14+0x2e22b9)
     #2 PyObject_Hash /__w/jax/jax/cpython/Objects/object.c (python3.14+0x2a6c56) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #3 tuple_hash /__w/jax/jax/cpython/Objects/tupleobject.c:321:27 (python3.14+0x2f38bf) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #4 PyObject_Hash /__w/jax/jax/cpython/Objects/object.c (python3.14+0x2a6c56) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)
     #5 builtin_hash /__w/jax/jax/cpython/Python/bltinmodule.c:1736:9 (python3.14+0x3f0d15) (BuildId: 48c7ba326d9f22d081e76424a9d52fc8476e98f5)

Probably missing atomic read here:

if (v->hash != -1 &&

CPython version: Python 3.14.0a6+ experimental free-threading build (heads/main:8680400, Apr 6 2025, 05:24:33) [Clang 18.1.3 (1ubuntu1)]

cc @kumaraditya303

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions