1

I am getting a series of warnings that occurs during interpreter shutdown of a Python 2.7 script when using libvirt. This is reproducible on a variety of distros including python2-libvirt 3.7.0-1 on Fedora 27 to libvirt-python 3.2.0-1 on Centos 7.4.1708. The warnings I get when the script is exitings is:

Exception AttributeError: "'NoneType' object has no attribute 'virDomainFree'" in <bound method virDomain.__del__ of <libvirt.virDomain object at 0x7f34a194ee10>> ignored
Exception AttributeError: "'NoneType' object has no attribute 'virDomainFree'" in <bound method virDomain.__del__ of <libvirt.virDomain object at 0x7f34a194ed90>> ignored
Exception AttributeError: "'NoneType' object has no attribute 'virConnectClose'" in <bound method virConnect.__del__ of <libvirt.virConnect object at 0x7f34a176a590>> ignored

Drilling down into the library, it seems to be an issue with assumptions in destructor order as in this code from libvirt.py:

class virDomain(object):
    def __del__(self):
        if self._o is not None:
            libvirtmod.virDomainFree(self._o)
        self._o = None

libvirtmod is a global created by an import at the top of the libvirt.py module. When the __del__() destructor for virDomain is finally run, libvirtmod has been replaced by the value None causing the code above to fail with a warning. We have been using this Python module for some time now, but only recently have these warning started showing up after we refactored the code quite heavily. What can I do to suppress these warnings from standard error or avoid the situation from occurring? Is there a way to ensure objects from libvirt.py are cleaned up before libvirtmod.so goes away?

We test on a variety of distros and would like to stick with using the stock (but updated) packages that come with the distro.

penguin359
  • 1,289
  • 13
  • 26

1 Answers1

0

The standard trick is to equip __del__ with default arguments that hold whatever global values it needs (here, libvirtmod itself would suffice). There is a variation using weakref, which can also benefit from default arguments.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Tested with default arguments, but it still seems that libvirtmod gets unloaded before libvirt in some cases. Ideally, I'd like to find a solution that doesn't require I patch someone else's library installed via the distribution's package management, but I may just be dealing with a buggy library. – penguin359 Apr 03 '18 at 16:48
  • @penguin359: Sorry—I think you actually need to capture the `libvirtmod` _member(s)_, not just the module. Can you try that, and I’ll edit the answer if it works? – Davis Herring Apr 03 '18 at 23:51