Skip to content

[BUG]: scoped_interpreter destructor does not respond #4172

Description

@bogdan-lab

Required prerequisites

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;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions