gh-149816: #96 Fix a race condition in invoke_gc_callback with free threading by dzaramelcone · Pull Request #150029 · python/cpython
The optimization that avoided allocating an info dict when no gc callbacks were registered introduces a time of check to time of use race in free threaded mode (lineno 1943-1955). I just pulled out the check. Since info is always non-null, I also replaced XDECREF use with DECREF. (lineno 1959, lineno 1982).
I implemented a stress test for this and uncovered what I believe to be a bug with _PyList_GetItemRef. I am following suit with python/cpython#149909 and not including the test with my PR for two reasons:
- My implementation was resource intensive and not deterministic.
- I didn't want to scope creep fixing the bug with
_PyList_GetItemRef(but I'm happy to open an issue for it!)
also, this is my first PR against CPython, I am doing it from PyCon US as part of the sprint! :) Hello world!