Add critical section guards to accessors#2215
Conversation
Protect accessors with cython critical sections so free-threaded execution avoids races when reading cached state. At least one of these was noticed by free-threaded test runs. I am not certain that they will guarantee identity returns, but pretty sure this will make them thread-safe. (The reason being that at least on non free-threaded builds they may release the GIL or, on free-threaded, do API calls that release the critical section temporarily.) About the `.pyi` fixes: I think these are incorrect but `cython` is already used in other places and shouldn't be a runtime/typing requirement at all.
|
/ok to test 652a3e7 |
|
We probably need to submit a fix to There are obviously a lot more properties/accessors in the codebase. Besides seeing multi-threaded test failures, are there other criteria we should follow about when to apply these decorators? |
Yeah, right now it seems you need
Whenever we mutate a I would hope that Cython will provide things to make this easier in the future (maybe You are not wrong, in that I should make another pass and maybe TSAN testing could flush out some as well. In practice hitting these issues is very unlikely but if you do... |
Protect accessors with cython critical sections so free-threaded execution avoids races when reading cached state.
@cython.critical_sectioneffectively takes a lock based on the first argument passed in. (Although that lock can be released to avoid deadlocks, it is as safe or safer as the GIL, but that doesn't make it 100% in all situations.)At least one of these was noticed by free-threaded test runs. I am not certain that they will guarantee identity returns, but pretty sure this will make them thread-safe.
(The reason being that at least on non free-threaded builds they may release the GIL or, on free-threaded, do API calls that release the critical section temporarily.)
About the
.pyifixes: I think these are incorrect butcythonis already used in other places and shouldn't be a runtime/typing requirement at all.Towards gh-2194 which will add some testing although not strictly guaranteed for all paths covered here.