Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions include/pybind11/detail/type_caster_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ handle smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&src,
}

auto inst = reinterpret_steal<object>(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();
Expand All @@ -452,10 +457,6 @@ handle smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&src,
auto smhldr = smart_holder::from_unique_ptr(std::move(src), src_raw_void_ptr);
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));

if (policy == return_value_policy::reference_internal) {
keep_alive_impl(inst, parent);
}

return inst.release();
}

Expand Down Expand Up @@ -507,6 +508,11 @@ handle smart_holder_from_shared_ptr(const std::shared_ptr<T> &src,
}

auto inst = reinterpret_steal<object>(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();
Expand All @@ -515,10 +521,6 @@ handle smart_holder_from_shared_ptr(const std::shared_ptr<T> &src,
auto smhldr = smart_holder::from_shared_ptr(std::shared_ptr<void>(src, src_raw_void_ptr));
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));

if (policy == return_value_policy::reference_internal) {
keep_alive_impl(inst, parent);
}

return inst.release();
}

Expand Down
13 changes: 13 additions & 0 deletions tests/test_class_sh_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ TEST_SUBMODULE(class_sh_basic, m) {
[]() { return std::unique_ptr<atyp>(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<atyp>(new atyp("uq_no_parent")),
py::return_value_policy::reference_internal);
});
m.def(
"rtrn_shcr_reference_internal_no_parent",
[]() -> const std::shared_ptr<atyp> & {
static const std::shared_ptr<atyp> obj(new atyp("sh_no_parent"));
return obj;
},
py::return_value_policy::reference_internal);

m.def("pass_shared_ptr_ptr", [](std::shared_ptr<atyp> *) {});

py::classh<LocalUnusualOpRef>(m, "LocalUnusualOpRef");
Expand Down
14 changes: 14 additions & 0 deletions tests/test_class_sh_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ TEST_SUBMODULE(thread, m) {
m.def("get_shared_object_classic", &get_shared<SharedObjectClassic>);
py::classh<SharedObjectSh>(m, "SharedObjectSh").def("value", &SharedObjectSh::value);
m.def("get_shared_object_sh", &get_shared<SharedObjectSh>);
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<SharedObjectSh> & {
static const auto instance = get_shared<SharedObjectSh>();
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
Expand Down
8 changes: 7 additions & 1 deletion tests/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading