Skip to content

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

Open
KowalskiThomas wants to merge 6 commits into
python:mainfrom
KowalskiThomas:kowalski/fix-eliminate-race-condition-in-list-rich-comparison
Open

gh-148442: eliminate race condition in list rich comparison#148531
KowalskiThomas wants to merge 6 commits into
python:mainfrom
KowalskiThomas:kowalski/fix-eliminate-race-condition-in-list-rich-comparison

Conversation

@KowalskiThomas
Copy link
Copy Markdown
Contributor

@KowalskiThomas KowalskiThomas commented Apr 13, 2026

What is this PR?

This PR is a fix for #148442, where the list's rich comparison function could return an incorrect result due a race condition in non-trivial __eq__ implementations of list elements.

It also updates test_equal_operator_modifying_operand which would previously assert two different lists were equal; it now properly asserts they are different.

@KowalskiThomas KowalskiThomas force-pushed the kowalski/fix-eliminate-race-condition-in-list-rich-comparison branch from cc583a3 to 3ddd03d Compare April 30, 2026 13:24
@KowalskiThomas KowalskiThomas marked this pull request as ready for review April 30, 2026 13:25
Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test specific to this issue, which shows why the new behavior can be better than the old one?

For example, let list1 = [x, 1] and list2 = [0, 0], where x > 0. During comparison list1 > list2, list1[0] is replaced with 0. Currently, the result will be False, even if both old and new values of list1 are larger than list2. Add also a test with replacing list2[0].

@KowalskiThomas KowalskiThomas force-pushed the kowalski/fix-eliminate-race-condition-in-list-rich-comparison branch from 3ddd03d to b4ebf34 Compare May 30, 2026 15:45
@KowalskiThomas
Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka I added two tests (and rebased on main since the branch was pretty out of date).

@serhiy-storchaka
Copy link
Copy Markdown
Member

I afraid this is too heavyweight. No need to use threads if we can trigger the issue with a single thread.

@KowalskiThomas KowalskiThomas force-pushed the kowalski/fix-eliminate-race-condition-in-list-rich-comparison branch from 41e55ba to 9668111 Compare May 31, 2026 14:18
@KowalskiThomas
Copy link
Copy Markdown
Contributor Author

Ah yes, that's a very good point...

I just removed those tests and added two new ones that are single-threaded and do about the same thing with some... peculiar comparison operators.

Also, currently the release note says...

Fix a race condition in ``list.__lt__``, ``list.__le__``, ``list.__gt__``,
and ``list.__ge__``. Previously, under concurrent list mutation, the items
being compared could be replaced with other objects before the final comparison,
causing the final comparison result to be incorrect.

... should I also update it to frame it better and make it clear that it's not necessarily concurrent mutation? Even though that would be the most "usual" case I believe.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants