Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-132917: Fix data race detected by tsan #133508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged

Conversation

Yhg1s
Copy link
Member

@Yhg1s Yhg1s commented May 6, 2025

Fix data race detected by tsan (https://github.com/python/cpython/actions/runs/14857021107/job/41712717208?pr=133502): young.count can be modified by other threads even while the gcstate is locked.

This is the simplest fix to (potentially) unblock beta 1, although this particular code path seems like it could just be an atomic swap followed by an atomic add, without having the lock at all.

(https://github.com/python/cpython/actions/runs/14857021107/job/41712717208?pr=133502):
young.count can be modified by other threads even while the gcstate is
locked.

This is the simplest fix to (potentially) unblock beta 1, although this
particular code path seems like it could just be an atomic swap followed by
an atomic add, without having the lock at all.
@nascheme
Copy link
Member

nascheme commented May 6, 2025

Looks okay to me.

@Yhg1s Yhg1s enabled auto-merge (squash) May 6, 2025 11:22
@Yhg1s Yhg1s merged commit 53e6d76 into python:main May 6, 2025
42 checks passed
@nascheme
Copy link
Member

nascheme commented May 6, 2025

I think the mutex is still needed to reliably update deferred_count. E.g. this interleaving loses the count value.

T1: read and zero count, young_count = non zero
T2: read and zero count, young_count = 0
T1: read deferred_count
T2: read deferred_count
T1: write deferred_count + young_count
T2: write deferred_count + young_count

@Yhg1s
Copy link
Member Author

Yhg1s commented May 6, 2025

Well, that's why I suggested a swap and an add :)

T1: read and zero count, young_count = non zero
T2: read and zero count, young_count = 0
T1: add young_count to deferred_count
T2: add 0 to deferred_count

The two sequential operations probably aren't noticeably faster than the locking though.

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