Skip to content

gh-153852: Fix data race in list.sort()#154572

Open
weixlu wants to merge 1 commit into
python:mainfrom
weixlu:list
Open

gh-153852: Fix data race in list.sort()#154572
weixlu wants to merge 1 commit into
python:mainfrom
weixlu:list

Conversation

@weixlu

@weixlu weixlu commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

As reported in #153852(TSAN0014), list.sort() writes each slot of the array with a plain, non-atomic store. When another thread concurrently accesses the same list , TSAN will report a data race between the in-place sort and the reader.

The fix is straightforward: publish each slot write with FT_ATOMIC_STORE_PTR_RELEASE, just like the other list mutators already do. For example, ins1() (used by list.insert) also uses FT_ATOMIC_STORE_PTR_RELEASE(items[i+1], items[i])

Testing

  • The reproducer now runs clean.
  • PASS ./python -m test test_sort test_list

Notes for reviewer:

  • On the default build (GIL-enabled), FT_ATOMIC_STORE_PTR_RELEASE expands to a plain store, so there is no change there.
  • This PR only adds a atomic store, so basic functional behavior is unchanged.

@brijkapadia

Copy link
Copy Markdown
Contributor
  1. Could you create a new issue instead of referencing the parent?
  2. Are you sure using a lot of release atomics is the best approach?

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