nanobind icon indicating copy to clipboard operation
nanobind copied to clipboard

leak report

Open tbrekalo opened this issue 2 years ago • 2 comments

Discussed in https://github.com/wjakob/nanobind/discussions/13

Originally posted by tbrekalo March 31, 2022 How do I interpret the following?

nanobind: leaked 1 types!
 - leaked type "Pile"
nanobind: leaked 5 functions!
 - leaked function "<anonymous>"
 - leaked function "<anonymous>"
 - leaked function "<anonymous>"
 - leaked function "<anonymous>"
 - leaked function "__init__"
nanobind: this is likely caused by a reference counting issue in the binding code.

Is it a bug on my part for getting something wrong in binding code or is it an issue with nanobind? Side note: code causing this issue deals with shared pointers for another type that is not brought up by the error message. Type 'Pile' is regular value type, I have a std::vector of them. So I am not sure how reference counting is being mentioned here.

Thanks for your help in advance.

Issue with false report of type leaks influenced by python type hints and imort of non-standard modules (in this case; seaborn 0.11)

tbrekalo avatar Apr 06 '22 09:04 tbrekalo

Generally, interpreter shutdown is a tricky business in Python, but collectable instances (including heap types) should be collected, and a lot of work has been put into this by the CPython team. This is needed to correctly close sockets, flush buffers, etc. -- things that are essential for the program to work correctly. However, a reference leak normally does not generate an error/warning and can still be missed.

So when nanobind complains about things not being garbage collected, it deserves some scrutiny instead of disabling the warning.

In your case, what seems to be responsible is a binary extension module of pandas that is imported by seaborn. In particular, the following line triggers this reference counting issue

import pandas._libs

This is as far as I got.

wjakob avatar Apr 09 '22 15:04 wjakob

I also encountered the leak issue https://github.com/rgl-epfl/cholespy/issues/10#issuecomment-1233807046. But I have no idea how to solve it.

xk-huang avatar Sep 01 '22 06:09 xk-huang

@tbrekalo @xk-huang : This is not a pybind11 issue to my knowledge. I believe that the following snippet should fix it:

import atexit

def cleanup():
    import typing
    for cleanup in typing._cleanups:
        cleanup()

atexit.register(cleanup)

wjakob avatar Oct 13 '22 19:10 wjakob

I filed a bug with Python: https://github.com/python/cpython/issues/98253

wjakob avatar Oct 13 '22 22:10 wjakob

Thanks for your kind reply! the cleanup indeed worked.

xk-huang avatar Oct 19 '22 01:10 xk-huang