ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
586 stars 161 forks source link

potential bug: reference leak when torch is loaded and a function errors #666

Open ncullen93 opened 1 month ago

ncullen93 commented 1 month ago

When torch is loaded and a function errors, there is a reference leak reported when the ipython console is exited. This does not seem to happen in a normal python console and only when torch is loaded. Very weird.. I've seen this a few other times but never reproducible so unsure if it's the same issue or if there's a general reference leak.

import ants
import torch
img = ants.image_read(ants.get_data('r16'))
img.crop_indices([0,0],[10,1000])

Then exit() and you get this:

nanobind: leaked 1 instances!
 - leaked instance 0x13362a208 of type "AntsImageF2"
nanobind: leaked 1 types!
 - leaked type "ants.lib.AntsImageF2"
nanobind: leaked 1 functions!
 - leaked function "cropImage"
nanobind: this is likely caused by a reference counting issue in the binding code.
ncullen93 commented 1 month ago

Seems to be something with pytorch and third-party modules generally - e.g. https://github.com/python/cpython/issues/98253. Probably not a big issue but still worrying.

See also nanobind FAQ on the issue https://nanobind.readthedocs.io/en/latest/faq.html#why-am-i-getting-errors-about-leaked-functions-and-types