Skip to content

Free-threading list.__getitem__ may return an item that was never in the list #162881

Backlog

Description

@dpdani

Bug report

In comparing the implementation of list_get_item_ref at

list_get_item_ref(PyListObject *op, Py_ssize_t i)
and the proposed implementation in PEP 703 at https://peps.python.org/pep-0703/#optimistically-avoiding-locking, I believe to have found a bug.

The if (ob_item != atomic_load(&a->ob_item)) check in the proposed implementation in the PEP is missing from the actual implementation.
I'd say it is a bug because IIUC it's possible that:

  1. thread A calls list_get_item_ref(L, i) and sees item == O
  2. thread B calls L.append(...) and resizes L
  3. thread C calls L[i] = P and frees O
  4. some thread allocates a new object at location O
  5. thread A increfs the new object at O, checks that the old ob_item[i] contains O, which it still does because P was stored after the resize, and returns it.

Therefore, the call to list_get_item_ref(L, i) returned an object that was possibly never contained in L.

I can't make out from the conversation in the PR why the check was removed in d5d39ec.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Activity

  1. added
    type-bug
    on Nov 30, 2026
  2. dpdani commented on Nov 30, 2026

    @dpdani
    ContributorAuthor

    I haven't actually run a test to demonstrate this.

  3. added
    interpreter-core
    on Nov 30, 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

    interpreter-coretopic-free-threadingtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions