pybind / pybind/pybind11

[BUG] Crash on garbage collection with python 3.9

Open
#3,108 1 comment 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

I'm wrapping the python logger on c++ side and for convenience store the logger attributes "debug", "info", "warning" and "error" in some class members. The logger is initialized by name via the getLogger function in python and is then kept as a global variable. This works fine with python <= 3.8, but causes an issue during garbage collection in python 3.9, resulting in a non-zero exit code.

Please consider the following reproducer.

In c++ with pybind 2.4.2 (also tried 2.6.2):

#include <pybind11/pybind11.h>

namespace py = pybind11;

namespace {
    struct NamedLogger 
    {
        const std::string name;
        py::object logger;
        py::object debug;
        py::object info;
        py::object warning;
        py::object error;

        NamedLogger(const std::string& name_)
            : name(name_) 
        {
            auto logging = py::module::import("logging");
            auto getLogger = logging.attr("getLogger");
            logger = getLogger(name);

            debug = logger.attr("debug");
            info = logger.attr("info");
            warning = logger.attr("warning");
            error = logger.attr("error");
        }
    };

    std::unique_ptr<NamedLogger> logger;

} // namespace

// Module ======================================================================
PYBIND11_MODULE( logger_cpp, mod ) 
{
    logger.reset( new NamedLogger("MyLogger") );
    logger->debug( "I'm alive" );
}

Then simply run

python -c "import logger_cpp"

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 reproducer and run python -c "import logger_cpp" under Python 3.9, comparing it with Python 3.8. Trace the global NamedLogger and its stored logger attributes during interpreter garbage collection; done means the import exits cleanly with code 0 on Python 3.9.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.