pybind / pybind/pybind11

[BUG] A dynamic link library (DLL) initialization routine failed when statically invoking globals() with Python 3.8

Open
#2,493 4 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

Issue description

Saving pybind11::globals() in a class static variable throws a Python interpreter' s exception under Windows 10 with Python 3.8 but it works with Python 3.7. I compile my code with MSVC 19.27.29111.0 (Visual Studio 16 2019 build tools called from cmake).

The exception is: ImportError: DLL load failed while importing __pybind11_debug: A dynamic link library (DLL) initialization routine failed.

Reproducible example code

Here is a minimal code that crashes on Windows 10 with Python 3.8 (using the command line C:\Users\vagrant\AppData\Local\Programs\Python\Python38\python.exe -c "import __pybind11_debug"):

#include <pybind11/pybind11.h>

namespace py = pybind11;

struct A {
    inline static py::object globals = py::globals();
};

PYBIND11_MODULE(__pybind11_debug, m) {
     py::class_<A> py_a(m, "A");
}

The same code perfectly works with Python 3.7 on Windows 10 (and also with Python 3.7 and 3.8 on Linux and MacOS).

Note: storing pybind11::globals() in a class static variable might seem silly and useless. I actually need to store py::globals()["__builtins__"]["NotImplemented"] that my real code calls millions of time (thus I want to save millions of dictionary key searches).

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 C++ reproducer in the issue and run the Python 3.8 command that imports __pybind11_debug on Windows. Investigate the static py::globals initialization path and compare it with Python 3.7 and other platforms; done means the reproducer imports successfully on Python 3.8 without losing the intended cached globals access.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, backend
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.