pybind / pybind/pybind11

[BUG]: ASAN reports that allocated ptr is not deallocated properly.

Open
#3,864 2 comments 1 reaction 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

Hi, ASAN reports that https://github.com/pybind/pybind11/blob/6493f496e30c80f004772c906370c8f4db94b6ec/include/pybind11/detail/internals.h#L498-L508 allocated ptr variable in internals.h:504 is not deallocated properly when calling finalize_interpreter few times.
ASAN report:

==497630==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f80838e8587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104
    #1 0x557df8f07925 in pybind11::detail::local_internals::local_internals() /home/vmcs/projects/scanner/vendor/pybind11/include/pybind11/detail/../detail/internals.h:504
    #2 0x557df8f07925 in pybind11::detail::get_local_internals() /home/vmcs/projects/scanner/vendor/pybind11/include/pybind11/detail/../detail/internals.h:514
    #3 0x557df8f07925 in pybind11::finalize_interpreter() /home/vmcs/projects/scanner/vendor/pybind11/include/pybind11/embed.h:203
Reproducible example code
int test_lib() {
  py::initialize_interpreter();
  try {
    py::object mainScope = py::module::import("__main__").attr("__dict__");
    py::exec( "print('hello')", mainScope);
  } catch(pybind11::error_already_set const& ex) {
    std::cerr << " [PYBIND11 ERROR] " << ex.what() << std::endl;
    return EXIT_FAILURE;
  }
  py::finalize_interpreter();
  return 0;
}
int main() {
  for(int i = 0; i < 10; i ++) {
    test_lib();
  }
}

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 local_internals allocation at include/pybind11/detail/internals.h:498-508 and the finalize_interpreter path in include/pybind11/embed.h:203. Run the provided repeated initialize/finalize example under ASAN; done means the leak report no longer identifies the allocated pointer after ten interpreter cycles.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.