Skip to content

Commit cfab0d3

Browse files
committed
Fix async Python functors invoking from multiple C++ threads (pybind#1587)
Ensure GIL is released after functor destructor finished (not only during functor execution as in previous implementation).
1 parent 978d439 commit cfab0d3

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

include/pybind11/functional.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ struct type_caster<std::function<Return(Args...)>> {
5454
}
5555
}
5656

57-
value = [func](Args... args) -> Return {
57+
value = [func](Args... args) mutable -> Return {
5858
gil_scoped_acquire acq;
59-
object retval(func(std::forward<Args>(args)...));
59+
// move into local var to ensure GIL is released AFTER functor destructor is called
60+
function f(std::move(func));
61+
object retval(f(std::forward<Args>(args)...));
6062
/* Visual studio 2015 parser issue: need parentheses around this expression */
6163
return (retval.template cast<Return>());
6264
};

0 commit comments

Comments
 (0)