diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 4c7691ff8d..075073d57b 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -439,6 +439,11 @@ handle smart_holder_from_unique_ptr(std::unique_ptr &&src, } auto inst = reinterpret_steal(make_new_instance(tinfo->type)); + // Before the instance owns the pointer (this can throw), and before init_instance + // publishes the instance to other threads. + if (policy == return_value_policy::reference_internal) { + keep_alive_impl(inst, parent); + } auto *inst_raw_ptr = get_instance(inst.ptr()); inst_raw_ptr->owned = true; void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr(); @@ -452,10 +457,6 @@ handle smart_holder_from_unique_ptr(std::unique_ptr &&src, auto smhldr = smart_holder::from_unique_ptr(std::move(src), src_raw_void_ptr); tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); - if (policy == return_value_policy::reference_internal) { - keep_alive_impl(inst, parent); - } - return inst.release(); } @@ -507,6 +508,11 @@ handle smart_holder_from_shared_ptr(const std::shared_ptr &src, } auto inst = reinterpret_steal(make_new_instance(tinfo->type)); + // Before the instance owns the pointer (this can throw), and before init_instance + // publishes the instance to other threads. + if (policy == return_value_policy::reference_internal) { + keep_alive_impl(inst, parent); + } auto *inst_raw_ptr = get_instance(inst.ptr()); inst_raw_ptr->owned = true; void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr(); @@ -515,10 +521,6 @@ handle smart_holder_from_shared_ptr(const std::shared_ptr &src, auto smhldr = smart_holder::from_shared_ptr(std::shared_ptr(src, src_raw_void_ptr)); tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); - if (policy == return_value_policy::reference_internal) { - keep_alive_impl(inst, parent); - } - return inst.release(); } diff --git a/tests/test_class_sh_basic.cpp b/tests/test_class_sh_basic.cpp index d9cbe1f6be..dec111e069 100644 --- a/tests/test_class_sh_basic.cpp +++ b/tests/test_class_sh_basic.cpp @@ -235,6 +235,19 @@ TEST_SUBMODULE(class_sh_basic, m) { []() { return std::unique_ptr(new atyp("rtrn_uq_automatic_reference")); }, pybind11::return_value_policy::automatic_reference); + // reference_internal without a parent: keep_alive fails before the holder exists. + m.def("cast_uq_reference_internal_no_parent", []() { + return py::cast(std::unique_ptr(new atyp("uq_no_parent")), + py::return_value_policy::reference_internal); + }); + m.def( + "rtrn_shcr_reference_internal_no_parent", + []() -> const std::shared_ptr & { + static const std::shared_ptr obj(new atyp("sh_no_parent")); + return obj; + }, + py::return_value_policy::reference_internal); + m.def("pass_shared_ptr_ptr", [](std::shared_ptr *) {}); py::classh(m, "LocalUnusualOpRef"); diff --git a/tests/test_class_sh_basic.py b/tests/test_class_sh_basic.py index aaea87b8f3..1ad8430e43 100644 --- a/tests/test_class_sh_basic.py +++ b/tests/test_class_sh_basic.py @@ -232,6 +232,20 @@ def test_unique_ptr_return_value_policy_automatic_reference(): assert m.get_mtxt(m.rtrn_uq_automatic_reference()) == "rtrn_uq_automatic_reference" +@pytest.mark.parametrize( + "rtrn_f", + [ + m.cast_uq_reference_internal_no_parent, + m.rtrn_shcr_reference_internal_no_parent, + ], +) +def test_reference_internal_no_parent(rtrn_f): + # Was a double free: the wrapper and the temporary holder both owned the pointer. + for _ in range(2): + with pytest.raises(RuntimeError, match="Could not activate keep_alive!"): + rtrn_f() + + def test_pass_shared_ptr_ptr(): obj = m.atyp() with pytest.raises(RuntimeError) as excinfo: diff --git a/tests/test_thread.cpp b/tests/test_thread.cpp index fcef8bef0f..1e67dc4a5a 100644 --- a/tests/test_thread.cpp +++ b/tests/test_thread.cpp @@ -235,6 +235,14 @@ TEST_SUBMODULE(thread, m) { m.def("get_shared_object_classic", &get_shared); py::classh(m, "SharedObjectSh").def("value", &SharedObjectSh::value); m.def("get_shared_object_sh", &get_shared); + m.def( + "get_shared_object_sh_keep_alive", + // By reference: a by-value return has its policy replaced with `move`. + [](py::handle /*parent*/) -> const std::shared_ptr & { + static const auto instance = get_shared(); + return instance; + }, + py::return_value_policy::reference_internal); #if defined(PYBIND11_HAS_STD_BARRIER) // In the free-threaded build, during PyThreadState_Clear, removing the thread from the biased diff --git a/tests/test_thread.py b/tests/test_thread.py index f5a71a9e6a..16d7e45d21 100644 --- a/tests/test_thread.py +++ b/tests/test_thread.py @@ -75,7 +75,13 @@ def access_shared_instance(): @pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") @pytest.mark.parametrize( - "get_shared", [m.get_shared_object_classic, m.get_shared_object_sh] + "get_shared", + [ + m.get_shared_object_classic, + m.get_shared_object_sh, + # reference_internal adds a patient to the new wrapper. + lambda: m.get_shared_object_sh_keep_alive(m), + ], ) def test_concurrent_cast_of_shared_cpp_object(get_shared): # Only fails under ThreadSanitizer on free-threaded builds; otherwise the race is silent.