Skip to content

Commit 15d9dae

Browse files
Fix data race when using shared variables (free threading) (#5494)
* Fix data race when using shared variables (free threading) 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 #5489 * style: pre-commit fixes * Avoid unused parameter * Add missing return for default build * Changes from review * Assign result to local variable * s/clang-tidy/ruff * clang-tidy: static is redundant * Use 'noqa: B018' --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 945e251 commit 15d9dae

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

include/pybind11/detail/class.h

+24
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,31 @@ inline void traverse_offset_bases(void *valueptr,
312312
}
313313
}
314314

315+
#ifdef Py_GIL_DISABLED
316+
inline void enable_try_inc_ref(PyObject *obj) {
317+
// TODO: Replace with PyUnstable_Object_EnableTryIncRef when available.
318+
// See https://github.com/python/cpython/issues/128844
319+
if (_Py_IsImmortal(obj)) {
320+
return;
321+
}
322+
for (;;) {
323+
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&obj->ob_ref_shared);
324+
if ((shared & _Py_REF_SHARED_FLAG_MASK) != 0) {
325+
// Nothing to do if it's in WEAKREFS, QUEUED, or MERGED states.
326+
return;
327+
}
328+
if (_Py_atomic_compare_exchange_ssize(
329+
&obj->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) {
330+
return;
331+
}
332+
}
333+
}
334+
#endif
335+
315336
inline bool register_instance_impl(void *ptr, instance *self) {
337+
#ifdef Py_GIL_DISABLED
338+
enable_try_inc_ref(reinterpret_cast<PyObject *>(self));
339+
#endif
316340
with_instance_map(ptr, [&](instance_map &instances) { instances.emplace(ptr, self); });
317341
return true; // unused, but gives the same signature as the deregister func
318342
}

include/pybind11/detail/type_caster_base.h

+47-1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,49 @@ PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if
241241
return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
242242
}
243243

244+
inline bool try_incref(PyObject *obj) {
245+
// Tries to increment the reference count of an object if it's not zero.
246+
// TODO: Use PyUnstable_TryIncref when available.
247+
// See https://github.com/python/cpython/issues/128844
248+
#ifdef Py_GIL_DISABLED
249+
// See
250+
// https://github.com/python/cpython/blob/d05140f9f77d7dfc753dd1e5ac3a5962aaa03eff/Include/internal/pycore_object.h#L761
251+
uint32_t local = _Py_atomic_load_uint32_relaxed(&obj->ob_ref_local);
252+
local += 1;
253+
if (local == 0) {
254+
// immortal
255+
return true;
256+
}
257+
if (_Py_IsOwnedByCurrentThread(obj)) {
258+
_Py_atomic_store_uint32_relaxed(&obj->ob_ref_local, local);
259+
# ifdef Py_REF_DEBUG
260+
_Py_INCREF_IncRefTotal();
261+
# endif
262+
return true;
263+
}
264+
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&obj->ob_ref_shared);
265+
for (;;) {
266+
// If the shared refcount is zero and the object is either merged
267+
// or may not have weak references, then we cannot incref it.
268+
if (shared == 0 || shared == _Py_REF_MERGED) {
269+
return false;
270+
}
271+
272+
if (_Py_atomic_compare_exchange_ssize(
273+
&obj->ob_ref_shared, &shared, shared + (1 << _Py_REF_SHARED_SHIFT))) {
274+
# ifdef Py_REF_DEBUG
275+
_Py_INCREF_IncRefTotal();
276+
# endif
277+
return true;
278+
}
279+
}
280+
#else
281+
assert(Py_REFCNT(obj) > 0);
282+
Py_INCREF(obj);
283+
return true;
284+
#endif
285+
}
286+
244287
// Searches the inheritance graph for a registered Python instance, using all_type_info().
245288
PYBIND11_NOINLINE handle find_registered_python_instance(void *src,
246289
const detail::type_info *tinfo) {
@@ -249,7 +292,10 @@ PYBIND11_NOINLINE handle find_registered_python_instance(void *src,
249292
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
250293
for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
251294
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) {
252-
return handle((PyObject *) it_i->second).inc_ref();
295+
auto *wrapper = reinterpret_cast<PyObject *>(it_i->second);
296+
if (try_incref(wrapper)) {
297+
return handle(wrapper);
298+
}
253299
}
254300
}
255301
}

tests/test_thread.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ struct IntStruct {
2828
int value;
2929
};
3030

31+
struct EmptyStruct {};
32+
EmptyStruct SharedInstance;
33+
3134
} // namespace
3235

3336
TEST_SUBMODULE(thread, m) {
@@ -61,6 +64,9 @@ TEST_SUBMODULE(thread, m) {
6164
},
6265
py::call_guard<py::gil_scoped_release>());
6366

67+
py::class_<EmptyStruct>(m, "EmptyStruct")
68+
.def_readonly_static("SharedInstance", &SharedInstance);
69+
6470
// NOTE: std::string_view also uses loader_life_support to ensure that
6571
// the string contents remain alive, but that's a C++ 17 feature.
6672
}

tests/test_thread.py

+19
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,22 @@ def test_implicit_conversion_no_gil():
4747
x.start()
4848
for x in [c, b, a]:
4949
x.join()
50+
51+
52+
@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads")
53+
def test_bind_shared_instance():
54+
nb_threads = 4
55+
b = threading.Barrier(nb_threads)
56+
57+
def access_shared_instance():
58+
b.wait()
59+
for _ in range(1000):
60+
m.EmptyStruct.SharedInstance # noqa: B018
61+
62+
threads = [
63+
threading.Thread(target=access_shared_instance) for _ in range(nb_threads)
64+
]
65+
for thread in threads:
66+
thread.start()
67+
for thread in threads:
68+
thread.join()

0 commit comments

Comments
 (0)