pybind / pybind/pybind11

[BUG] Segmentation Fault while importing library with pybind >= v2.6

Open
#3,054 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hello, I've tried to take a look at issues, examples and the documentation to solve the problem but could not find anything.

Issue description

I use pybind v2.5 on my python library, and recently i decided to update that to the latest version. However, using pybind 2.6.0, 2.6.1 and 2.6.2(revision 1587960bf437392e322ca3202dd347ed in conan center) causes getting segmentation fault when trying to import the library from python side. 2.6.2 (revision 66f97eac059bef5b60bd6898ec4869fd in conan center) causes a cmake error that I guess is discussed for previous versions (so it was supposed to be fixed in version 2.6.2)
I'm using conan in cmake_find_package_multi mode.
Environment : MacOS 11.2.1
Conan : v1.37.2 with the following profile configuration

[settings]
arch=x86_64
arch_build=x86_64
build_type=Debug
compiler=apple-clang
compiler.libcxx=libc++
compiler.version=11.0
os=Macos
os_build=Macos
[options]
[build_requires]
[env]

CMake : v3.16.2
Python : Both tried 3.9.0 and 3.9.5

Reproducible example code

conanfile.txt

 [requires]
 pybind11/2.6.1

 [generators]
 cmake_find_package_multi

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 17)

project(py_lib DESCRIPTION "Test library")

set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR};${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR};${CMAKE_PREFIX_PATH})

set (PYBIND11_PYTHON_VERSION "3.9")
find_package(pybind11 CONFIG REQUIRED)
if (TARGET pybind11::pybind11)
    message("pybind11 Found")
    set(pybind11_libraries pybind11::pybind11)
endif()

pybind11_add_module(py_lib main.cpp)

target_link_libraries(py_lib PRIVATE ${pybind11_libraries})

main.cpp

#include <iostream>
#include "pybind11/pybind11.h"

namespace py = pybind11;

std::string sum(int a, int b) {
  std::cout << "Sum!" << std::endl;
  return "Result is : " + std::to_string(a + b);
}

PYBIND11_MODULE(py_lib, module) {
  std::cout << "Module start";
  module.def("sum", &sum,
    "Documentation for sum.",
    py::arg("a"), py::arg("b"));
  std::cout << "Py library module loaded";
}

For the simple reproducible build you can use the following commands

mkdir out && cd out
conan install -s build_type=Debug ../
cmake -DCMAKE_BUILD_TYPE=Debug ../
make

That should create the library under directory ./out and when I run python -c "import py_lib" from that folder, i get "Segmentation Fault: 11"

The exact same project works without any problems with pybind 2.5.0

I would be glad to hear if there is anything wrong above.

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 conanfile.txt, CMakeLists.txt, and main.cpp, then reproduce the import using the listed Conan, CMake, and Python commands. Compare the build and import behavior between pybind11 2.5.0 and versions 2.6.x on macOS. Done means the example builds without the reported CMake error and imports without a segmentation fault.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.