Repository navigation
Conversation
under_cached_property.__get__ safe for free-threadingunder_cached_property and cached_property safe for free-threading
At 20,000 iterations the cached_property test only crashed the unpatched free-threaded build in 2 of 3 runs; at 100,000 both tests crash it in 5 of 5 runs while still finishing in well under a second.
|
|
Will need to benchmark that. The idea was to keep the instructions byte identical from master on GIL |
Cython's cpython.dict.PyDict_GetItemRef replaces the custom lookup helper. Lookup errors now propagate on GIL builds too, matching the pure Python implementation, instead of being reported as unraisable and treated as a miss. PyDict_GetItem saves and restores the pending exception on every call, so the hit path is no slower on GIL builds. The miss path is plain Cython again, which also releases the computed value when storing it in the cache fails.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #297 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 10 10
Lines 437 532 +95
Branches 17 27 +10
=========================================
+ Hits 437 532 +95
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Adopted in 4233aff. Hit path no longer byte-identical to master. Cython maps |
|
|
The race the test guards against cannot happen with the GIL, so GIL and PyPy legs run 1,000 iterations instead of 100,000.
PR Review — Make
|
Confidence Score: 5/5Safe to merge. Reviews (2): Last reviewed commit: "Keep the new tests out of the mypy impre..." | Re-trigger Greptile |
Annotate the instance dict, reach the descriptor through the class instead of __dict__, and build the thread list in one comprehension, so the added lines do not lower the type preciseness score.
What do these changes do?
Fixes a use-after-free in the C
cached_propertyandunder_cached_propertydescriptors on free-threaded Python. The cached value was read through a borrowed reference fromPyDict_GetItem, so another thread evicting it could free it before__get__returned it. The descriptors now usePyDict_GetItemReffromcpython.dicton all builds.On GIL builds the hit path is as fast as before or slightly faster, since
PyDict_GetItemsaves and restores the pending exception on every call. Errors raised while looking up the cache now propagate like they do in the pure Python implementation, instead of being reported as unraisable and treated as a miss. The miss path also no longer leaks the computed value when storing it in the cache fails.Are there changes in behavior for the user?
Free-threaded builds no longer crash when a cached value is evicted concurrently. Errors raised while looking up the cache, such as from a colliding key whose
__eq__raises, are now propagated by the C extension.Related issue number
N/A
Checklist
CHANGES/folder