pybind / pybind/pybind11

[BUG] SIGSEGV if Py_Initialize not called when Shared Library instead of Module

Open
#2,892 0 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

It is stated in https://pybind11.readthedocs.io/en/stable/compiling.html#pybind11-add-module that one can use a MODULE or a SHARED keyword when building a target in CMake.

I wanted to create a SHARED library with a class handling all the python binding stuff of a bigger project. When calling from the bigger project the classes and Python methods from the shared library, I got this SIGSEGV:

#8  0x00007fe33f23a558 in PyUnicode_InternInPlace () from /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0
#9  0x00007fe33f23a62d in PyUnicode_InternFromString () from /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0
#10 0x00007fe33f1e012c in PyImport_Import () from /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0
#11 0x00007fe33f1e027a in PyImport_ImportModule () from /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0
#12 0x00007fe342f6edf5 in pybind11::module_::import (name=0x7fe342f83aab "os") at /usr/local/include/pybind11/pybind11.h:992
#13 0x00007fe342f62a97 in USBTMC::USBTMC (this=0x7fffbd6e011f) at usb/USBTMC.cpp:15
#14 0x000055fb3bde1f56 in main (argc=1, argv=0x7fffbd700dd8) at main.cxx:57

I fixed the crash by calling Py_Initialize() in the shared library class constructor, following this link https://stackoverflow.com/questions/53383322/pydict-setitemstring-segfaults

I would like to propose that this segfault is prevented on pybind11 side by checking the result of Py_IsInitialized(), with a warning error telling user to call Py_Initialize. And/or that further information is given on this link https://pybind11.readthedocs.io/en/stable/compiling.html#pybind11-add-module about how to correctly use pybind when using SHARED instead of MODULE.

Thanks for this nice package!!

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 pybind11::module_::import call at pybind11.h:992 and the documented pybind11_add_module CMake guidance. Reproduce the SHARED-library path described from USBTMC.cpp:15 and main.cxx:57, then evaluate handling of an uninitialized interpreter using Py_IsInitialized and the corresponding documentation update. Done means the reported SIGSEGV path is addressed or the SHARED-library initialization requirements are clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp, python
Domain
api, documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.