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

Win32 exception occurred releasing IUnknown at 0xe3e0ebc8 #114

Closed JerryLui closed 1 year ago

JerryLui commented 6 years ago

I found a similiar issue reported https://github.com/SethMichaelLarson/virtualbox-python/issues/52 but I wasn't able to reproduce that issue. Instead I found the error when running vbox in a thread.

ENVIRONMENT
SUMMARY

During the second iteration of the while loop, where the th variable gets reassigned, the python app crashes and produces a Win32 exception. The crash seems to stem from release and reassignment of the self.session and self.vbox variables.

STEPS TO REPRODUCE
import virtualbox
import threading
import time

class ThreadExecutor(threading.Thread):
    def __init__(self):
        self.vbox = None
        self.session = None
        self.vm = None
        super().__init__()

    def run(self):
        self.vbox = virtualbox.VirtualBox()
        self.session = virtualbox.Session()
        self.vm = self.vbox.find_machine("Ubuntu")
        self.vm.launch_vm_process(self.session, 'gui', '')

        time.sleep(30)
        if int(self.session.state) == 1:
            print('Boot failed!')
            return
        else:
            print('Powering down')
            self.session.console.power_down()
        print('Operation completed')
        return

if __name__ == '__main__':
    while True:
        print('Input')
        if input():
            th = ThreadExecutor()
            th.start()
            print('Thread started')

            time.sleep(5)
            while th.isAlive():
                print('App running')
                time.sleep(5)
            print('Execution finished')
EXPECTED RESULTS

New vbox and sessions should be able to start after powering down the first session.

ACTUAL RESULTS
Win32 exception occurred releasing IUnknown at 0x035e40b8
Win32 exception occurred releasing IUnknown at 0x04babcb0
sethmlarson commented 6 years ago

I think I've seen this before too, I was unable to figure out why. The only thing I can think of is objects being de-allocated and threading not playing nice together.

JerryLui commented 6 years ago

Yeah, there seems to be some issues with threading and virtualbox. I get the following exception when trying to run PyCharm debugger or Flask from console on the same code.

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\vboxapi\__init__.py", line 449, in __init__
    None)
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/JerryL/Documents/Aptiv/Python/VASP/grand_compile_auto/dev/issue_oXctp_not_defined.py", line 20, in run
    self.vbox = virtualbox.VirtualBox()
  File "C:\Program Files\Python36\lib\site-packages\virtualbox\library_ext\vbox.py", line 22, in __init__
    manager = virtualbox.Manager()
  File "C:\Program Files\Python36\lib\site-packages\virtualbox\__init__.py", line 143, in __init__
    self.manager = vboxapi.VirtualBoxManager(mtype, mparams)
  File "C:\Program Files\Python36\lib\site-packages\vboxapi\__init__.py", line 991, in __init__
    self.platform = PlatformMSCOM(dPlatformParams)
  File "C:\Program Files\Python36\lib\site-packages\vboxapi\__init__.py", line 455, in __init__
    print("Warning: CoInitializeSecurity failed: ", oXctp);
NameError: name 'oXctp' is not defined
sethmlarson commented 1 year ago

I no longer have time to maintain this library, so am closing this issue.