Skip to contentSkip to conversation

shell: Prevent orphan timeout sleep processes

PATCH New Li Wang 2 participants 2 messages May 8, 2021 – May 10, 2021

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v3 2/4 shell: Prevent orphan timeout sleep processes

From: Joerg Vehlow <[email protected]>

The implementation of the timeout handling failed to terminate the sleep process, because the command "sleep $sec && _tst_kill_test &" spawns two
processes: A shell, that is used to execute the sleep and after sleep terminates it executes _tst_kill_test. The pid stored in $! is the pid of the shell process.
When the test finished, the timeout process is supposed to be killed, but only the shell process is killed, not the sleep. If the output of the test is piped somewhere else, the pipe stays open, until the sleep finished, because it still has stdout open.
Additionally, if a lot of short tests with high timeouts are executed, a lot of sleep processes will linger around.

---
 lib/newlib_tests/shell/timeout03.sh |  4 ++--
 lib/newlib_tests/shell/timeout04.sh |  2 +-
 testcases/lib/tst_test.sh           | 30 ++++++++++++++++++++---------
 3 files changed, 24 insertions(+), 12 deletions(-)
lib/newlib_tests/shell/timeout03.sh +2 -2
index 30aa0a149..cd548d9a2 100755
@@ -32,7 +32,7 @@ do_test()
 {
 	tst_res TINFO "testing killing test after TST_TIMEOUT"
 
-	tst_sleep 2
+	sleep 2
 	tst_res TFAIL "test: running after TST_TIMEOUT"
 }
 
@@ -40,7 +40,7 @@ cleanup()
 {
 	tst_res TPASS "test run cleanup after timeout"
 
-	tst_sleep 15 # must be higher than wait time in _tst_kill_test
+	sleep 15 # must be higher than wait time in _tst_kill_test
 	tst_res TFAIL "cleanup: running after TST_TIMEOUT"
 }
 
lib/newlib_tests/shell/timeout04.sh +1 -1
index 0a6ba053c..c702905f3 100755
@@ -9,7 +9,7 @@ TST_TIMEOUT=1
 
 do_test()
 {
-	tst_res TINFO "Start"
+    tst_res TINFO "Start"
     sleep 5
     tst_res TFAIL "End"
 }
testcases/lib/tst_test.sh +21 -9
index 951518785..ed2699175 100644
@@ -23,14 +23,6 @@ export TST_LIB_LOADED=1
 # default trap function
 trap "tst_brk TBROK 'test interrupted or timed out'" INT
 
-_tst_cleanup_timer()
-{
-	if [ -n "$_tst_setup_timer_pid" ]; then
-		kill $_tst_setup_timer_pid 2>/dev/null
-		wait $_tst_setup_timer_pid 2>/dev/null
-	fi
-}
-
 _tst_do_exit()
 {
 	local ret=0
@@ -463,6 +455,25 @@ _tst_kill_test()
 	fi
 }
 
+_tst_cleanup_timer()
+{
+	if [ -n "$_tst_setup_timer_pid" ]; then
+		kill -TERM $_tst_setup_timer_pid 2>/dev/null
+		wait $_tst_setup_timer_pid 2>/dev/null
+	fi
+}
+
+_tst_timeout_process()
+{
+	local sleep_pid
+
+	sleep $sec &
+	sleep_pid=$!
+	trap "kill $sleep_pid; exit" TERM
+	wait $sleep_pid
+	_tst_kill_test
+}
+
 _tst_setup_timer()
 {
 	TST_TIMEOUT=${TST_TIMEOUT:-300}
@@ -486,7 +497,8 @@ _tst_setup_timer()
 	tst_res TINFO "timeout per run is ${h}h ${m}m ${s}s"
 
 	_tst_cleanup_timer
-	sleep $sec && _tst_kill_test &
+
+	_tst_timeout_process &
 
 	_tst_setup_timer_pid=$!
 }
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 2/4 shell: Prevent orphan timeout sleep processes

Hi!

lib/newlib_tests/shell/timeout04.sh TST_TIMEOUT=1 · line 15

Quoted 11 lines
lib/newlib_tests/shell/timeout04.sh +1 -1
@@ -9,7 +9,7 @@ TST_TIMEOUT=1
 
 do_test()
 {
-	tst_res TINFO "Start"
+    tst_res TINFO "Start"
     sleep 5
     tst_res TFAIL "End"
 }

This should be really fixed in the previous patch rather than here.

Other than this the rest looks good.

With the indentation fix moved to previous patch:

Quoted 61 lines
testcases/lib/tst_test.sh +21 -9
index 951518785..ed2699175 100644
@@ -23,14 +23,6 @@ export TST_LIB_LOADED=1
 # default trap function
 trap "tst_brk TBROK 'test interrupted or timed out'" INT
 
-_tst_cleanup_timer()
-{
-	if [ -n "$_tst_setup_timer_pid" ]; then
-		kill $_tst_setup_timer_pid 2>/dev/null
-		wait $_tst_setup_timer_pid 2>/dev/null
-	fi
-}
-
 _tst_do_exit()
 {
 	local ret=0
@@ -463,6 +455,25 @@ _tst_kill_test()
 	fi
 }
 
+_tst_cleanup_timer()
+{
+	if [ -n "$_tst_setup_timer_pid" ]; then
+		kill -TERM $_tst_setup_timer_pid 2>/dev/null
+		wait $_tst_setup_timer_pid 2>/dev/null
+	fi
+}
+
+_tst_timeout_process()
+{
+	local sleep_pid
+
+	sleep $sec &
+	sleep_pid=$!
+	trap "kill $sleep_pid; exit" TERM
+	wait $sleep_pid
+	_tst_kill_test
+}
+
 _tst_setup_timer()
 {
 	TST_TIMEOUT=${TST_TIMEOUT:-300}
@@ -486,7 +497,8 @@ _tst_setup_timer()
 	tst_res TINFO "timeout per run is ${h}h ${m}m ${s}s"
 
 	_tst_cleanup_timer
-	sleep $sec && _tst_kill_test &
+
+	_tst_timeout_process &
 
 	_tst_setup_timer_pid=$!
 }
Signature · 5 lines
2.30.2


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]