Skip to content

Data race in _Py_slot_tp_getattr_hook #137238

@colesbury

Description

@colesbury

Bug report

In _Py_slot_tp_getattr_hook, the assignment tp->tp_getattro = _Py_slot_tp_getattro is not thread-safe. It's unlikely to cause any real problems in practice, but it currently necessitates a suppression.

cpython/Objects/typeobject.c

Lines 10581 to 10586 in 9d3b53c

getattr = _PyType_LookupRef(tp, &_Py_ID(__getattr__));
if (getattr == NULL) {
/* No __getattr__ hook: use a simpler dispatcher */
tp->tp_getattro = _Py_slot_tp_getattro;
return _Py_slot_tp_getattro(self, name);
}

I think we should disable the assignment in the free threaded build:

  1. It's not necessary for correctness
  2. It's not really important for performance because we specialize this code path anyways
  3. Even without the specialization, this doesn't seem particularly important to me. The cost of the _PyType_LookupRef isn't huge and classes that use __getattribute__ aren't super common -- they are less common than classes that use __getattr__.

I'll put up a PR for this soon.

cc @nascheme

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs 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