[BUG]: ASAN reports that allocated ptr is not deallocated properly.
Open
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
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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