aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorAlexander Gagarin <unientity@gmail.com>2019-06-12 01:23:08 +0500
committerWenzel Jakob <wenzel.jakob@epfl.ch>2019-06-11 23:28:58 +0200
commit0071a3feb091ee0a1ede2010922ef825c7d08fff (patch)
tree8272fd6548604c9f425e09ea4cf15a62f01ebcb2 /include
parent047ce8c452087809f5ab10d7a2ea58c8709a8da3 (diff)
downloadplatform_external_python_pybind11-0071a3feb091ee0a1ede2010922ef825c7d08fff.tar.gz
platform_external_python_pybind11-0071a3feb091ee0a1ede2010922ef825c7d08fff.tar.bz2
platform_external_python_pybind11-0071a3feb091ee0a1ede2010922ef825c7d08fff.zip
Fix async Python functors invoking from multiple C++ threads (#1587) (#1595)
* Fix async Python functors invoking from multiple C++ threads (#1587) Ensure GIL is held during functor destruction. * Add async Python callbacks test that runs in separate Python thread
Diffstat (limited to 'include')
-rw-r--r--include/pybind11/functional.h15
1 files changed, 13 insertions, 2 deletions
diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h
index 9cdf21f..7a0988a 100644
--- a/include/pybind11/functional.h
+++ b/include/pybind11/functional.h
@@ -54,9 +54,20 @@ public:
}
}
- 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>());
};