Avoid data races in BorrowFlag #4948
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4904 (specifically the race I found here, the others have been reported upstream).
I haven't been able to see any observable impact of the data races that TSAN finds. That said, with these changes I no longer see race reports from ThreadSanitizer. Note that you need to use Python 3.14 to be able to use thread sanitizer usefully without creating a ton of suppressions, free-threaded CPython 3.13 has a number of races with fixes in 3.14 that probably won't be backported.
As soon as we can add 3.14 in CI I'll look at setting up free-threaded TSAN CI using the cpython_sanity docker image @nascheme has been working on.
I added some comments to explain the choices I made. I'm no expert in C++ atomics though so I'd appreciate some more careful eyes on this code.
I tested this by running this command in the 3.14t docker container after cloning pyo3, checking out the PR branch from #4811 and installing rustup to install a nightly toolchain and the
rust-srccomponent:I created a suppressions file for a race in CPython that's been reported upstream:
Ping @davidhewitt.