sethmlarson / virtualbox-python

Complete implementation of VirtualBox's COM API with a Pythonic interface.
https://pypi.org/project/virtualbox
Apache License 2.0
354 stars 75 forks source link

Python script errors using pyvbox on Windows #35

Closed jiml521 closed 8 years ago

jiml521 commented 9 years ago

Hi, After completing a script run to control a VirtualBox guest, the script ends with:

Exception RuntimeError: 'sys.metapath must be a list of import hooks' in bound method VirtualBoxManager. del _ of vboxapi.VirtualBoxManager object at 0x0000000002D7F0B8 ignored

I assume this is because something is still open or allocated; session.unlock_machine() doesn't help.

My question: is this a pyvbox bug, or vboxapi bug?

mjdorma commented 9 years ago

Thanks for bringing this to my attention.

Checking out the delete function in the class definition of vboxapi.VirtualBoxManager:

    def deinit(self):
        """
        For unitializing the manager.
        Do not access it after calling this method.
        """
        if hasattr(self, "vbox"):
            del self.vbox
            self.vbox = None
        if hasattr(self, "platform"):
            self.platform.deinit()
            self.platform = None
        return True;

    def __del__(self):
        self.deinit()

I wonder if you wrapped the self.deinit() func in an exception handler if that would resolve your issue (hack the vboxapi for the test)? If that is the problem I think we could add an atexit hook that explicitly deinit's each VirtualBoxManager object when the interpreter is exited.

This problem could also be due to the crazy context import vboxapi with a VirtualBoxManager per process code that was required to make multiprocessing library work sanely in pyvbox's virtualbox/init.py function (avoids this https://www.virtualbox.org/ticket/12127).

jiml521 commented 9 years ago

Yes - wrapping that func with a try/except solves the problem in the short term. Thanks for the advice.

On Mon, May 18, 2015 at 9:02 AM, Michael Dorman notifications@github.com wrote:

Thanks for bringing this to my attention.

Checking out the delete function in the class definition of vboxapi.VirtualBoxManager:

def deinit(self):
    """        For unitializing the manager.        Do not access it after calling this method.        """
    if hasattr(self, "vbox"):
        del self.vbox
        self.vbox = None
    if hasattr(self, "platform"):
        self.platform.deinit()
        self.platform = None
    return True;

def __del__(self):
    self.deinit()

I wonder if you wrapped the self.deinit() func in an exception handler if that would resolve your issue (hack the vboxapi for the test)? If that is the problem I think we could add an atexit hook that explicitly deinit's each VirtualBoxManager object when the interpreter is exited.

This problem could also be due to the crazy context import vboxapi with a VirtualBoxManager per process code that was required to make multiprocessing library work sanely in pyvbox's virtualbox/init.py function (avoids this https://www.virtualbox.org/ticket/12127).

— Reply to this email directly or view it on GitHub https://github.com/mjdorma/pyvbox/issues/35#issuecomment-103052121.

mjdorma commented 9 years ago

I just uploaded a 0.2.1 release which has cleanup code to attempt a denint. Please let me know if this resolves your issue.

The vboxapi code has a fair few dags like:

if hasattr(self, "vbox"):
            del self.vbox
            self.vbox = None

It should be either checking if vbox is None already or performing a delattr(self, 'vbox').

jiml521 commented 9 years ago

I'll give it a try and let you know. Thanks for the fast turn-around!

--Jim L

On Mon, May 18, 2015 at 9:15 PM, Michael Dorman notifications@github.com wrote:

I just uploaded a 0.2.1 release which has cleanup code to attempt a denint. Please let me know if this resolves your issue.

The vboxapi code has a fair few dags like:

if hasattr(self, "vbox"): del self.vbox self.vbox = None

It should be either checking if vbox is None already or performing a setattr(self, 'vbox', None).

— Reply to this email directly or view it on GitHub https://github.com/mjdorma/pyvbox/issues/35#issuecomment-103290090.

jiml521 commented 9 years ago

This worked, btw. On vacation and forgot to respond. Thanks again.

kvalkhof commented 8 years ago

I had the same issue. I moved from win32file import CloseHandle from within PlatformMSCOM::deinit() to a globel location (just before the class definition) to solve it. Hope it is usefull.