gh-132942: Fix races in type lookup cache#133032
Merged
nascheme merged 4 commits intopython:mainfrom Apr 28, 2025
Merged
Conversation
Two races related to the type lookup cache, when used in the free-threaded build. This caused test_opcache to sometimes fail (as well as other hard to re-produce failures).
colesbury
reviewed
Apr 28, 2025
Member
Author
|
Here is a script that triggers the crash. It can take a while, especially if running under "rr". |
|
Thanks @nascheme for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
|
Sorry, @nascheme, I could not cleanly backport this to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Two races related to the type lookup cache, when used in the free-threaded build. This caused test_opcache to sometimes fail (as well as other hard to reproduce failures).
The first problem is that
find_name_in_mro()can block on some mutex and then release critical sections. If that happens, the type version used for the cache entry can be wrong (too new). Assigning the version before doing the find fixes this issue. If it does race, you will add an entry that uses an out-of-date version.The second problem was much harder to track down. There is a hard to trigger race in
update_cache(), writing to cache, and_PyType_LookupStackRefAndVersion(), reading from cache. We use a sequence lock to avoid races. However, if the reader reads the old entry value and the new entry version, it will try to execute_Py_TryXGetStackRef()on a stale cache entry value. If that value has been deallocated,PyStackRef_XCLOSE()will crash. This could happen before because the version was written first and then new value second.The fix is simply to write the entry value first and the version after. That way, the reader always sees a value at least as new as the version.
Possible scenarios for the reader of the cache entry, as it is being written to concurrently: