[BUG]: gil_scoped_acquire blocks
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
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.
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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