pybind / pybind/pybind11

[BUG]: Segfault on RHEL/CentOS when binding code that imports shared library and STL queue

Open
#3,386 7 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 met a crash on a project with CentOS, that I could isolate with the following minimal conditions:

If a shared library has class with a std::queue member, using this class in a pybind11 module will lead to a segmentation fault.

So yes, all these conditions must be met:

  • If the library is static, or code is directly included in the pybind module, no crash.
  • If you run it on Ubuntu, no crash.
  • And if you replace the std::queue with a std::vector, no crash.

(Note that it's probably not a duplicate of #3380)

Adding a complete PR with the below code in a few moments

Reproducible example code
crashing.cpp

#include "crashing.h"

crashing::crashing()
= default;
crashing.h
#include <queue>

class crashing{
public:
    crashing();

private:
    std::queue<int> queue_;
};
pybind.cpp
#include "crashing.h"
#include <pybind11/pybind11.h>

void crash()
{
    crashing crash;
}

PYBIND11_MODULE(crash_shared, m) {
    m.def("crash", &crash);
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(crash)

add_library(libcrash SHARED crashing.cpp)
target_compile_features(libcrash PRIVATE cxx_std_14)

pybind11_add_module(crash_shared pybind.cpp)
target_link_libraries(crash_shared PUBLIC
        $<TARGET_LINKER_FILE_DIR:libcrash>/$<TARGET_LINKER_FILE_NAME:libcrash>)
add_dependencies(crash_shared libcrash)
pybind.py
import crash_shared
crash_shared.crash()

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 with the minimal example files crashing.cpp, crashing.h, pybind.cpp, CMakeLists.txt, and pybind.py. Build the shared-library configuration and run pybind.py on RHEL or CentOS, comparing it with the static, directly included, vector, and Ubuntu cases. Done means the shared-library case no longer segfaults while the reproduction remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
centos, cmake, cpp, python
Domain
build-system, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.