pybind / pybind/pybind11

[BUG]: gil_scoped_acquire blocks

Open
#4,848 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

pybind11_bazel commit: fc56ce8a8b51e3dd941139d329b63ccfea1d304b

Problem description

I have a struct Middleware in C++ and I want to hold scoped_interpreter py::object and event_loop_thread as fields.
In the method initialize I have some std::thread, that will invoke some python code there.

Since it is multithreaded, I want to acquire in the thread, but it just blocks and hangs, no error is thrown, the release before the thread start didn't help as well.

Any suggestions please?

No other code running.

struct Middleware{
  void initialize(){
    py::gil_scoped_release release; // didn't help ???
    event_loop_runner = std::thread([&]() {
      {
        {
          py::gil_scoped_acquire acquire; // ??? blocks for some reason
          py::object module = py::module::import("simple.python");
          event_loop_thread = module.attr("EventLoopThread")();
          event_loop_thread.attr("start")(); // non-blocking
        }
        std::unique_lock<std::mutex> lock(python_thread_lock_);
        cv.wait(lock, [&]() { return !is_running; }); // block until stop called.
      }
    });
  }

  pybind11::scoped_interpreter guard;
  py::object event_loop_thread;
   std::thread event_loop_runner;
};
Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names only Middleware::initialize, py::gil_scoped_acquire, py::gil_scoped_release, and the scoped_interpreter member; no repository file or test is provided. Start by reviewing the complete threading and interpreter-lifetime example and reproducing the hang. Done means identifying the cause and documenting an actionable correction or a confirmed missing prerequisite.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.