tests: Ensure `_process` is set in _TraceTestApplications
authorKienan Stewart <kstewart@efficios.com>
Fri, 9 Feb 2024 14:16:26 +0000 (09:16 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 7 Mar 2024 17:13:53 +0000 (12:13 -0500)
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: <function _TraceTestApplication.__del__ at 0x7fcbc9a21620>
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 <kstewart@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/utils/lttngtest/environment.py

index e51f5eb66da533cc36c6dde3ec2d8e235c7636fd..f0e894a69e14c470c7ac56a0f98b8fc3dab3dad4 100644 (file)
@@ -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()
This page took 0.026699 seconds and 4 git commands to generate.