-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG]: Data race when using static variables with free-threaded Python #5489
Comments
This appears to stem from unsafe memory management and reference counting. So, storing a persistence reference to Foo.bar that you did prevents this reference count from dropping to zero. To make it thread safe try
This approach should eliminate the race conditions, segmentation faults, and exceptions while ensuring compatibility with free-threaded Python. |
Did you already try the reproducer with the latest state of the master branch? In particular, #5419 was merged after the 2.13.6 release (I'm totally not sure if that's related to your issue, but it's a quick experiment). @colesbury for awareness, JIC this rings any bells. |
Thanks for the reply! I tried again on master and it doesn't solve the issue. |
I'm able to reproduce the failure. I'll look into it. |
Ok, I think I understand the issue. There's a race between object deallocation ( There's a brief period of time when the instance's reference count is zero, but it's not yet removed from the instance map. During this time, another thread may retrieve the instance from the map and increment its reference count) Deallocation still proceeds as normal, but now there's an instance that thinks it's registered but is not in the instance map. This crashes on subsequent deallocation calls. In CPython, we use |
Thank you @colesbury for looking into this. Would this request be a similar thing we are trying to achieve? python/cpython#113920 |
Yes |
In the free threading build, there's a race between wrapper re-use and wrapper deallocation. This can happen with a static variable accessed by multiple threads. Fixing this requires using some private CPython APIs: _Py_TryIncref and _PyObject_SetMaybeWeakref. The implementations of these functions are included until they're made available as public ("unstable") APIs. Fixes pybind#5489
Required prerequisites
What version (or hash if on master) of pybind11 are you using?
v2.13.6
Problem description
With free-threaded Python, I get data races when using static class variables defined like this:
I get multiple kinds of errors when accessing
Foo.Bar
from multiple threads, from segmentation faults to "pybind11_object_dealloc(): Tried to deallocate unregistered instance!
" exceptions.Further investigation suggested that it might come from
Foo.Bar
being freed too many times. I'm unsure if it's a pybind11 or CPython issue so I'm posting it here.Reproducible example code
Is this a regression? Put the last known working version here if it is.
Not a regression
The text was updated successfully, but these errors were encountered: