GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `list`'s rich comparison has a race condition for non-trivial `__eq__` and may return an incorrect result · Issue #148442 · python/cpython

list's rich comparison has a race condition for non-trivial __eq__ and may return an incorrect result #148442

New issue
Copy link
New issue
Copy link
Closed
Closed
list's rich comparison has a race condition for non-trivial __eq__ and may return an incorrect result#148442
Copy link
Labels
3.16new features, bugs and security fixesnew features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@KowalskiThomas
KowalskiThomas
opened on Apr 12, 2026
Issue body actions

Bug report

I found a race condition happening in the rich comparison function for list where calling a rich comparison on items, which may release the GIL, may result in the list being mutated and the wrong items being compared later on in list's rich comparison.

The current code iterates through the list's ob_item to find the index of the first non-equal objects. It then calls PyObject_RichCompareBool on the two objects (which may call user-defined Python code, which may release the GIL), then checks whether the rich comparison said the items were different.
At this point, what the items exactly were is forgotten (vitem and witem go out of scope).

Later in the rich comparison function, if the desired comparison operation is not EQ/NEQ, a new call to PyObject_RichCompare is made to know which is larger/smaller. To do this, vitem and witem are re-read from the ob_item, but ob_item may actually have changed in the meantime, meaning the return value may be incorrect.

This is in a way similar to my other issue #148259 -- the root cause is mostly the same (re-reading a previously-read value that may have changed in the meantime).

I have a branch with a proposed fix, although there may be a better way as this one requires to add a lot of refcount changes which makes it all a bit hard to read / do bookkeeping for.

Note I don't currently have a reproducer, but I may try to make one if needed.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

  • gh-148442: eliminate race condition in list rich comparison #148531

Activity

  1. KowalskiThomas
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 12, 2026
  2. picnixz
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Apr 12, 2026
  3. colesbury commented on Apr 13, 2026

    @colesbury
    colesbury
    on Apr 13, 2026
    Contributor
    More actions

    I think your proposed fix makes sense here. I just have some minor comments about code structure: I think it'll be easier to read if you lift the logic for if (!k) { ... } into that block instead of breaking out of the loop.

  4. KowalskiThomas commented on Apr 13, 2026

    @KowalskiThomas
    KowalskiThomas
    on Apr 13, 2026
    ContributorAuthor
    More actions

    Noted! I'll give it a look when I get a chance.

  5. KowalskiThomas commented on Apr 30, 2026

    @KowalskiThomas
    KowalskiThomas
    on Apr 30, 2026
    ContributorAuthor
    More actions

    @colesbury Sorry for the slight delay here, I changed what you mentioned on #148531 and should be ready for review now!

  6. serhiy-storchaka
    added
    topic-free-threading
    on Jul 3, 2026
  7. serhiy-storchaka
    added a commit that references this issue on Aug 30, 2026

    gh-148442: Eliminate race condition in list rich comparison (GH-148531)

    ec8a420
  8. serhiy-storchaka
    added
    3.16new features, bugs and security fixesnew features, bugs and security fixes
    on Aug 30, 2026
  9. serhiy-storchaka commented on Aug 30, 2026

    @serhiy-storchaka
    serhiy-storchaka
    on Aug 30, 2026
    Member
    More actions

    Thank you for your contribution, @KowalskiThomas. Merged in main only. This is too large change, and while it makes some cases more correct, it cannot fix all cases. For example, if already compared elements were mutated during comparing the following elements, we can still get wrong result.

  10. serhiy-storchaka
    closed this as completedon Aug 30, 2026
  11. clin1234
    added a commit that references this issue on Sep 12, 2026

    pythongh-148442: Eliminate race condition in list rich comparison (py…

    d2ef024
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.16new features, bugs and security fixesnew features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.