[PATCH stalld 08/36] tests/helpers: Fix stop_stalld() timeout and shutdown logic

Wander Lairson Costa <[email protected]>
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
stop_stalld() has two bugs. The polling loop after SIGKILL uses
sleep 0.1 with an integer counter, so the nominal timeout of 10
actually gives only 1 second. Additionally, SIGTERM only gets
0.2 seconds before escalating to SIGKILL, which is insufficient
for stalld to perform graceful cleanup such as removing its
pidfile or flushing logs.

Restructure stop_stalld() into two clear phases. First send
SIGTERM and poll for graceful exit with 1-second intervals for
up to 5 seconds, giving stalld time to clean up. Then escalate
to SIGKILL only if the process is still alive, and poll again
for up to 5 seconds with correct timeout arithmetic. Document
the guarantee that the process is dead before the function
returns so callers do not need post-stop sleeps.

Signed-off-by: Wander Lairson Costa <[email protected]>
---
 tests/helpers/test_helpers.sh | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/tests/helpers/test_helpers.sh b/tests/helpers/test_helpers.sh
index f6d6ce2..1750423 100755
--- a/tests/helpers/test_helpers.sh
+++ b/tests/helpers/test_helpers.sh
@@ -365,24 +365,33 @@ start_stalld() {
 }
 
 # Stop stalld
+# Sends SIGTERM and polls for graceful exit, then escalates to
+# SIGKILL if needed. Guarantees the process is dead before
+# returning so callers do not need post-stop sleeps.
 stop_stalld() {
 	if [ -n "${STALLD_PID}" ]; then
 		if kill -0 ${STALLD_PID} 2>/dev/null; then
-			# Try graceful shutdown first
+			# Try graceful shutdown first (SIGTERM)
 			kill ${STALLD_PID} 2>/dev/null || true
-			# Give it a moment to exit gracefully
-			sleep 0.2
-			# Force kill if still running
-			if kill -0 ${STALLD_PID} 2>/dev/null; then
-				kill -9 ${STALLD_PID} 2>/dev/null || true
-			fi
-			# Poll for process termination (don't use wait - might not be a child)
-			local timeout=10
+
+			# Poll for graceful exit (up to 5 seconds)
+			local timeout=5
 			local elapsed=0
 			while kill -0 ${STALLD_PID} 2>/dev/null && [ ${elapsed} -lt ${timeout} ]; do
-				sleep 0.1
+				sleep 1
 				elapsed=$((elapsed + 1))
 			done
+
+			# Escalate to SIGKILL if still running
+			if kill -0 ${STALLD_PID} 2>/dev/null; then
+				kill -9 ${STALLD_PID} 2>/dev/null || true
+				# Poll for forced termination (up to 5 seconds)
+				elapsed=0
+				while kill -0 ${STALLD_PID} 2>/dev/null && [ ${elapsed} -lt ${timeout} ]; do
+					sleep 1
+					elapsed=$((elapsed + 1))
+				done
+			fi
 		fi
 		STALLD_PID=""
 	fi
-- 
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.