ext/admin: workaround for extension's __init__() called multiple times

... during tests.
qubes.ext.Extension class is a weird thing that tries to make each extension
a singleton. But this unfortunately have a side effect that __init__()
is called separately for each "instance" (created in Qubes()'s
__init__()), even though this is really the same object. During normal
execution this isn't an issue, because there is just one Qubes() object
instance. But during tests, multiple objects are created.

In this particular case, it caused PolicyCache() to be created twice and
the second one overriden the first one - without properly cleaning it
up. This leaks a file descriptor (inotify one). The fact that cleanup()
was called twice too didn't helped, because it was really called on
the same object, the one requiring cleanup was already gone.

Workaround this by checking if policy_cache field is initialize and
avoid re-initialize it. Also, on Qubes() object cleanup remove that
field, so it can be properly initialized on the next test iteration.
This commit is contained in:
Marek Marczykowski-Górecki 2020-03-31 01:57:22 +02:00
parent 0341cc5258
commit b11d6e058b
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -37,6 +37,9 @@ class JustEvaluateAllowResolution(parser.AllowResolution):
class AdminExtension(qubes.ext.Extension):
def __init__(self):
super(AdminExtension, self).__init__()
# during tests, __init__() of the extension can be called multiple
# times, because there are multiple Qubes() object instances
if not hasattr(self, 'policy_cache'):
self.policy_cache = utils.PolicyCache(lazy_load=True)
self.policy_cache.initialize_watcher()
@ -136,4 +139,6 @@ class AdminExtension(qubes.ext.Extension):
def on_qubes_close(self, app, event, **kwargs):
"""Unregister policy file watches on app.close()."""
# pylint: disable=unused-argument
if hasattr(self, 'policy_cache'):
self.policy_cache.cleanup()
del self.policy_cache