Skip to contentSkip to conversation

test_children_cleanup.sh: Fix race condition

PATCH In review Martin Doucha 3 participants 4 messages Feb 15, 2022 – Feb 16, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 test_children_cleanup.sh: Fix race condition

Processes can stay alive for a short while even after receiving SIGKILL. Give the child in subprocess cleanup libtest up to 5 seconds to fully exit or change state to zombie before reporting that it was left behind.

---

Changes since v1: Report success even if the child gets stuck in zombie state

 lib/newlib_tests/test_children_cleanup.sh | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
lib/newlib_tests/test_children_cleanup.sh +17 -6
index 4b4e8b2f0..23408c1bc 100755
@@ -10,10 +10,21 @@ rm "$TMPFILE"
 if [ "x$CHILD_PID" = "x" ]; then
 	echo "TFAIL: Child process was not created"
 	exit 1
-elif ! kill -s 0 $CHILD_PID &>/dev/null; then
-	echo "TPASS: Child process was cleaned up"
-	exit 0
-else
-	echo "TFAIL: Child process was left behind"
-	exit 1
 fi
+
+# The child process can stay alive for a short while even after receiving
+# SIGKILL, especially if the system is under heavy load. Wait up to 5 seconds
+# for it to fully exit.
+for i in `seq 6`; do
+	CHILD_STATE=`sed -ne 's/^State:\s*\([A-Z]\).*$/\1/p' "/proc/$CHILD_PID/status" 2>/dev/null`
+
+	if [ ! -e "/proc/$CHILD_PID" ] || [ "x$CHILD_STATE" = "xZ" ]; then
+		echo "TPASS: Child process was cleaned up"
+		exit 0
+	fi
+
+	sleep 1
+done
+
+echo "TFAIL: Child process was left behind"
+exit 1
Signature · 5 lines
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Martin Doucha

Processes can stay alive for a short while even after receiving SIGKILL. Give the child in subprocess cleanup libtest up to 5 seconds to fully exit or change state to zombie before reporting that it was left behind.

Quoted 47 lines

---

Changes since v1: Report success even if the child gets stuck in zombie state

 lib/newlib_tests/test_children_cleanup.sh | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
lib/newlib_tests/test_children_cleanup.sh +3 -6
index 4b4e8b2f0..23408c1bc 100755
@@ -10,10 +10,21 @@ rm "$TMPFILE"
 if [ "x$CHILD_PID" = "x" ]; then
        echo "TFAIL: Child process was not created"
        exit 1
-elif ! kill -s 0 $CHILD_PID &>/dev/null; then
-       echo "TPASS: Child process was cleaned up"
-       exit 0
-else
-       echo "TFAIL: Child process was left behind"
-       exit 1
 fi
+
+# The child process can stay alive for a short while even after receiving
+# SIGKILL, especially if the system is under heavy load. Wait up to 5

seconds

lib/newlib_tests/test_children_cleanup.sh +3 -0
+# for it to fully exit.
+for i in `seq 6`; do
+       CHILD_STATE=`sed -ne 's/^State:\s*\([A-Z]\).*$/\1/p'

"/proc/$CHILD_PID/status" 2>/dev/null`

lib/newlib_tests/test_children_cleanup.sh +11 -1
+
+       if [ ! -e "/proc/$CHILD_PID" ] || [ "x$CHILD_STATE" = "xZ" ]; then
+               echo "TPASS: Child process was cleaned up"
+               exit 0
+       fi
+
+       sleep 1
+done
+
+echo "TFAIL: Child process was left behind"
+exit 1
--

2.34.1

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 1/1
#2 lore raw
In reply to Martin Doucha

Hi!

Quoted 25 lines

Processes can stay alive for a short while even after receiving SIGKILL. Give the child in subprocess cleanup libtest up to 5 seconds to fully exit or change state to zombie before reporting that it was left behind.

---

Changes since v1: Report success even if the child gets stuck in zombie state

 lib/newlib_tests/test_children_cleanup.sh | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
lib/newlib_tests/test_children_cleanup.sh +0 -5
index 4b4e8b2f0..23408c1bc 100755
@@ -10,10 +10,21 @@ rm "$TMPFILE"
 if [ "x$CHILD_PID" = "x" ]; then
 	echo "TFAIL: Child process was not created"
 	exit 1
-elif ! kill -s 0 $CHILD_PID &>/dev/null; then
-	echo "TPASS: Child process was cleaned up"
-	exit 0
-else
-	echo "TFAIL: Child process was left behind"

lib/newlib_tests/test_children_cleanup.sh rm "$TMPFILE" · line 21

Quoted 10 lines
lib/newlib_tests/test_children_cleanup.sh +8 -1
-	exit 1
 fi
+
+# The child process can stay alive for a short while even after receiving
+# SIGKILL, especially if the system is under heavy load. Wait up to 5 seconds
+# for it to fully exit.
+for i in `seq 6`; do
+	CHILD_STATE=`sed -ne 's/^State:\s*\([A-Z]\).*$/\1/p' "/proc/$CHILD_PID/status" 2>/dev/null`
+
+	if [ ! -e "/proc/$CHILD_PID" ] || [ "x$CHILD_STATE" = "xZ" ]; then

As long as we have the variable inside the quotes there is no point in adding the 'x'.

Other than that:

Quoted 9 lines
lib/newlib_tests/test_children_cleanup.sh +9 -0
+		echo "TPASS: Child process was cleaned up"
+		exit 0
+	fi
+
+	sleep 1
+done
+
+echo "TFAIL: Child process was left behind"
+exit 1
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 40 lines · Cyril Hrubis

Hi!

Quoted 2 lines

Processes can stay alive for a short while even after receiving SIGKILL. Give the child in subprocess cleanup libtest up to 5 seconds to fully

exit

Quoted 6 lines

or change state to zombie before reporting that it was left behind.

---

Changes since v1: Report success even if the child gets stuck in zombie

state

Review

Quoted 5 lines
 lib/newlib_tests/test_children_cleanup.sh | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/lib/newlib_tests/test_children_cleanup.sh

Quoted 6 lines

index 4b4e8b2f0..23408c1bc 100755

lib/newlib_tests/test_children_cleanup.sh +0 -0
@@ -10,10 +10,21 @@ rm "$TMPFILE"
 if [ "x$CHILD_PID" = "x" ]; then
      echo "TFAIL: Child process was not created"

lib/newlib_tests/test_children_cleanup.sh rm "$TMPFILE" · line 15

Quoted 10 lines
lib/newlib_tests/test_children_cleanup.sh +2 -6
      exit 1
-elif ! kill -s 0 $CHILD_PID &>/dev/null; then
-     echo "TPASS: Child process was cleaned up"
-     exit 0
-else
-     echo "TFAIL: Child process was left behind"
-     exit 1
 fi
+
+# The child process can stay alive for a short while even after

receiving

lib/newlib_tests/test_children_cleanup.sh rm "$TMPFILE" · line 16

Quoted 1 line
lib/newlib_tests/test_children_cleanup.sh +1 -0
+# SIGKILL, especially if the system is under heavy load. Wait up to 5

seconds

lib/newlib_tests/test_children_cleanup.sh rm "$TMPFILE"

Quoted 3 lines
lib/newlib_tests/test_children_cleanup.sh +3 -0
+# for it to fully exit.
+for i in `seq 6`; do
+     CHILD_STATE=`sed -ne 's/^State:\s*\([A-Z]\).*$/\1/p'

"/proc/$CHILD_PID/status" 2>/dev/null`

Quoted 6 lines

lib/newlib_tests/test_children_cleanup.sh rm "$TMPFILE" · line 21

Quoted 2 lines
lib/newlib_tests/test_children_cleanup.sh +2 -0
+
+     if [ ! -e "/proc/$CHILD_PID" ] || [ "x$CHILD_STATE" = "xZ" ]; then

As long as we have the variable inside the quotes there is no point in adding the 'x'.

Pushed with a note added and this fix. Thanks~

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp