Skip to content

Type slots are not thread-safe in free-threaded builds #127266

Description

@mpage

Bug report

Bug description:

Modification of type slots is protected by the global type lock, however, type slots are read non-atomically without holding the type lock. For example, in PyObject_SetItem:

cpython/Objects/abstract.c

Lines 231 to 235 in 5bb059f

if (m && m->mp_ass_subscript) {
int res = m->mp_ass_subscript(o, key, value);
assert(_Py_CheckSlotResult(o, "__setitem__", res >= 0));
return res;
}

It's not clear how we want to address this. From @colesbury in #127169 (comment):

I'd lean towards doing a stop-the-world pause when modifying the slot so that we don't need 
to change every read. I expect that to be a bit tricky since class definitions look like they're mutating 
the class.

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. Pitmbar commented on Dec 5, 2024

    @Pitmbar
  2. colesbury commented on Feb 7, 2025

    @colesbury
    Contributor

    For future reference, you can reproduce this race by running the following test with TSAN enabled:

    ./python -m test test_opcache -m test_load_attr_method_lazy_dict
    
  3. added 3 commits that reference this issue on Feb 7, 2025
  4. added 2 commits that reference this issue on Feb 26, 2025
  5. 2 remaining items

  6. added 10 commits that reference this issue on Apr 22, 2025
  7. added a commit that references this issue on May 28, 2025
  8. added a commit that references this issue on Jul 12, 2025
  9. added a commit that references this issue on Aug 4, 2025
  10. added a commit that references this issue on Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions