diff mbox series

[2/8] test-runner: make is_process_running more accurate

Message ID 20220624230741.1957863-2-prestwoj@gmail.com (mailing list archive)
State Accepted, archived
Headers show
Series [1/8] test-runner: remove reference to missing class member | expand

Commit Message

James Prestwood June 24, 2022, 11:07 p.m. UTC
This function was checking if the process object exists, which can
persist long after a process is killed, or dies unexpectedly. Check
that the actual PID exists by sending signal 0.
---
 tools/utils.py | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/tools/utils.py b/tools/utils.py
index 857aa1eb..7272c1f9 100644
--- a/tools/utils.py
+++ b/tools/utils.py
@@ -342,10 +342,24 @@  class Namespace:
 	def stop_process(self, p, force=False):
 		p.kill(force)
 
+	def _is_running(self, pid):
+		try:
+			os.kill(pid, 0)
+		except OSError:
+			return False
+
+		return True
+
 	def is_process_running(self, process):
 		for p in Process.get_all():
-			if p.namespace == self.name and p.args[0] == process:
-				return True
+			# Namespace processes are actually started by 'ip' where
+			# the actual process name is at index 4 of the arguments.
+			idx = 0 if not p.namespace else 4
+
+			if p.namespace == self.name and p.args[idx] == process:
+				# The process object exists, but make sure its
+				# actually running.
+				return self._is_running(p.pid)
 		return False
 
 	def _cleanup_dbus(self):