Skip to content

TSAN race in frozenset_hash #132013

Closed
Closed

Description

@hawkinsp

Bug report

Bug description:

Frozenset's hash has code to cache the hash computation that is not thread-safe under free-threading:

if (so->hash != -1) {

TSAN report from JAX test suite under Python 3.14:

WARNING: ThreadSanitizer: data race (pid=145260)
  Read of size 8 at 0x7fffb70ee4a0 by thread T6 (mutexes: read M0):
    #0 frozenset_hash /__w/jax/jax/cpython/Objects/setobject.c:796:13 (python3.14+0x2e1d93) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
    #1 PyObject_Hash /__w/jax/jax/cpython/Objects/object.c (python3.14+0x2a6876) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
    #2 tuple_hash /__w/jax/jax/cpython/Objects/tupleobject.c:321:27 (python3.14+0x2f34bf) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
    #3 PyObject_Hash /__w/jax/jax/cpython/Objects/object.c (python3.14+0x2a6876) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
    #4 builtin_hash /__w/jax/jax/cpython/Python/bltinmodule.c:1736:9 (python3.14+0x3f0915) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
 ...

  Previous write of size 8 at 0x7fffb70ee4a0 by thread T1 (mutexes: read M0):
    #0 frozenset_hash /__w/jax/jax/cpython/Objects/setobject.c:801:14 (python3.14+0x2e1ec3) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
    #1 PyObject_Hash /__w/jax/jax/cpython/Objects/object.c (python3.14+0x2a6876) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
    #2 tuple_hash /__w/jax/jax/cpython/Objects/tupleobject.c:321:27 (python3.14+0x2f34bf) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
    #3 PyObject_Hash /__w/jax/jax/cpython/Objects/object.c (python3.14+0x2a6876) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
    #4 builtin_hash /__w/jax/jax/cpython/Python/bltinmodule.c:1736:9 (python3.14+0x3f0915) (BuildId: 36ce1663ba151019a0cc812ec2af69c6b980a390)
...

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bug
    on Apr 2, 2025
  2. added a commit that references this issue on Apr 2, 2025
  3. hawkinsp commented on Apr 2, 2025

    @hawkinsp
    ContributorAuthor

    Wow that was fast!

  4. vfdev-5 commented on Apr 7, 2025

    @vfdev-5
    Contributor

    @kumaraditya303 is it possible to backport the fix to python 3.13?

  5. added a commit that references this issue on Apr 12, 2025
  6. kumaraditya303 commented on Apr 12, 2025

    @kumaraditya303
    Contributor

    is it possible to backport the fix to python 3.13?

    Backported to 3.13 in #132447

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

topic-free-threadingtype-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions