[PATCH stalld 01/36] tests: Add pre-test and post-test cleanup of stalld processes

Wander Lairson Costa <[email protected]>
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
From: Clark Williams <[email protected]>

Add kill_existing_stalld() function to test_helpers.sh and
kill_existing_stalld_processes() function to run_tests.sh to ensure
no stale stalld processes from previous test runs interfere with new
tests. This prevents orphaned processes from interrupted test runs
from causing false negatives or unexpected behavior.

Changes:
- test_helpers.sh: Add kill_existing_stalld() with graceful then
  forced shutdown, export the function, and call from
  setup_test_environment()
- run_tests.sh: Add kill_existing_stalld_processes() with logging,
  call from init_tests() before tests start, and call from
  cleanup_runner() on exit

This ensures clean test environment initialization and proper cleanup
even when test runs are interrupted.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Assisted-By: git-scm-master (claude-sonnet-4 / 36f1d1f06f2e)
Signed-off-by: Clark Williams <[email protected]>
Signed-off-by: Wander Lairson Costa <[email protected]>
---
 tests/helpers/test_helpers.sh | 35 ++++++++++++++++++++++++++++-
 tests/run_tests.sh            | 42 +++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/tests/helpers/test_helpers.sh b/tests/helpers/test_helpers.sh
index 630601a..c42dab3 100755
--- a/tests/helpers/test_helpers.sh
+++ b/tests/helpers/test_helpers.sh
@@ -407,6 +407,36 @@ stop_stalld() {
 	fi
 }
 
+# Kill any existing stalld processes (cleanup from previous runs)
+# This ensures a clean slate before starting tests
+kill_existing_stalld() {
+	local pids=$(pgrep -x stalld 2>/dev/null)
+	if [ -n "${pids}" ]; then
+		echo "Killing existing stalld processes: ${pids}"
+		for pid in ${pids}; do
+			# Try graceful shutdown first
+			kill ${pid} 2>/dev/null || true
+		done
+		sleep 0.5
+		# Force kill any remaining
+		pids=$(pgrep -x stalld 2>/dev/null)
+		if [ -n "${pids}" ]; then
+			for pid in ${pids}; do
+				kill -9 ${pid} 2>/dev/null || true
+			done
+			sleep 0.2
+		fi
+		# Verify all killed
+		pids=$(pgrep -x stalld 2>/dev/null)
+		if [ -n "${pids}" ]; then
+			echo -e "${YELLOW}WARNING: Could not kill all stalld processes: ${pids}${NC}"
+			return 1
+		fi
+		echo "All existing stalld processes killed"
+	fi
+	return 0
+}
+
 # Cleanup function (call in trap)
 cleanup() {
 	local exit_code=$?
@@ -712,6 +742,9 @@ disable_dl_server() {
 setup_test_environment() {
 	echo "Setting up test environment..."
 
+	# Kill any existing stalld processes from previous runs
+	kill_existing_stalld
+
 	# Save and disable RT throttling
 	save_rt_throttling
 	disable_rt_throttling
@@ -1022,7 +1055,7 @@ export -f start_test end_test
 export -f assert_equals assert_contains assert_not_contains
 export -f assert_file_exists assert_file_not_exists
 export -f assert_process_running assert_process_not_running
-export -f start_stalld stop_stalld cleanup
+export -f start_stalld stop_stalld kill_existing_stalld cleanup
 export -f wait_for_log_message
 export -f get_thread_policy get_thread_priority
 export -f create_cpu_load
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index c6adc49..16b6750 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -220,6 +220,15 @@ cleanup_runner() {
 	fi
 
 	if [ $EUID -eq 0 ]; then
+		# Kill any remaining stalld processes
+		local pids=$(pgrep -x stalld 2>/dev/null)
+		if [ -n "${pids}" ]; then
+			echo -e "${BLUE}Cleaning up remaining stalld processes: ${pids}${NC}"
+			for pid in ${pids}; do
+				kill -9 ${pid} 2>/dev/null || true
+			done
+		fi
+
 		restore_dl_server_state
 		restore_rt_throttling_state
 	fi
@@ -236,6 +245,34 @@ handle_interrupt() {
 trap cleanup_runner EXIT
 trap handle_interrupt INT TERM
 
+# Kill any existing stalld processes before tests
+kill_existing_stalld_processes() {
+	local pids=$(pgrep -x stalld 2>/dev/null)
+	if [ -n "${pids}" ]; then
+		echo -e "${BLUE}Killing existing stalld processes: ${pids}${NC}" | tee -a "${LOG_FILE}"
+		for pid in ${pids}; do
+			kill ${pid} 2>/dev/null || true
+		done
+		sleep 0.5
+		# Force kill any remaining
+		pids=$(pgrep -x stalld 2>/dev/null)
+		if [ -n "${pids}" ]; then
+			for pid in ${pids}; do
+				kill -9 ${pid} 2>/dev/null || true
+			done
+			sleep 0.2
+		fi
+		# Verify
+		pids=$(pgrep -x stalld 2>/dev/null)
+		if [ -n "${pids}" ]; then
+			echo -e "${YELLOW}WARNING: Could not kill all stalld processes: ${pids}${NC}" | tee -a "${LOG_FILE}"
+		else
+			echo -e "${GREEN}All existing stalld processes killed${NC}" | tee -a "${LOG_FILE}"
+		fi
+		echo "" | tee -a "${LOG_FILE}"
+	fi
+}
+
 # Initialize
 init_tests() {
 	mkdir -p "${RESULTS_DIR}"
@@ -243,6 +280,11 @@ init_tests() {
 	print_banner | tee "${LOG_FILE}"
 	echo "" | tee -a "${LOG_FILE}"
 
+	# Kill any existing stalld processes from previous runs
+	if [ $EUID -eq 0 ]; then
+		kill_existing_stalld_processes
+	fi
+
 	# Save and disable RT throttling if running as root
 	if [ $EUID -eq 0 ]; then
 		save_and_disable_rt_throttling
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.