VirtualBox

Changeset 69574 in vbox


Ignore:
Timestamp:
Nov 4, 2017 8:52:53 AM (7 years ago)
Author:
vboxsync
Message:

testdriver/vbox.py: Extended dangling object logging in _teardownVBoxApi and added COM shutdown when we found no dangling COM interfaces around.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/testdriver/vbox.py

    r69562 r69574  
    14481448        import gc;
    14491449
     1450        # Drop all references we've have to COM objects.
    14501451        self.aoRemoteSessions = [];
    14511452        self.aoVMs            = [];
     
    14551456        self.checkProcessHeap(); ## TEMPORARY
    14561457
     1458        # Do garbage collection to try get rid of those objects.
    14571459        try:
    14581460            gc.collect();
    1459             aObjects = gc.get_objects();
    1460             try:
    1461                 try:                from types import InstanceType; # For old-style python 2.x objects, not in python 3.
    1462                 except ImportError: InstanceType = None;
    1463                 for oObj in aObjects:
    1464                     oObjType = type(oObj)
    1465                     if oObjType == InstanceType: # Python 2.x codepath
    1466                         oObjType = oObj.__class__
    1467                     if oObjType.__name__ == 'VirtualBoxManager':
    1468                         reporter.log('actionCleanupAfter: CAUTION, there is still a VirtualBoxManager object, GC trouble')
    1469                         break
    1470             finally:
    1471                 del aObjects;
    14721461        except:
    14731462            reporter.logXcpt();
     
    14751464        self.checkProcessHeap(); ## TEMPORARY
    14761465
     1466        # Check whether the python is still having any COM objects/interfaces around.
     1467        cVBoxMgrs = 0;
     1468        aoObjsLeftBehind = [];
    14771469        if self.sHost == 'win':
     1470            import pythoncom;                                   # pylint: disable=import-error
    14781471            try:
    1479                 import pythoncom;                       # pylint: disable=import-error
    1480                 cIfs  = pythoncom._GetInterfaceCount(); # pylint: disable=no-member,protected-access
    1481                 cObjs = pythoncom._GetGatewayCount();   # pylint: disable=no-member,protected-access
     1472                cIfs  = pythoncom._GetInterfaceCount();         # pylint: disable=no-member,protected-access
     1473                cObjs = pythoncom._GetGatewayCount();           # pylint: disable=no-member,protected-access
    14821474                if cObjs == 0 and cIfs == 0:
    1483                     reporter.log('actionCleanupAfter: no interfaces or objects left behind.');
     1475                    reporter.log('_teardownVBoxApi: no interfaces or objects left behind.');
    14841476                else:
    1485                     reporter.log('actionCleanupAfter: Python COM still has %s objects and %s interfaces...' % ( cObjs, cIfs));
    1486                     from win32com.client import DispatchBaseClass; # pylint: disable=import-error
    1487                     for oObj in gc.get_objects():
    1488                         if isinstance(oObj, DispatchBaseClass):
    1489                             reporter.log('actionCleanupAfter:   %s' % (oObj,));
    1490                     oObj = None;
     1477                    reporter.log('_teardownVBoxApi: Python COM still has %s objects and %s interfaces...' % ( cObjs, cIfs));
     1478
     1479                from win32com.client import DispatchBaseClass;  # pylint: disable=import-error
     1480                for oObj in gc.get_objects():
     1481                    if isinstance(oObj, DispatchBaseClass):
     1482                        reporter.log('_teardownVBoxApi:   %s' % (oObj,));
     1483                        aoObjsLeftBehind.append(oObj);
     1484                    elif utils.getObjectTypeName(oObj) == 'VirtualBoxManager':
     1485                        reporter.log('_teardownVBoxApi:   %s' % (oObj,));
     1486                        cVBoxMgrs += 1;
     1487                        aoObjsLeftBehind.append(oObj);
     1488                oObj = None;
    14911489            except:
    14921490                reporter.logXcpt();
     1491
     1492            # If not being used, we can safely uninitialize COM.
     1493            if cIfs == 0 and cObjs == 0 and cVBoxMgrs == 0 and len(aoObjsLeftBehind) == 0:
     1494                reporter.log('_teardownVBoxApi:   Calling CoUninitialize...');
     1495                try:    pythoncom.CoUninitialize();             # pylint: disable=no-member
     1496                except: reporter.logXcpt();
     1497                else:
     1498                    reporter.log('_teardownVBoxApi:   Returned from CoUninitialize.');
    14931499        else:
    14941500            try:
    1495                 from xpcom import _xpcom as _xpcom;     # pylint: disable=import-error
     1501                from xpcom import _xpcom as _xpcom;             # pylint: disable=import-error
    14961502                hrc   = _xpcom.NS_ShutdownXPCOM();
    1497                 cIfs  = _xpcom._GetInterfaceCount();    # pylint: disable=W0212
    1498                 cObjs = _xpcom._GetGatewayCount();      # pylint: disable=W0212
     1503                cIfs  = _xpcom._GetInterfaceCount();            # pylint: disable=W0212
     1504                cObjs = _xpcom._GetGatewayCount();              # pylint: disable=W0212
     1505
    14991506                if cObjs == 0 and cIfs == 0:
    1500                     reporter.log('actionCleanupAfter: NS_ShutdownXPCOM -> %s, nothing left behind.' % (hrc, ));
     1507                    reporter.log('_teardownVBoxApi: NS_ShutdownXPCOM -> %s, nothing left behind.' % (hrc, ));
    15011508                else:
    1502                     reporter.log('actionCleanupAfter: NS_ShutdownXPCOM -> %s, leaving %s objects and %s interfaces behind...' \
     1509                    reporter.log('_teardownVBoxApi: NS_ShutdownXPCOM -> %s, leaving %s objects and %s interfaces behind...'
    15031510                                 % (hrc, cObjs, cIfs));
    15041511                    if hasattr(_xpcom, '_DumpInterfaces'):
    1505                         try:
    1506                             _xpcom._DumpInterfaces();   # pylint: disable=W0212
    1507                         except:
    1508                             reporter.logXcpt('actionCleanupAfter: _DumpInterfaces failed');
     1512                        try:    _xpcom._DumpInterfaces();       # pylint: disable=W0212
     1513                        except: reporter.logXcpt('_teardownVBoxApi: _DumpInterfaces failed');
     1514
     1515                from xpcom.client import Component;             # pylint: disable=import-error
     1516                for oObj in gc.get_objects():
     1517                    if isinstance(oObj, Component):
     1518                        reporter.log('_teardownVBoxApi:   %s' % (oObj,));
     1519                        aoObjsLeftBehind.append(oObj);
     1520                    if utils.getObjectTypeName(oObj) == 'VirtualBoxManager':
     1521                        reporter.log('_teardownVBoxApi:   %s' % (oObj,));
     1522                        cVBoxMgrs += 1;
     1523                        aoObjsLeftBehind.append(oObj);
     1524                oObj = None;
     1525
    15091526            except:
    15101527                reporter.logXcpt();
    15111528        self.checkProcessHeap(); ## TEMPORARY
    15121529
     1530        # Try get the referrers to (XP)COM interfaces and objects that was left behind.
     1531        for iObj in range(len(aoObjsLeftBehind)): # pylint: disable=consider-using-enumerate
     1532            try:
     1533                aoReferrers = gc.get_referrers(aoObjsLeftBehind[iObj]);
     1534                reporter.log('_teardownVBoxApi:   Found %u referrers to %s:' % (len(aoReferrers), aoObjsLeftBehind[iObj],));
     1535                for oReferrer in aoReferrers:
     1536                    oMyFrame = sys._getframe(0);  # pylint: disable=protected-access
     1537                    if oReferrer is oMyFrame:
     1538                        reporter.log('_teardownVBoxApi:     - frame of this function');
     1539                    elif oReferrer is aoObjsLeftBehind:
     1540                        reporter.log('_teardownVBoxApi:     - aoObjsLeftBehind');
     1541                    else:
     1542                        fPrinted = False;
     1543                        if isinstance(oReferrer, dict) or isinstance(oReferrer, list) or isinstance(oReferrer, tuple):
     1544                            try:
     1545                                aoSubReferreres = gc.get_referrers(oReferrer);
     1546                                for oSubRef in aoSubReferreres:
     1547                                    if    not isinstance(oSubRef, list) \
     1548                                      and not isinstance(oSubRef, dict) \
     1549                                      and oSubRef is not oMyFrame \
     1550                                      and oSubRef is not aoSubReferreres:
     1551                                        reporter.log('_teardownVBoxApi:     - %s :: %s:'
     1552                                                     % (utils.getObjectTypeName(oSubRef), utils.getObjectTypeName(oReferrer)));
     1553                                        fPrinted = True;
     1554                                        break;
     1555                                del aoSubReferreres;
     1556                            except:
     1557                                reporter.logXcpt('subref');
     1558                        if not fPrinted:
     1559                            reporter.log('_teardownVBoxApi:     - %s:' % (utils.getObjectTypeName(oReferrer),));
     1560                        try:
     1561                            import pprint;
     1562                            for sLine in pprint.pformat(oReferrer, width = 130).split('\n'):
     1563                                reporter.log('_teardownVBoxApi:       %s' % (sLine,));
     1564                        except:
     1565                            reporter.log('_teardownVBoxApi:       %s' % (oReferrer,));
     1566            except:
     1567                reporter.logXcpt();
     1568        del aoObjsLeftBehind;
     1569
     1570        # Force garbage collection again, just for good measure.
    15131571        try:
    15141572            gc.collect();
    1515             time.sleep(0.5); # fudge factory
     1573            time.sleep(0.5); # fudge factor
    15161574        except:
    15171575            reporter.logXcpt();
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette