Skip to contentSkip to conversation

lib: shell: Fix timeout process races

PATCH In review Cyril Hrubis 3 participants 4 messages Sep 22, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 lib: shell: Fix timeout process races

There were actually several races in the shell library timeout handling.

This commit fixes hopefully all of them by:

* Reimplementing the backgroud timer in C

* Making sure that the timer has started before we leave test setup

The rewrite of the backround timer to C allows us to run all the timeout logic in a single process, which simplifies the whole problem greatly since previously we had chain of processes that estabilished signal handlers to kill it's descendants, which in the end had a few races in it.

The race that caused the problems is, as far as I can tell, in the way how shell spawns it's children. I haven't checked the shell code, but I guess that when shell runs a process in bacground it does vfork() + exec(). My guess that signals are disabled between vfork() and exec() and if the SIGTERM arrives at that point it gets lost.

That means that we created a race window in the shell library each time we started a new process. The rewrite to C simplifies the code but we still end up with a single place where this can happen and that is when we execute the tst_timeout_kill binary. This is now fixed in the shell library by waiting until the background process gets to a sleep state.

---
 testcases/lib/.gitignore         |  1 +
 testcases/lib/Makefile           |  2 +-
 testcases/lib/tst_test.sh        | 47 ++++++-------------
 testcases/lib/tst_timeout_kill.c | 79 ++++++++++++++++++++++++++++++++
 4 files changed, 94 insertions(+), 35 deletions(-)
 create mode 100644 testcases/lib/tst_timeout_kill.c

v3:
add setpgid(0, 0) to the tst_timeout_kill so that it does not kill itself when sending signal to the whole process group

testcases/lib/.gitignore +1 -0
index 5a0e8cba2..9625d9043 100644
@@ -14,3 +14,4 @@
 /tst_sleep
 /tst_supported_fs
 /tst_hexdump
+/tst_timeout_kill
testcases/lib/Makefile +1 -1
index 179b47479..d6b4c7a91 100644
@@ -11,6 +11,6 @@ INSTALL_TARGETS		:= *.sh
 MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
 			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
 			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
-			   tst_get_median tst_hexdump tst_get_free_pids
+			   tst_get_median tst_hexdump tst_get_free_pids tst_timeout_kill
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tst_test.sh +13 -34

67 lines not on this page · raw message

testcases/lib/tst_timeout_kill.c new file +79 -0

79 lines not on this page · raw message

Signature · 5 lines
2.32.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v3 1/1
#2 lore raw
In reply to Cyril Hrubis

Patch tested well from my side, that'd be great if you can help update code comments in timeout03.sh.

Or, do that separately is also fine, it's not a big deal.

# bash timeout03.sh
timeout03 1 TINFO: timeout per run is 0h 0m 1s
timeout03 1 TINFO: testing killing test after TST_TIMEOUT
Test timed out, sending SIGTERM!
If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1
Terminated
timeout03 1 TBROK: test terminated
timeout03 1 TPASS: test run cleanup after timeout
Test is still running...
Test is still running...
Test is still running...
Test is still running...
Test is still running...
Test is still running...
Test is still running...
Test is still running...
Test is still running...
Test is still running...
Test is still running, sending SIGKILL
Killed
Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi!

Quoted 2 lines

Patch tested well from my side, that'd be great if you can help update code comments in timeout03.sh.

I've added the number back to the "Test is still running..." message and updated the expected test output before pushing. Thanks for the reviews.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp