Closed
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
Problem description
I am trying to make a singleton wrapper for pybind11::scoped_interpreter
. The idea is, that class will create separate thread for python, then create there scoped_interpreter
and do all python calculation in that thread. The problem is that in this case scoped_interpreter
destructor freezes. It actually feels like some deadlock, however I do not have evidence of that. It freezes on the line
detail::get_local_internals().registered_types_cpp.clear();
Interestingly, it freezes only in case when I try to join
the python thread (interpreter is the local variable of that thread) in singleton class destructor. If I create separate method for joining python thread and call it somewhere before leaving the main()
, interpreter destructor works OK.
Windows Server 2019 Standart
Visual Studio C++ 2019
Reproducible example code
class WorkerExample {
public:
static WorkerExample& Access() {
static WorkerExample we;
return we;
}
void Stop() {
do_work_ = false;
if (python_thread_.joinable()) python_thread_.join();
}
~WorkerExample() { Stop(); }
private:
WorkerExample() {
python_thread_ = std::thread([this]() {
pybind11::scoped_interpreter guard{};
while (do_work_) {
// place holder for calculations, but bug can be reproduced without them
}
});
}
std::thread python_thread_;
std::atomic<bool> do_work_ = true;
};
int main() {
auto& we = WorkerExample::Access();
// uncomment line below in order to avoid freezing in
// scoped_interpreter destructor
// we.Stop();
return 0;
}