Skip to content

Commit

Permalink
Fix async Python functors invoking from multiple C++ threads (#1587)
Browse files Browse the repository at this point in the history
Ensure GIL is held during functor destruction.
  • Loading branch information
uentity committed Dec 2, 2018
1 parent 83113a0 commit b66623f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions include/pybind11/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,20 @@ struct type_caster<std::function<Return(Args...)>> {
}
}

value = [func](Args... args) -> Return {
// ensure GIL is held during functor destruction
struct func_handle {
function f;
func_handle(function&& f_) : f(std::move(f_)) {}
func_handle(const func_handle&) = default;
~func_handle() {
gil_scoped_acquire acq;
function kill_f(std::move(f));
}
};

value = [hfunc = func_handle(std::move(func))](Args... args) -> Return {
gil_scoped_acquire acq;
object retval(func(std::forward<Args>(args)...));
object retval(hfunc.f(std::forward<Args>(args)...));
/* Visual studio 2015 parser issue: need parentheses around this expression */
return (retval.template cast<Return>());
};
Expand Down

0 comments on commit b66623f

Please sign in to comment.