Skip to content

Commit 9a4eb75

Browse files
committed
Use native API for holding GIL when Python functor is being destructed
Don't use `gil_scoped_aquire` because it can result in deadlock.
1 parent 0814105 commit 9a4eb75

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

include/pybind11/functional.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ struct type_caster<std::function<Return(Args...)>> {
6060
func_handle(function&& f_) : f(std::move(f_)) {}
6161
func_handle(const func_handle&) = default;
6262
~func_handle() {
63-
gil_scoped_acquire acq;
64-
function kill_f(std::move(f));
63+
PyGILState_STATE gstate;
64+
gstate = PyGILState_Ensure();
65+
{
66+
function kill_f(std::move(f));
67+
}
68+
PyGILState_Release(gstate);
6569
}
6670
};
6771

0 commit comments

Comments
 (0)