lib: shell: Fix timeout process races
PATCH v2 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() and because signals are ignored during the operation. My guess that signals are disabled between vfork() and exec(). 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 | 75 ++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 35 deletions(-) create mode 100644 testcases/lib/tst_timeout_kill.c
Changes in v2:
- simplify the message printing
- fix .gitignore entry
testcases/lib/.gitignore +1 -0
@@ -14,3 +14,4 @@ /tst_sleep /tst_supported_fs /tst_hexdump +/tst_timeout_kill
testcases/lib/Makefile +1 -1
@@ -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 +75 -0
75 lines not on this page · raw message
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Cyril,
Quoted 4 lines
Changes in v2:
- simplify the message printing
- fix .gitignore entry
changes LGTM.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Cyril,
Quoted 52 lines
+++
b/testcases/lib/tst_timeout_kill.ctestcases/lib/tst_timeout_kill.c new file +48
-0@@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2021 Cyril Hrubis <[email protected]> +*/ + +#include <stdio.h> +#include <signal.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> + +static void print_help (const char *name) +{ +fprintf (stderr,"usage: %s timeout pid\n" , name); +} + +#define print_msg (fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__) + +int main (int argc,char *argv[]) +{ +int timeout, pid, ret, i; + +if (argc !=3 ) { +print_help (argv[0 ]); +return 1 ; + } + + timeout =atoi (argv[1 ]); + pid =atoi (argv[2 ]); + +if (timeout <0 ) { +fprintf (stderr,"Invalid timeout '%s'\n" , argv[1 ]); +print_help (argv[0 ]); +return 1 ; + } + +if (pid <=1 ) { +fprintf (stderr,"Invalid pid '%s'\n" , argv[2 ]); +print_help (argv[0 ]); +return 1 ; + } + +if (timeout) +sleep (timeout); + +print_msg ("Test timed out, sending SIGTERM!" ); + print_msg("If you are running on slow machine, try exporting
LTP_TIMEOUT_MUL> 1");
Review
Quoted 2 lines
(quoted diff) +2
-0+ + ret = kill(-pid, SIGTERM);
This 'pid' is the parent shell process id, so it obviously that tst_timeout_kill
process would get signal SIGTERM as well.
I'm thinking maybe we should let tst_timeout_kill itself ignore SIGTERM otherwise we have no chance to perform the following double-check code?
testcases/lib/tst_timeout_kill.c line 60
Quoted 11 lines
testcases/lib/tst_timeout_kill.c new file +10
-0+if (ret) { +print_msg ("kill(%i) failed: %s" , -pid,strerror (errno)); +return 1 ; + } + +usleep (100000 ); + + i =10 ; + +while (!kill (-pid,0 ) && i-- >0 ) {
And just check 'pid' but not '-pid' as original shell behavior?
testcases/lib/tst_timeout_kill.c line 65
Quoted 6 lines
testcases/lib/tst_timeout_kill.c new file +5
-0+print_msg ("Test is still running..." ); +sleep (1 ); + } + +if (!kill (-pid,0 )) {
check pid, if so like that.
Quoted 19 lines
testcases/lib/tst_timeout_kill.c new file +4
-0+print_msg ("Test is still running, sending SIGKILL" ); + ret =kill (-pid,SIGKILL ); +if (ret) { + print_msg("kill(%i) failed: %s", -pid,strerror(errno));
testcases/lib/tst_timeout_kill.c new file +6
-1+return 1 ; + } + } + +return 0 ; +} --2.32.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Regards, Li Wang
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 6 lines
This 'pid' is the parent shell process id, so it obviously that
tst_timeout_kill
process would get signal SIGTERM as well.I'm thinking maybe we should let
tst_timeout_killitself ignore SIGTERM otherwise we have no chance to perform the following double-check code?
I guess that signal(SIGTERM, SIG_IGN) a the start of the main() should fix it.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 11 lines · Cyril Hrubis
Hi!
Quoted 6 lines
This 'pid' is the parent shell process id, so it obviously that
tst_timeout_kill
process would get signal SIGTERM as well.I'm thinking maybe we should let
tst_timeout_killitself ignore SIGTERM otherwise we have no chance to perform the following double-check code?I guess that
signal(SIGTERM, SIG_IGN)a the start of themain()should fix it.
It works, but better put it behind of sleep(timeout).
Because we still need to guarantee tst_timeout_kill can be stopped by _tst_cleanup_timer before timeout happening.
testcases/lib/tst_timeout_kill.c +2 -0
@@ -44,6 +44,8 @@ int main(int argc, char *argv[])if (timeout)sleep (timeout); +signal (SIGTERM ,SIG_IGN ); +print_msg ("Test timed out, sending SIGTERM!" ); print_msg("If you are running on slow machine, try exporting
LTP_TIMEOUT_MUL > 1");
@@ -57,12 +59,12 @@ int main(int argc, char *argv[])
i = 10;
- while (!kill(-pid, 0) && i-- > 0) {
+ while (!kill(pid, 0) && i-- > 0) {
print_msg("Test is still running...");
sleep(1);
}- if (!kill(-pid, 0)) {
+ if (!kill(pid, 0)) {
print_msg("Test is still running, sending SIGKILL");
ret = kill(-pid, SIGKILL);
if (ret) {Signature · 2 lines
Regards, Li Wang
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
This 'pid' is the parent shell process id, so it obviously that
tst_timeout_kill
process would get signal SIGTERM as well.I'm thinking maybe we should let
tst_timeout_killitself ignore SIGTERM otherwise we have no chance to perform the following double-check code?
Quoted 8 lines
Quoted 3 lines
I guess that
signal(SIGTERM, SIG_IGN)a the start of themain()should fix it.It works, but better put it behind of sleep(timeout).
Because we still need to guarantee
tst_timeout_killcan be stopped by_tst_cleanup_timerbefore timeout happening.
Right, of course we want the timeout killer to be killable before the timeout.
Quoted 17 lines
testcases/lib/tst_timeout_kill.c +2
-1@@ -44,6 +44,8 @@ int main(int argc, char *argv[])if (timeout)sleep (timeout); +signal (SIGTERM ,SIG_IGN ); +print_msg ("Test timed out, sending SIGTERM!" ); print_msg("If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1");@@ -57,12 +59,12 @@ int main(int argc, char *argv[]) i =10 ; -while (!kill (-pid,0 ) && i-- >0 ) {
testcases/lib/tst_timeout_kill.c int main(int argc, char *argv[]) · line 70
Quoted 10 lines
testcases/lib/tst_timeout_kill.c +2
-1+while (!kill (pid,0 ) && i-- >0 ) {print_msg ("Test is still running..." );sleep (1 ); } -if (!kill (-pid,0 )) { +if (!kill (pid,0 )) {print_msg ("Test is still running, sending SIGKILL" ); ret =kill (-pid,SIGKILL );if (ret) {
Hmm, and of course this does not work since the timeout kill process is around. Maybe we should make this process a separate process group from the start, what about calling setpgid(0, 0) instead of setting up the signal handler? That way we can send the signals to the whole process group and make sure everything has been cleaned up.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 6 lines
Hmm, and of course this does not work since the timeout kill process is around. Maybe we should make this process a separate process group from the start, what about calling
setpgid(0, 0)instead of setting up the signal handler? That way we can send the signals to the whole process group and make sure everything has been cleaned up.
Agreed, that sounds like a great method.
Signature · 2 lines
Regards, Li Wang
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 1 line
Agreed, that sounds like a great method.
Should I send v3 or can I commit v2 with this change?
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 5 lines · Cyril Hrubis
Hi!
Quoted 1 line
Agreed, that sounds like a great method.
Should I send v3 or can I commit v2 with this change?
V3 please, I will help test it all.
I just think of setsid(), is that works and better than setpgid(0, 0)?
Signature · 2 lines
Regards, Li Wang
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Agreed, that sounds like a great method.
Should I send v3 or can I commit v2 with this change?
V3 please, I will help test it all.
Ok, will send ASAP.
Quoted 1 line
I just think of
setsid(), is that works and better thansetpgid(0, 0)?
For moving the process out of the process group setpgid() is enough, we use it in the lib/tst_test.c for the fork_testrun() in order to track all children of the test process as well.
I do not think that we need a new session here, that would probably have strange side effects, for instance it would isolate the tst_timeout_kill process from the terminal the tests were started in.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 10 lines · Cyril Hrubis
Hi!
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Agreed, that sounds like a great method.
Should I send v3 or can I commit v2 with this change?
V3 please, I will help test it all.
Ok, will send ASAP.
Thanks.
Quoted 11 lines
Quoted 2 lines
I just think of
setsid(), is that works and better thansetpgid(0, 0)?For moving the process out of the process group
setpgid()is enough, we use it in thelib/tst_test.cfor thefork_testrun()in order to track all children of the test process as well.I do not think that we need a new session here, that would probably have strange side effects, for instance it would isolate the
tst_timeout_killprocess from the terminal the tests were started in.
Ok, thanks! Let's go with the safe method setpgid(0, 0).
Signature · 2 lines
Regards, Li Wang
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp