pybind / pybind/pybind11

Bus error when importing numpy from a separate thread.

Open
#4,240 3 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
Problem description

I have a very simple program in which I want to embed python code.
For this, I use py::initialize_interpreter() to start the interpreter. Afterwards, I directly want to import numpy. Consider the following code:

#include <iostream>
#include <thread>

#include <pybind11/embed.h>
namespace py = pybind11;

void test()
{
    py::initialize_interpreter(); // if I understand correctly, the GIL is still locked by now, thus we can safely call python.
    py::module_* mod = new py::module_(py::module_::import("numpy"));
    while(true); // block the function
}

This works well, if I call this function from the main thread:

int main()
{
    test();
    while(true);
}

However, if I call the same function from a different thread, it does not work.
The following yields a bus error, when importing numpy.

int main()
{
    std::thread testThread(&test);
    while(true);
}

I am building the application using cmake:

set (CMAKE_CXX_STANDARD 14)
add_subdirectory(pybind11)
add_executable(test main.cpp)
include_directories(${CMAKE_CURRENT_LIST_DIR}/pybind11/include)
target_link_libraries(test PRIVATE pybind11::embed)

I understand, that using Python functions from different thread is not trivial, and that you need to manage the GIL properly. However, this is a very simple test case. All I want to do is to simply start and use the Python interpreter in a separate thread. No other thread is using the interpreter at the same time. The GIL should still be locked after py::initialize_interpreter() (adding a py::gil_scoped_acquire after py::inizialize_interpreter does not solve the issue).
Thus, the only thing I can imagine is that the python interpreter needs to run in the main thread. But if so, why is this the case? And also, this only happens when importing numpy. It does not occure when importing "sys" or "os" for example.

Thanks and best regards

Reproducible example code
// Consider the following code.
// We add a function, that initializes the interpreter and imports numpy.
// If we call this function from the original thread in main, it works.
// If we call it from a separate thread, it does not work.

#include <iostream>
#include <thread>

#include <pybind11/embed.h>
namespace py = pybind11;

void test()
{
    py::initialize_interpreter();

    py::module_* mod = new py::module_(py::module_::import("numpy"));
    printf("done\n");
    while(true);
}

// The following works
int main()
{
    test();
    while(true);
}


// This does not work (bus error).
int main()
{
    std::thread testThread(&test);
    while(true);
}

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

Start by running the provided C++ reproducer with the CMake setup and compare importing numpy from main versus std::thread after py::initialize_interpreter(). Read the pybind11::embed initialization and thread/GIL documentation, then investigate the differing numpy import behavior. Done means identifying the cause of the bus error and documenting or implementing a verified resolution.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.