From cebde614e5805e89341ba75d276b4d3e2da7225f Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Fri, 9 Feb 2024 09:16:26 -0500 Subject: [PATCH] tests: Ensure `_process` is set in _TraceTestApplications MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Observed issue ============== An exception is thrown when deleting a _TraceTestApplication object that has thrown an exception during it's `__init__` method. Eg. ``` Exception ignored in: Traceback (most recent call last): File "/home/kstewart/src/efficios/lttng/master/src/lttng-tools/tests/utils/lttngtest/environment.py", line 348, in __del__ self._process.kill() ^^^^^^^^^^^^^ AttributeError: '_TraceTestApplication' object has no attribute '_process' ``` Similarly, this can happen to _WaitTraceTestApplication objects. Cause ===== The object's `_process` attribute is set during `__init__`; however, if an exception is thrown during `subprocess.Popen` a value is never assigned to the attribute. Solution ======== A default value for the `_process` attribute is set and checked as part of the condition when executing the `__del__` method. Known drawbacks =============== None. Change-Id: I2220ae764be49fafb3b977a5e723931421485d63 Signed-off-by: Kienan Stewart Signed-off-by: Jérémie Galarneau --- tests/utils/lttngtest/environment.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/utils/lttngtest/environment.py b/tests/utils/lttngtest/environment.py index e51f5eb66..f0e894a69 100644 --- a/tests/utils/lttngtest/environment.py +++ b/tests/utils/lttngtest/environment.py @@ -93,6 +93,7 @@ class _WaitTraceTestApplication: wait_before_exit=False, # type: bool wait_before_exit_file_path=None, # type: Optional[pathlib.Path] ): + self._process = None self._environment = environment # type: Environment self._iteration_count = event_count # File that the application will wait to see before tracing its events. @@ -223,7 +224,7 @@ class _WaitTraceTestApplication: return str(path) def __del__(self): - if not self._has_returned: + if self._process is not None and not self._has_returned: # This is potentially racy if the pid has been recycled. However, # we can't use pidfd_open since it is only available in python >= 3.9. self._process.kill() @@ -315,6 +316,7 @@ class _TraceTestApplication: def __init__(self, binary_path, environment): # type: (pathlib.Path, Environment) + self._process = None self._environment = environment # type: Environment self._has_returned = False @@ -341,7 +343,7 @@ class _TraceTestApplication: self._has_returned = True def __del__(self): - if not self._has_returned: + if self._process is not None and not self._has_returned: # This is potentially racy if the pid has been recycled. However, # we can't use pidfd_open since it is only available in python >= 3.9. self._process.kill() -- 2.34.1