Fixes cached old WMI service objects issue

When using WMI, the resources passed to the
Msvm_VirtualSystemManagementService are not cleaned up
by the garbage collector if the Service object is still
referenced (cached as a property in BaseUtilsVirt).

PyMI doesn't have this issue, so the Service objects can be
safely cached.

This issue only affects Windows / Hyper-V Server 2012.

Change-Id: If8df0bf0a2f13a9e94993dde8b2a18943f2e9f83
Closes-Bug: #1697389
This commit is contained in:
Claudiu Belu 2017-06-13 16:07:58 +03:00
parent ad2be4739c
commit 49a012e83c
3 changed files with 12 additions and 5 deletions

View File

@ -33,7 +33,6 @@ class MigrationUtilsTestCase(test_base.OsWinBaseTestCase):
self._migrationutils = migrationutils.MigrationUtils()
self._migrationutils._vmutils = mock.MagicMock()
self._migrationutils._conn_attr = mock.MagicMock()
self._migrationutils._compat_conn_attr = mock.MagicMock()
self._migrationutils._jobutils = mock.MagicMock()
def test_get_export_setting_data(self):

View File

@ -95,6 +95,7 @@ class BaseUtilsVirtTestCase(test_base.OsWinBaseTestCase):
mock_os]
expected = self.utils._conn.Msvm_VirtualSystemManagementService()[0]
self.assertEqual(expected, self.utils._vs_man_svc)
self.assertEqual(expected, self.utils._vs_man_svc_attr)
@mock.patch.object(baseutils, 'imp')
@mock.patch.object(baseutils, 'wmi', create=True)
@ -108,6 +109,7 @@ class BaseUtilsVirtTestCase(test_base.OsWinBaseTestCase):
expected = old_conn.Msvm_VirtualSystemManagementService()[0]
self.assertEqual(expected, self.utils._vs_man_svc)
self.assertIsNone(self.utils._vs_man_svc_attr)
mock_imp.load_source.assert_called_once_with(
'old_wmi', '%s.py' % fake_module_path)

View File

@ -91,10 +91,16 @@ class BaseUtilsVirt(BaseUtils):
@property
def _vs_man_svc(self):
if not self._vs_man_svc_attr:
self._vs_man_svc_attr = (
self._compat_conn.Msvm_VirtualSystemManagementService()[0])
return self._vs_man_svc_attr
if self._vs_man_svc_attr:
return self._vs_man_svc_attr
vs_man_svc = self._compat_conn.Msvm_VirtualSystemManagementService()[0]
if BaseUtilsVirt._os_version >= [6, 3]:
# NOTE(claudiub): caching this property on Windows / Hyper-V Server
# 2012 (using the old WMI) can lead to memory leaks. PyMI doesn't
# have those issues, so we can safely cache it.
self._vs_man_svc_attr = vs_man_svc
return vs_man_svc
def _get_wmi_compat_conn(self, moniker, **kwargs):
# old WMI should be used on Windows / Hyper-V Server 2012 whenever